FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


ini_set or something other





sonam
Hi,
I would like to insert some data in txt file and this is quite simple if is file in my root folder. But how I can write in file out of root folder. I cannot find something usefull on internet. ini_set("include_path", "somefolder") is working fine with include function but not with write command. How to set path to file out of root folder. I need boht example - for linux and windows.

Thanks,
Sonam
hexkid
Code:
<?php
### uncomment one (and only one) of these defines
define('PATH_TO_FILE', '/var/www/notroot/folder/'); // Linux
#define('PATH_TO_FILE', 'C:/Apache2/htdocs/notroot/folder/'); // Windows

$fh = fopen(PATH_TO_FILE . 'textfile', 'wb');
if (!$fh) {
  // Error opening file. For simplicity sake, print an error and exit
  exit('Error opening file.');
}
fwrite($fh, "One line of text.\n");
fclose($fh);
?>
sonam
Thanks Hexkid,
I am forgot define function. Embarassed
Just one more question. What do you mean, is it better (security reason) to use define for include path or ini_set? Of course, if I can use define for include path (I haven't time for search around, sorry).

Sonam
hexkid
I don't like to rely on ini_set for one reason.

Code:
<?php
// ...
ini_set('include_path', 'c:/includes;c:/php/includes;c:/wwwroot');

// ...

$fh = fopen('textfile', 'rb', 1); // use include path
if ($fh) {
  ### What file did I just open?
  # maybe c:/includes/textfile
  # or was it c:/wwwroot/textfile
  # ????????????????????
}
// ...
?>


Code:
<?php
// ...
define('PATH_TO_FILE', 'c:/wwwroot/');

// ...

$fh = fopen(PATH_TO_FILE . 'textfile', 'rb'); // do not use include path!
if ($fh) {
  ### No questions here
  # I know I've just opened c:/wwwroot/textfile
}
// ...
?>
sonam
Jeah, I think how define is winn in this situation because I will use same folder for included php and txt files. Laughing

Sonam
Related topics
php sessions
PHP Sessions
php.ini
[*READ THIS FIRST!*] Commonly asked questions
*[PRZECZYTAJ NAJPIERW]* Czesto Zadawane Pytania, FAQ, TOS.
Possible to change the timezone? (server 2)
extremely short SESSION lifetime
php include
PHP and relative/absolute paths
JOOMLA: Has anyone succeeded in installing it at FRIH.Net?
Adding blank rows
Hot Or Not script not working
destroy\unset sessions?
image upload, and return url to image
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.