Relevenz App

Relevenz is now available from iTunes for your iPhone. See more about this great new calendar app at www.relevenz.com.

Tuesday, April 09, 2013

Wordpress: Error Saving Media Attachment

The Problem

I am not the first person to come up against this problem when installing Wordpress. Google has 38,000 pages in the search results for just such a search query, some dating back over three years!

This problem occurs when you try to upload a media file to the blog (either in the library or inside a post). Unfortunately, it's not a very helpful message, nor is it obvious why the media file could not be saved. At least that would be helpful in trying to solve the problem.

Diagnosis

So I did try all the solutions that are out there on the web.

1. In the standard install, the uploads directory does not exist in the wp-content directory. Wordpress creates it the first time you try to upload. So I made sure that the correct write permissions were on the wp-content directory.

2. Checked that the PHP settings allowed the files to be uploaded:
file_uploads = On
upload_max_filesize = 2M

3. Frustrated myself slightly by trying to find where in the settings I could specify the upload folder. In the latest version () this setting doesn't appear anymore.

I still couldn't get it to work.

As a desperate last resort, I decided to pepper the Wordpress code with display statements out to a log file. And here is where I finally made the breakthrough.

It turns out that there is a bug in PHP that causes the $_FILES array to be incorrectly initialized. This means that when Wordpress is looking for the details of the file to be uploaded, they don't exist. This triggers the unhelpful error message.

Solution

Upgrade the PHP version to the latest production version. Simple.

Reflection

I don't like the fact that the PHP code was not failing properly. The Wordpress code makes several tests on a file, not based on the file itself, but on what PHP tells it the file looks like. So if the 'size' element in the array is missing, it triggers an error. This is not the same as the 'size' element evaluating to 0, which is what it should be testing.

Of course, it can be argued that the problem was with the underlying PHP version, not Wordpress. But this is small help to people who might not have the ability to pull apart the code.


No comments: