uploads - media_handle_upload() with 1 image under 1Mb chokes my server and returns timeout error

I'm struggling with 500 Timeout Errors when uploading images. Initially I was trying to upload 4 images at a time.

I'm struggling with 500 Timeout Errors when uploading images. Initially I was trying to upload 4 images at a time. Then I changed my code to upload 1 image at one time. If I strln() the $_SESSION I get 22872 bytes plus an image ofd less than 1Mb but the server still times out.

I've tried placing ini_set('max_execution_time', 0); at the top of my code right after session_start(). I also tried increasing max_execution_time and max_input_time to 50000 and max_input_vars to 3000 while post_max_size is set to 64Mb and memory_limit and upload_max_filesize are set to 512Mb in the Options of my cPanel's PHP Selector tab (which I believe changes the values in php.ini).

I wrote a function that performs all the checks and then uses media_handle_upload() to upload the files. The function is called like thi:

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

When I look at the WP uploads folder I see the image gets uploaded but the browser returns Request Timeout This request takes too long to process, it is timed out by the server. If it should not be timed out, please contact administrator of this web site to increase 'Connection Timeout'.

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?

To me it appears like the upload is happening but then the server times out on whatever else media_handle_upload() does but I'm new to the world of WP functions so I'm at a total loss

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742341517a4425719.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信