Question

given the following running on a windows machine, I would expect a file to be created in the document root directory, but it is not.

$file = str_replace("\\",'/',dirname(__FILE__).'/swsql.bat');
$handle = fopen($file,'w+b'); //using binary mode for windows
fwrite($handle,'test');
fclose($handle);

I am following the documentation given here http://www.php.net/manual/en/function.fopen.php and since the project I'm working on is running php version 4 I cannot use file_put_contents(). Also I have tried it without specifying binary mode and get the same result.

I've used echo to output the $file variable with and without the str_replace. without it the string contains backslashes \ with it the string contains forward slashes / according to the documentation if you don't want to escape backslashes \\ you can use forward slashes as windows respects both. Since dirname(__FILE__) returns unescaped backslashes, I'm fixing it using str_replace. The contents of $file are exactly what I'd expect them to be, the current path of the file being interpreted appended with /swsql.bat/ Also there are no errors in the error log.

EDIT

changing $file = str_replace("\\",'/',dirname(__FILE__).'/swsql.bat');
to $file = str_replace("\\",'/',dirname(__FILE__)).'/swsql.bat'; fixed the issue.

`

Was it helpful?

Solution

changing

$file = str_replace("\\",'/',dirname(__FILE__).'/swsql.bat'); 

to

$file = str_replace("\\",'/',dirname(__FILE__)).'/swsql.bat'; 

fixed the issue.

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