Support » Developing with WordPress » my custom image upload function is causing 500 Connection Timeout Error

  • Resolved marcnyc


    I wrote a custom script to insert a post into WordPress and upload 3 images to the WP uploads directory.

    To write the post I use the WP function wp_insert_post( $wp_post_array, true );.
    Inside the script at various stages I also use wp_get_attachment_image_src($image_id, $size)[0];, wp_get_attachment_metadata($image_id); and wp_get_attachment_image( $image_id, 'large', false, $image_attr ); but to upload the images and create their metadata I wrote this custom function below…

    I must have messed up somewhere because I get a 500 Connection Timeout error when I run this code (even though it is only 3 images that are less than 1Mb each in size).

    Can somebody spot what I am doing wrong? Thank you for your eyes and experience.

        function insert_WP_Images_Data( $post_id, $image_url ) {
    	global $writer_WP_id;
    	$upload_dir = wp_upload_dir();
    	if ( isset($image_url) && isset($post_id) ) {
    		$filename = basename($image_url);
    			$file = $upload_dir['path'] . '/' . $filename;
    			$file = $upload_dir['basedir'] . '/' . $filename;
    		$image_data = file_get_contents( $image_url );
    		file_put_contents($file, $image_data);
    		$wp_filetype = wp_check_filetype($filename, null);
    		$attachment = array(
    		'post_author' => $writer_WP_id,
    		'post_content' => '',
    		'post_title' => $_SESSION['artist'],
    		'post_status' => 'inherit',
    		'post_name' => pathinfo($image_url)['filename'],
    		'post_mime_type' => $wp_filetype['type'],
    		'post_parent' => $post_id,
    		'guid' => $upload_dir['url'].'/'.$filename
    		//	'post_title' => sanitize_file_name($filename),
    		$image_id = wp_insert_attachment( $attachment, $file, $post_id );
    		require_once( ABSPATH.'wp-admin/includes/image.php' );
    		$attach_data = wp_generate_attachment_metadata( $image_id, $file );
    		$res1 = wp_update_attachment_metadata( $image_id, $attach_data );
    		$res2 = set_post_thumbnail( $post_id, $image_id );
    		return $image_id;
    	} else {
    		echo '<span class="error">No post is selected or image is selected</span>';

    I have already tried increasing my server execution time in cPanel (200, 600) and via .htaceess (300) but nothing works…

    • This topic was modified 2 years, 1 month ago by marcnyc.
    • This topic was modified 2 years, 1 month ago by marcnyc.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi again!
    Did you look at ?
    What is your $image_url? Is that remote or the temp folder? You aren’t doing any checking on validity of URL.
    If you are uploading images, you can simply call WP’s functions to do that

    There is wp_basename instead of simple basename so it is more flexible.

    Did you start a session in other code? (if not, that variable will not work)

    If you don’t have anything in the error log, you can write stuff to it to see what’s going on.

    Thread Starter marcnyc


    I did not know about sideload. Does sideload handle upload as well as metadata and other DB dependencies?

    The manual says sideload “Downloads an image”, while I’m trying to upload images.

    I’ll look at wp_basename

    Yes I did start a session

    You have to read about it…
    It sounds like you should be using
    instead of your whole function.

    Thread Starter marcnyc


    I shall read about it… so should I use sideload or media_handle_upload? I don’t understand the difference

    Upload comes from a POST from the user’s computer. It checks the POST variables.
    Sideload gets the image from a URL.

    Both have the wrapper function that handles the whole thing, and both have the inner function that you can call yourself to bypass part of the big picture, like sideload an image that you already uploaded in a different way (like legacy images on your server or whatever).

    Thread Starter marcnyc


    ah I see… thanks for clarifying…
    right then I would need media_handle_upload…
    so, does media_handle_upload do everything I try to do with my function? uploading, creating database post, metadata and dependencies?

    That’s where you need to read the code, not ask here.

    Thread Starter marcnyc


    Ok. I’ve been reading for hours but cannot figure out why I keep getting ” ERROR: Specified file failed upload test.” when I try this simple 20-line example on its own…. What am I doing wrong please?

    <form method="post" enctype='multipart/form-data'>
    <input type="file" id="file_to_upload" name="dlkimage1" />
    require_once( ABSPATH.'wp-admin/includes/image.php' );
    if ( isset($_FILES["dlkimage1"]) && !empty($_FILES["dlkimage1"]["tmp_name"]) ) {
    	$image_id = media_handle_upload($_FILES["dlkimage1"]["tmp_name"],0);
    	if ( is_wp_error($image_id) )
    		echo 'ERROR: ' . $image_id->get_error_message();
    		echo $image_id;
    <button class="button" id="updatepreview" type="submit" />Update Preview</button>
    • This reply was modified 2 years, 1 month ago by marcnyc.

    It is clear to me that the first parameter should be the index of $_FILES, not the actual value.
    There is an example on the Code Reference page I gave you (even checks capability).

    Thread Starter marcnyc


    Thank you @joyously

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘my custom image upload function is causing 500 Connection Timeout Error’ is closed to new replies.