permission warning for file_put_contents but works anyway

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Matt2012
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 5:31 am

permission warning for file_put_contents but works anyway

Post by Matt2012 »

Im using file_put_contents and both the file its in and the folder its posting the file to have correct permissions the operation is successful
(the new file is put in the folder) but I get this error message.

The file and folder are in root the script is getting a database backup using cpanel url. The script is run by cron.

<b>Warning</b>: file_put_contents(/../../) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Permission denied in <b>/../../../</b> on line <b>21</b><br />

So why the error?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: permission warning for file_put_contents but works anyway

Post by infolock »

sounds to me like you're doing this to multiple files (or are trying to read a different file).

This error can come up with one of 5 reasons:

1) Cannot create file

2) Cannot read file

3) Cannot execute file

4) Cannot write to file

5) Cannot delete file

What are the permissions set to the file(s) ?
Matt2012
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 5:31 am

Re: permission warning for file_put_contents but works anyway

Post by Matt2012 »

permissions are 777 and the new files are being created but still getting the error message

im basically cycling through all my databases and making a backup to root
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: permission warning for file_put_contents but works anyway

Post by pickle »

I'm sure you can't get a "no permissions" error on an operation, and have it also work. I'm with ~infolock - you're likely doing this multiple times & the failure is happening on a different file.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Matt2012
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 5:31 am

Re: permission warning for file_put_contents but works anyway

Post by Matt2012 »

I am doing it multiple times

im also getting an error for each occurrence

and each occurence is working!

strange I know my hosts tell me I can ignore the error so I guess im leaving it but it dosent feel quite right.

I think the issue maybe due to certain restrictions my hosts have put on root anyway ho hum..
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: permission warning for file_put_contents but works anyway

Post by infolock »

in that case, check to make sure group/owner privs are right (not only on the files, but the directory where the files live as well)
Matt2012
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 5:31 am

Re: permission warning for file_put_contents but works anyway

Post by Matt2012 »

Solved this issue by unlinking the file being written over prior to the file_put_contents,

like this...

Code: Select all

    $path_week = $home."/weekly/database".date('D').".sql.gz";
    if (file_exists($path_week)) unlink($path_week);
    $handles['week'] = file_put_contents($path_week,$file);
My host dont allow chmod in php.ini
Post Reply