views:

424

answers:

5

I am working in php on ubuntu. When I use any image on web page which has denied access, there is warning on page. I want to check it before displaying and if it does not have rights to open then give it access to open. As we do in terminal command.

chmod 777 myimage.jpg

How to check this and give full access to a file in php.

Thanks

+7  A: 

Check the function is_readable() and is_writable().

Example:

$filename = '/home/myuser/example.txt';

if (is_readable($filename) && is_writable($filename))
{
    echo "File has read and write permissions.";
}
rogeriopvl
You did not discuss about giving access to a file using chmod.
NAVEED
You can't chmod() a file if the server is not the owner. And setting 777 permission on a file is really bad idea. The only way you can give your server full access on images is to use an image file upload form that PHP (server) saves in the filesystem. That way the image file is created by the server, therefore is the owner.
rogeriopvl
Yes, I want same thing. In my case, an user add his photo to his/her profile Then server should be owner of that image. But the image is not readable after uploading. It means server can change access rights after uploading?
NAVEED
could you add to your question the code where you save the file?
rogeriopvl
+2  A: 

Use is_readable() to check whether or not the file is readable by the PHP process.

Use chmod() to change the permissions of the file.

Also, you can use is_writable() to test if you can write to the file, and file_exists() to check to see if the file even exists.

Justin Johnson
I have this error when I use chmod function: chmod() [function.chmod]: Operation not permitted.
NAVEED
In order to use chmod the web server needs to be the file owner, or you'll get that error.
rogeriopvl
+1  A: 

Doing this on the fly from PHP every time a file is referenced is a very inefficient way to manage your files. It also requires all file access to be mediated via a PHP script. Also, allowing content to be world writeable is rather messy from a security point of view.

I'd go with running an admin script once to tidy up the permissions for your existing files, then fixing the problem when new files enter the system.

Sure, if you've not got shell access / shell access as someone other than the webserver uid, then you'll have to implement this using PHP (and therefore readdir/is_readable/is_writeable).

Without knowing how files appear on your webserver its hard to recommend a specific solution.

C.

symcbean
+2  A: 

One thing you can do is use the fileowner function (and posix_getpwuid) and compare to whatever your PHP user is (often www-data).

If the users are the same you will be able to change permissions if you need to. But first check if the file is writeable anyway.

UPDATE: the chmod and chown functions return TRUE on success and FALSE on failure, so it would be a good idea to put them in an if clause. You can suppress the error output by setting error_reporting(0); at the beginning of the script, or using the @ symbol like this:

if ( @chmod($filename, 0666) ) {
    // do whatever with file
}
else if ( @chown($filename, 1000) ) {
    chmod($filename, 0666);
    // do whatever with file
}
else {
    // can't change permissions
}
DisgruntledGoat
Is it possible to give full access to a file using chmod method after knowing file owner. Even in ubuntu, when I am logged in as administrator and I have all rights, I have to enter password on terminal to give access using sudo.
NAVEED
I don't think there is a PHP function to do that, but you can try the `exec` function which lets you execute commands on the server. http://php.net/manual/en/function.exec.php
DisgruntledGoat
A: 

One thing you can do to make the file readable / writable is to call this function upon file / folder creation without the second argument:

function AutoChmod($path, $chmod = null)
{
    if (file_exists($path) === true)
    {
        if (is_null($chmod) === true)
        {
            $chmod = (is_file($path) === true) ? 644 : 755;

            if (in_array(get_current_user(), array('apache', 'httpd', 'nobody', 'system', 'webdaemon', 'www', 'www-data')) === true)
            {
                $chmod += 22;
            }
        }

        return chmod($path, octdec(intval($chmod)));
    }

    return false;
}

Example:

AutoChmod('/path/to/file/you/just/created.txt');

This function will give appropriate permission whether you are working with SuPHP / SuExecPHP or not.


To check permissions you just need to use the functions is_readable() and is_writable().

Alix Axel