Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

test.php opens test.txt and writes to it. It works fine on one hosting server.

After migrating the code to another host, test.php now can't write out test.txt. The error shows:

Warning: fopen(/home/username/public_html/test.txt)
[function.fopen]: failed to open stream: Permission denied

It will only work when the test.txt file is set to 777. On the original host, test.txt was set to 755.

What am I missing to give permission for test.php to write to the file without 777 on this new host?

Also, file upload PHP scripts aren't working either.

share|improve this question
2  
try to give permition to the whole folder –  user2051349 Feb 19 '13 at 22:36
    
The test file is in the public_html folder, which is set to 750. What should I set it to? Why not just the file? –  detailCode Feb 19 '13 at 22:39
1  
On the previous host, php was running with file owner permissions, on new one it is running as user with limited permisions (like www, apache or nobody) and can't change other users files. –  dev-null-dweller Feb 19 '13 at 22:39
    
@dev-null-dweller Thanks! That should interesting. Any URL references on running PHP with file owner permissions? –  detailCode Feb 19 '13 at 22:40
1  
You should contact your host provider for that (or change host provider for one that can provide that basic requirement for most php apps) –  dev-null-dweller Feb 19 '13 at 22:44

1 Answer 1

up vote 1 down vote accepted

It is not just the permission that is important, but also who owns the files. 755 is enough if the folder/file is owned by the same user that is used for running the web server.

Writing directly to the public_html directory is a huge security risk. I suggest creating a subdirectory (probably best outside the public_html) folder and give it the appropriate permissions / ownership.

It is safer to have the folder owned by the user that runs apache (or whichever web server is being used) - usually something like apache or www-data instead of giving world write permissions.

share|improve this answer
    
I'll move it. This user access stuff, can it be setup in .htaccess or php.ini? –  detailCode Feb 19 '13 at 22:43
1  
no, its *nix ownership/permissions - something you need to do on the commandline/ftp using chown (for changing owner) and chmod (for permissions). If you can't change owner, setting permissions to be permissive may be our only option but restricting it to a standalone folder minimises your risk. –  drone.ah Feb 19 '13 at 22:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.