Permission denied on mkdir()

11,121

Solution 1

is_writable() is probably the function you're looking for.

http://cz.php.net/manual/en/function.is-writable.php says:

Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.

Also, the directly next line is relevant here:

Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').

In other words, check if your directory is writable by the user id of the web server - this may be a different user id than yours! Set the appropriate permissions - e.g. set the usergroup of the folder to that of the server's user, and grant read, write, and execute to group. (chgrp somegroup uploads; chmod g+r uploads; chmod g+w uploads; chmod g+x uploads)

Solution 2

Make sure the parent folder is writable to the process that the web server runs as.

Edit: Oops, premature reply. Does your host give you a GUI file browser thingy?

Share:
11,121
Scott B
Author by

Scott B

Updated on June 10, 2022

Comments

  • Scott B
    Scott B about 2 years

    I'm getting the following error when trying to call mkdir() on a server...

    Warning: mkdir() [function.mkdir]: Permission denied in /home/server/public_html/wp-content/themes/mytheme/catimages/cat-images.php on line 373

    The function is below. Its attempting to create a folder under the site's "wp-content/uploads folder". I've verified that the PHP Version is 5.2.15 and that the files inside the theme folder are writable, but that does not necessarily means the uploads folder is writable I suppose.

    How can I find out if the uploads folder is writable?

    protected function category_images_base_dir()
    {
        // Where should the dir be? Get the base WP uploads dir
        $wp_upload_dir = wp_upload_dir();
        $base_dir = $wp_upload_dir[ 'basedir' ];
        // Append our subdir
        $dir = $base_dir . '/cat-images';
        // Does the dir exist? (If not, then make it)
        if ( ! file_exists( $dir ) ) {
            mkdir( $dir ); //THIS IS LINE 373
        }
        // Now return it
        return $dir;
    }
    
  • Scott B
    Scott B over 13 years
    Thanks for the excellent answer. I'm going to insert some additional code checks to verify is_writable() state of the parent folder and add exception handling in that event.