permission denied after using chmod, why, how....

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
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

permission denied after using chmod, why, how....

Post by kryles »

pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I am trying to write to a file on the server. When I try opening the file I get

Code: Select all

 
fopen(file.csv): failed to open stream: Permission denied in path/to/file.csv
 
Next I tried chmod() before opening the file to 0755

Code: Select all

 
if(file_exists("./file.csv"))
{
        chmod("./file.csv", 0755);
    $fp = fopen('./file.csv', 'w');
 
    $query = "SELECT name
              FROM names
              WHERE name = 'Jim'";
    $result = mysql_query($query);
 
        while($row = mysql_fetch_row($result))
    {
        fputcsv($fp, $row[0]);
    }
 
    fclose($fp);
}
 
and I get the following error.

Code: Select all

chmod(): Operation not permitted
My guess is this is a server issue? Some kind of setting on it is preventing me from using these? Or maybe I am using them wrong? Any suggestions would be helpful (The server is a hosting service, not my own).

Thanks,


pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: permission denied after using chmod, why, how....

Post by Zoxive »

The server probably doesn't have permissions in the folder which file.csv is in.

You need to change the permissions manually. (Not from within php, because that is the problem to begin with php can't modify that file)
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: permission denied after using chmod, why, how....

Post by kryles »

Okay this is the hierarchy

Public_html -> folder1 (0715) -> file.csv(0644)

Wouldn't these allow access to the file? how should I change the permissions on them if not?

Thanks,
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: permission denied after using chmod, why, how....

Post by pickle »

Ownership is probably the issue, not access permissions. If the user PHP is running as doesn't have rights to the file/directory, you won't be able to use PHP to change rights or ownership.

Like ~Zoxive said, you'll have to go in via the CLI & manually either set the ownership of the offending files/directories to be owned by PHP's user, or open up the privileges so the whole world can modify those files.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: permission denied after using chmod, why, how....

Post by Benjamin »

Pickle is correct. You can also more than likely just ftp in to add global read permissions. The directory that the files are stored in will need to have execute permissions so that the apache user can open them.
Post Reply