Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Directory Permissions Check

Tags:

php

I have a PHP script that needs to check the permissions on a directory to make sure it is writable. What I have so far is:

$perms = substr(sprintf('%o', fileperms($folder)), -4);
if ($perms == "0777" || is_writable('temp'.DS)) 
{
    //code here
}

Is this a sufficient check?

like image 858
David Thompson Avatar asked Sep 05 '12 15:09

David Thompson


People also ask

How can I tell if a directory is writable in PHP?

PHP | is_writable() Function The is_writable() function in PHP used to check whether the specified file is writable or not. The name of the file is sent as a parameter to the is_writable() function and it returns True if the filename exists and is writable.

How do you inspect file permission in PHP?

Checking file permissionsis_readable() function returns true if the file exists and is readable; otherwise, it returns false . is_writable() function returns true if the file exists and is writable; otherwise, it returns false .

What permissions should PHP files have?

Set php files to 640. For maximum security you should set minimum permissions, which is 640. The owner 6 would be the one uploading the files.


1 Answers

There's no need to check the permissions manually, it's enough to use use is_writable and is_dir:

if (is_dir($myDir) && is_writable($myDir)){
   //do stuff
}
like image 156
Adi Avatar answered Nov 02 '22 23:11

Adi