David Thompson
David Thompson

Reputation: 115

PHP Directory Permissions Check

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?

Upvotes: 5

Views: 8874

Answers (2)

mmcachran
mmcachran

Reputation: 484

PHP's is_writable() should be sufficient. Below is the description from the PHP manual:

The filename argument may be a directory name allowing you to check if a directory is writable.

See http://php.net/manual/en/function.is-writable.php for more details.

Upvotes: 6

Adi
Adi

Reputation: 5179

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
}

Upvotes: 8

Related Questions