Home » Php » php – Permission denied on mkdir()

php – Permission denied on mkdir()

Posted by: admin July 12, 2020 Leave a comment

Questions:

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;
}
How to&Answers:

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)

Answer:

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?