• I’m reallu struggling with 500 Timeout Errors when uploading 4 images of less than 800kb each… I’ve tried placing ini_set('max_execution_time', 0); at the top of my code right after session_start(). I also tried setting php_value max_execution_time 300 in .htaccess and increasing the value in cPanel. I’m at a loss…

    I wrote a function that performs all the checks and then uses media_handle_upload() to upload the files. The function is called 4 times in the code later with lines like this one:

    if ( isset($_FILES["dlkimage1"]) && !empty($_FILES["dlkimage1"]["tmp_name"]) )
              fileUpload($_FILES["dlkimage1"],'dlkimage1');

    I have also tried checking whether the $_SESSION ID of the image ID was set (which I set at the end of the upload function when a file was correctly uploaded) on the following upload, like this:

    if ( isset($_FILES["dlkimage2"]) && !empty($_FILES["dlkimage2"]["tmp_name"]) && 
         isset($_SESSION['imagesuploaded']['dlkimage1_wp_id']) )
              fileUpload($_FILES["dlkimage2"],'dlkimage2');

    But even with that extra check it gets stuck. When I look at the WP uploads folder I see the first image gets uploaded and nothing after that…

    Here is the function:

    function fileUpload($file,$which_image) {
            global $images_upload_folder;
            global $max_file_size;
            define ('SITE_ROOT', realpath(dirname(__FILE__)));
            $target_dir = $images_upload_folder;
            if ( isset($file) && !empty($file["tmp_name"]) ) {
                $filename = preg_replace('/\s+/','_', $file["name"]);   // remove white spaces from file name and replace with '_'
                $target_file = $target_dir . basename(cleanString(hyphenize($_SESSION['artist']).'_'.hyphenize($filename))); // where our file will live and what it will be called
                $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));   // image extension
    
                echo '<small>';
    
                // Check if image file is a actual image or fake image
                $check = getimagesize($file["tmp_name"]);
                if( $check !== false ) {
                    // echo "File is an image - " . $check["mime"] . ".";
                    $uploadOk = 1;
    
                    // Check if file already exists
                    if ( file_exists($target_file) ) {
                      echo '<br /><span class="error">A file with the same name already exists.</span>';
                      $uploadOk = 0;
                    }
    
                    // Check file size
                    if ( $file["size"] > $max_file_size ) {
                      echo '<br /><span class="error">This file is too large. Please keep files under '.( $max_file_size / 1000000 ).'Mb.</span>';
                      $uploadOk = 0;
                    }
    
                    // Allow only certain file formats
                    if ( !empty($file) && $imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
                      echo '<br /><span class="error">Only JPG, JPEG, PNG & GIF files are allowed.</span>';
                      $uploadOk = 0;
                    }
    
                    // Check if $uploadOk is set to 1 and, if so, proceed!!!
                    if ( $uploadOk == 1 ) {
                        if ( !empty($file) ) {
                            $wp_image_array = array(
                                'post_author' => $_SESSION['writer_WP_id'],
                                'post_title' => $_SESSION['artist'],
                                'post_status' => 'inherit',
                                'post_name' => hyphenize($_SESSION['artist']).'_'.hyphenize($_SESSION['imagesuploaded'][$which_image]['name']).'_'.$which_image
                            );
    
                                $image_id = media_handle_upload(
                                            $which_image,       // file to upload
                                            $post_id,           // ID of image post
                                            $wp_image_array );  // image-specific data
                                if ( is_wp_error($image_id) )
                                    echo '<br /><span class="error">There was an error uploading your file. - ' . $image_id->get_error_message() .'</span>';
                                else
                                    echo '<br /><span class="success">Thank you for uploading your <b>'.str_replace('dlk','',$which_image).'</b>.</span>';
    
                        }
                    } else {
                        echo '<br /><span class="error">Sorry, your file could not be uploaded.</span>';
                        $uploadOk = 0;
                    }
    
              } else {
                echo '<br /><span class="error">This file is not an image.</span>';
                $uploadOk = 0;
              }
    
                echo '</small>';
    
            }
        if ( $uploadOk == 1 ) {
            // adds WHICH and WHAT image it is in the images uploaded array
            $_SESSION['imagesuploaded'][$which_image] = $file;  
            // add the WP image id into the session         
            $_SESSION['imagesuploaded'][$which_image.'_wp_id'] = $image_id;
            return $_SESSION['imagesuploaded'];
            unset($target_dir);
        }
    }

    Where am I failing?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’ve tried placing ini_set(‘max_execution_time’, 0); at the top of my code right after session_start(). I also tried setting php_value max_execution_time 300 in .htaccess

    Try with a smaller image, just one. See what happens. Also contact your hosting tech support, and ask them to make sure that execution time is set to 300 (and upload size to 25 MB)

    Thread Starter marcnyc

    (@marcnyc)

    Thanks for the reply @a2hostingrj

    I have tried with smaller images and it works if they are really small but even with just one image of 1mb it times outs.
    I tried settings the max exec Time to 3000 and the upload size to 25 in my cPanel settings but to no avail

    I tried settings the max exec Time to 3000 and the upload size to 25 in my cPanel settings but to no avail

    Did you talk with your hosting tech support?

    Thread Starter marcnyc

    (@marcnyc)

    Of course. But they don’t offer support for code

    I understand, but I was referring to ask them the values of the max exec time and upload size for your site.

    Thread Starter marcnyc

    (@marcnyc)

    my phpinfo suggests I have it set to “600” as local value and “30” as master value

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘media_handle_upload() with 4 images under 4Mb total crashes server / timeout err’ is closed to new replies.