zettca
zettca

Reputation: 17

PHP fwrite() cannot write to subdirectory

My website is on /var/www/html/hs/, where I have my update.php. It is supposed to write to /var/www/html/hs/json/myFile.json, but it just doesn't. When I try writing to the same directory of update.php, no problems seem to occur.

update.php file-writing code part thing:

$fileJSON = fopen("json/$fileName.json", "w");
fwrite($fileJSON, $JSONE);
fclose($fileJSON);

I (believe) have already given myself the necessary permissions through sudo chown -R myName /var/www/.

System: Linux ubuntu 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64

PHP Version 5.5.9-1ubuntu4

Apache Version Apache/2.4.7 (Ubuntu)

Upvotes: 0

Views: 663

Answers (2)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137467

If the json/ directory doesn't exist, you'll need to create it:

if (!is_dir('json'))
    mkdir('json');

Upvotes: 1

nl-x
nl-x

Reputation: 11832

I think you don't have the necessary permissions. Check what your error_log has to say about this!

You should not chown to your own username, but the use username that is running Apache. (Use ps -aux | grep httpd to see which user it is. Once chowned, the directory might become inaccessible for you if you don't also have root.

Another thing you can do is chmod the directory that you want to write to to 777.

Upvotes: 0

Related Questions