views:

415

answers:

5

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).

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.

A: 

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.

BillThor
I tried my sample script with and without `s` set on the containing `tmp` directory, but I was unable to create the nested folder (see my edit).
Justin Johnson
Are the directories being created as the same user on both domains? Compare the directory tree all the way to /. There could be a permission problem further up the tree.
BillThor
I compared the directory tree all the way up to `/var/www/vhosts` where both domains are located. As far as I could tell, they were identical permissions. The only thing that differed between the two paths were the user. The group is the same for both paths (psaserv), but each domain has a different user (a restriction of Plesk).
Justin Johnson
A: 

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()
              )
        );
streetparade
I tried it, but it makes no difference. Also, this is not how WP creates directories (for uploads at least) and I don't want to fork the codebase for something so simple.
Justin Johnson
A: 

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.

Aaron Harun
Your final solution does work, however, it doesn't address the underlying problem :( If I don't get an answer to that effect, I'll definitely award you the bounty. As a side question, what host did you have this problem on?
Justin Johnson
Some random small shop for a client. I had to wrestle with issue after issue with the host until I finally convinced the client to move to my host after 2 years. Oddly, there just haven't been as many issues since then.It is probably a PHP safe mode thing. I did some googling, and saw a couple recommendations to set the owners to `nobody`. This thread has a bunch of posts about it: http://wordpress.org/support/topic/254069
Aaron Harun
Oh, I assume you tried this, but have you recursively explicitly set the scripts and the folders to the same owner?
Aaron Harun
Yes I have. PHP is running as apache:apache and creates the folders with the same ownership. The problem is that even though the permissions are `rwxrwxrwx`, PHP cannot move uploaded files inside of the folder it has just created. There's something screwed up with the permissions along the way in some parent folder for this specific domain, but I can't find what it is.
Justin Johnson
A: 

I had a similar issue with Joomla recently, and solved the problem by adding myuser into the apache group, and add apache into the psaserv group.

phalacee
This seemed like it would work, but no dice :/
Justin Johnson
A: 

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.

owise1

related questions