Support » Developing with WordPress » wp_generate_attachment_metadata generates 503 Service Unavailable or 500 timeout

  • I’ve been struggling for a week with this…
    I was trying to upload an image with media_handle_upload but it never worked (it always timed out), so I decided to break it down to see where it failed and I wrote this:

    
    if ( move_uploaded_file($file["tmp_name"], $target_file) ) {
    	$wp_filetype = wp_check_filetype($filename, null);
    	$attachment = array(
    		'post_author' => $_SESSION['writer_WP_id'],
    		'post_title' => $_SESSION['artist'],
    		'post_status' => 'inherit',
    		'post_name' => $filename,
    		'post_mime_type' => $wp_filetype['type'],
    		'guid' => $target_url.'/'.$filename
    	);
    	$image_id = wp_insert_attachment( $attachment, $target_file, null, true );
    		if ( is_wp_error($image_id) && !empty( $image_id->errors ) ) {
    			echo '<span class="error">There was an error inserting your image attachment. - ' . $image_id->get_error_message() .'</span><br />';
    		} else {
    			// The image sub-sizes are created during wp_generate_attachment_metadata().
    			// This is generally slow and may cause timeouts or out of memory errors.
    			$image_attachment = wp_generate_attachment_metadata( $image_id, $target_file );
    			if ( !$image_attachment ) {
    				echo '<span class="error">There was an error generating attachment metadata</span><br />';
    			} else {
    				if ( !wp_update_attachment_metadata( $image_id, $image_attachment ) ) {
    					echo '<span class="error">There was an error attaching info to your your file</span><br />';
    				} else {
    					echo '<span class="success">Thank you for uploading your <b>'.str_replace('dlk','',$which_image).'</b>.</span><br />';
    					$_SESSION['ready_to_publish'] = 1; // after this the user CAN publish if they don't want to add more images
    				}
    			}
    		}
    	} else {
    		echo '<span class="error">There was an error uploading your file</span><br />';
    	}
    
    

    The file gets uploaded (move_uploaded_file works) and gets inserted into the dB (wp_insert_attachment works) but it fails every time with 500 or 503 when it reaches wp_generate_attachment_metadata… how can I tackle this? it’s driving me mad…

    • This topic was modified 1 year, 7 months ago by marcnyc.
    • This topic was modified 1 year, 7 months ago by marcnyc.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Often times when you get unexpected timeout errors it’s because an infinite loop of some sort has been encountered. These can be pretty obscure and difficult to identify. It’s not clear in what context your code is called so it’s hard to speculate in any greater detail. I take it your code is a means of investigation. That’s fine, but what is your original usage of media_handle_upload() that’s causing the original problem?

    Why would you do this instead of relying upon the WP UI to handle uploads? It’s OK to manage your own upload, I’m trying to understand your motive because there might be a better approach.

    Thread Starter marcnyc

    (@marcnyc)

    Thank you for your reply @bcworkz
    Basically I have contributors that are making a mess with their posts and every post looks different so I wrote a script that has a multi-step form for the contributors to enter all the data and the script to create consistent-looking, consistently-laid out, consistently-designed articles (in this case interviews).

    Once I have the data I use wp_insert_post() to put it into WP.
    Every article has 4 images.

    The script is beautiful and does exactly what I need but the images that go with the article need to go from the contributors computer into WP. Initially I was hoping to use media_hand_upload() and do it with one WP function. It worked with small images (<100kB) but would time out with 4 images of less than 1Mb each, so then I added steps to upload every image one after the other, but even with a single 1Mb image it would still time out… so that brought me to this point…

    I figured since media_handle_upload() keeps timing out, I’ll break down the steps with error checking between them that that function performs into multiple steps (hence the function I posted).
    My steps are:
    1. move_uploaded_file to put the files from user into wp uploads folder
    2. wp_insert_attachment to create dB attachments
    3. wp_generate_attachment_metadata to create the subsizes…

    It always times out at step 3… and that’s where I’m at… I don’t get an error message, just a timeout

    • This reply was modified 1 year, 7 months ago by marcnyc.
    Moderator bcworkz

    (@bcworkz)

    Creating multiple image files does take a lot of time and server resource, which seems to be lacking. How many image sizes are defined? Do you have similar trouble uploading via the media library? You might try using a plugin like Advanced Custom Fields to collect image uploads for a post. It can also generate image sizes and attachment posts, but the images are not actually part of post content, it’s up to your own code to work them into a post in a structured manner. Such a plugin is a common way to collect user input in a structured manner to address exactly the issue you are trying to solve.

    Thread Starter marcnyc

    (@marcnyc)

    I believe 5 or 6 su sizes are created but not sure (where can I check this?)

    I’ve developed too much logic, conditions and code to switch to a plugin and restart so I’d like to get this to work.

    The obvious solution is to not create the subsizes but I’d love to find a way to have the subsizes to increase compatibility and responsiveness

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_generate_attachment_metadata generates 503 Service Unavailable or 500 timeout’ is closed to new replies.