0

I am able to write .txt file with the following code but unable to do so with php files(existing or newly created with the following code) on localhost.

Also it echo string length correctly without any error notice or warning

<?php
$file = fopen("test.php","w");
echo fwrite($file,"do not vote me down");
fclose($file);
?>

Thanks in advance

7
  • Please post more details of the problem. Whether the file is being created, if it is being created, are the contents written to the file, etc... Commented Dec 14, 2016 at 6:11
  • @captainblack yes file is created for the first time but then i cant make any further changes to it (php files) Commented Dec 14, 2016 at 6:12
  • do i need to use chmod or something? Commented Dec 14, 2016 at 6:14
  • are the contents of the file empty or the content got saved? Commented Dec 14, 2016 at 6:19
  • It shows the same old content Commented Dec 14, 2016 at 6:20

3 Answers 3

3

may be a ownership problem. try do chmod -R 775 /var/www/folder and/or chown -R domain:www-data /var/www/folder - where domain is the user of that particular virtualhost or www-data

Also to update a file instead rewrite you can use append option - adding + will help to create the file first time if it is not exists.

$file = fopen("test.php","a+");
echo fwrite($file,"do not vote me down ab");
fclose($file);

And If you want to change the ownership of the file you can use Php chown function-

chown('test.php','apache');

Too change permission of file or folder recusively use this code -

exec ("find /path/to/folder -type d -exec chmod 0750 {} +");
exec ("find /path/to/folder -type f -exec chmod 0644 {} +");
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe your file will not open so you can add die() function after fopen(), review below code :-

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

5 Comments

Nah its still the same
problem is only with php file that I can not overwrite them
make sure .txt file is store in same directory with .php file.
<?php $myfile = fopen("test.txt", "w") or die("Unable to open file!"); fwrite($myfile,"do not vote me down"); fclose($myfile); ?>
just remove echo from second line @sam
0

Your code works. I can rewrite the content, as long as test.php has appropriate file permission.

5 Comments

Ok ! can i change permission of file in php before opening it in write mode
You can. chmod function will help you achieve that. Alternatively, you can create a sub folder and set permission to that folder instead. lets say if you create a folder 'test_data' and set recursive permission to that folder, any file created under that folder will inherit the same permissions.
<?php $file = fopen("test_data/test.php","w"); echo fwrite($file,"do not vote me down rewritting 12345"); fclose($file); ?>
I tried it this way but to no success <?php chmod("test.php",0755); $file = fopen("test.php","w") or die("Unable to open file!"); echo fwrite($file,"do not vote me down"); fclose($file); ?>
try moving that file inside a folder, set 777 for that folder and file and try to execute the code. IMO, it is a permission issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.