Cannot upload media via WordPress uploader

This has to do with media uploading in WordPress.

Every time WP creates a folder for new uploads (it organizes uploads by year and month: yyyy/mm), it creates it with the “apache:apache’ user and group, with full access to all (777 or drwxrwxrwx).

Read More

However, after that, WP cannot create a folder within that folder (e.g.: mkdir 2011 succeeds, but mkdir 2011/01 fails). Also, uploads cannot be moved into these newly created folders even though the permissions are 777 (rwxrwxrwx).

Once a month, I have to chown the newly created folders to be the same as user:group as the rest of the files. Once I do that, uploading works fine (which doesn’t make sense to me The really frustrating part is that this problem doesn’t exist in other WP installs on other domains on the same server.

* I wasn’t sure if this should be here or on serverfault.


Edit: The containing directory /.../httpdocs/blog/wp-content/uploads has the correct ownership

drwxrwxrwx 5 myuser psaserv 4096 Jun  3 18:38 uploads

This is a Plesk/CentOS environment hosted by Media Temple (dv).

I’ve written the following test script to simulate the problem

<pre><?php 

$d = "d" . mt_rand(100, 500);

var_dump(
    get_current_user(),
    $d,
    mkdir($d),
    chmod($d, 0777),
    mkdir("$d/$d"),
    chmod("$d/$d", 0777),
    fileowner($d),
    getmyuid()
);

The script always creates the first directory mkdir($d) successfully. On domain A, where the WP problem is, it cannot create the nested directory mkdir("$d/$d"). However, on domain B, both directories are successfully created.

I am running each script at /var/www/vhosts/domainA/httpdocs/tmp/t.php and /var/www/vhosts/domainB/httpdocs/tmp/t.php respectively I checked the permissions on tmp, httpdocs, and domain[AB] and they are the same for each path. The only thing that differs is the user.

Related posts

Leave a Reply

9 comments

  1. A solution is to use FastCgi. This makes PHP run as the user who owns the site. New files and folders will be the same user and group. This will solve your problem.

    There is a performance penalty to FastCgi but you get some added security as it restricts php. If you are hosting multiple website with multiple users this could be a good idea.

  2. Try going to your miscellaneous settings page (or media depending on your version) and make sure the upload directory is still wp-content/uploads.

    If you need to. set the full url too.

    Also, as a final solution, disable the option to organize them into folders so that way WordPress doesn’t even need to create folders.

  3. Check for a setuid or setgid bit on a directory above the 2010 directory. ls -l will have an s or S in the permissions for the directory. Make sure this directory has the correct ownership.

  4. Try to create directory recursive with mkdir($d, true)

    <pre><?php 
    
    $d = "d" . mt_rand(100, 500);
    
    var_dump(
             array(
                   get_current_user(),
                   $d,
                   mkdir($d,true),
                   chmod($d, 0777),
                   mkdir("$d/$d", true),
                   chmod("$d/$d", 0777),
                   fileowner($d),
                   getmyuid()
                  )
            );
    
  5. One of our websites on a Media Temple DV was having this problem. Turning PHP Safe Mode off solved it. The directories were still created as apache:apache, but the media files were allowed in there.

  6. One thing that occurred to me – WP will tell you that it can nto copy file to /wp-content/upload even when all permissions are right…. if

    upload_max_filesize

    in php.ini is too small (say 2M and you try to upload 3.5MB file)!

    Hope that helps all thsoe who have right permissions but still can not upload!

  7. You shouldn’t need 777 on your directories, 775 at most should be sufficient. Just make sure it’s set on the uploads directory with 755 for all the other directories above.

    Also, you could try to chown it to www-data, sometimes that helps when your ftp user that you are logged in as when changing the permissions once a month doesn’t have sufficient access level and owning the directories by that user prevents the server from being able to write into them.

    Lastly, as someone has pointed out above, you may need to up the upload size limit along with making sure other file upload related php.ini settings are correct:

    http://php.net/manual/en/ini.core.php

    http://kb.mediatemple.net/questions/137/How+can+I+edit+the+php.ini+file%3F#dv