Question

I ran into a really bizarre problem. I am trying to perform writing to file using fopen().

This is what I tried in writetofile.php:

$fw = fopen('/test.txt', 'w');
fwrite($fw, 'hello world' . "\r\n");
fclose($fw);

This is the error I keep getting:

Warning: fopen(/test.txt): 
failed to open stream: Permission denied in C:\inetpub\wwwroot\writetofile.php on line 41
Warning: fwrite() expects parameter 1 to be resource, boolean given...

I am 100% sure I have permissions to the server. I am the Administrator. Furthermore, I temporarily gave full permissions to everyone. I even tried running the php script locally, directly from the server using localhost. I am not using apache, I am using IIS. I tried restarting IIS after modifying permissions. I am not running php in safe mode.

Any idea on what might be causing this issue?

Was it helpful?

Solution

/test.txt would be a file in the ROOT directory of your filesystem, where user accounts generally do NOT have write privileges (unless you're running this code as root). This is especially true of PHP running under the webserver's user account.

You probably want just test.txt (no leading slash)` which will try to put the file into the script's "current working directory" - usually the same directory the script itself is in.

OTHER TIPS

1- when you rollout website, delete all logs folder names

2- inside the code create folder name as below and create the logs insides

3- write at top of file. (during init the web)

$ClientUserName   = gethostbyaddr($_SERVER['REMOTE_ADDR']);

function Data_Log($dataline)
{
 global  $ClientUserName;
 $dir = 'UserInputLog' ;
 $fileName = $ClientUserName. '_ServerWebLog.txt';

 if(is_dir($dir) === false)
  mkdir($dir);

  $fileName = $dir. '\\'.$fileName;

  $myfile = fopen($fileName, "a") or die("Unable to open file!");
  fwrite($myfile, "$dataline\r\n");
  fclose($myfile);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top