Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP can't write files

I had a Apache HTTP server on CentOS, I installed PHP (yum install php) and then tested a simple script that writes text in a file, so I do

$file = fopen($filename,"w") or die("Failure"); 

The problem is that it's always a failure, even after I did a chown apache:apache /var/www/html/* or a chmod 777 * in that directory, so anyone knows a way to understand / fix this?

EDIT : So there the problem was thath the directory itself did'nt have the chown

like image 309
Traxys Avatar asked Oct 22 '25 05:10

Traxys


1 Answers

It's hard to say what it is without the actual error message from PHP.

Have a look at this page:

http://www.w3schools.com/php/php_error.asp

Long story short, create and set a custom error handler, like so:

<?php
//error handler function
function customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr";
}

//set error handler
set_error_handler("customError");

//trigger error
$file = fopen($filename,"w")
?>

In your case, I think that in order for it to be able to create new files in the given directory, you would want to add a '-R' flag to chmod or chown and call it on the directory itself rather than the children -- that way, if PHP has to create the file, it has permissions to do so.

EDIT: Just curious about why this has a downvote -- what is the "egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect."? I told the OP to examine the error messages and set their permissions in a properly recursive fashion.

like image 77
Laereom Avatar answered Oct 24 '25 20:10

Laereom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!