2012-06-07 2 views
0

이 문제는 당분간 당황 스럽습니다. 기본적으로 나는 제출할 때 동시에 여러 이미지를 서버에 업로드하고 각 이미지의 레코드를 MySQL 데이터베이스에 삽입하는 양식을 가지고 있습니다.PHP - 일부 이미지가 업로드되지 않습니다.

현재 최대 6 개의 이미지를 선택하여 서버에 업로드하고 레코드를 mysql 데이터베이스에 삽입 할 수 있습니다.

이미지를 7 개만 선택하면 6 개가 업로드됩니다. 8, 9 또는 10 이미지를 선택하면 동일합니다. 항상 업로드 한 총 수보다 적은 수입니다.

11 개의 이미지를 선택하고 양식을 제출해도 아무런 문제가 없으며 이미지가 업로드되지 않고 이미지 기록도 데이터베이스에 삽입되지 않습니다.

내가 이미지를 확인하고 모두가 허용 된 파일 형식, 크기, 치수 등 내 내가 왜 모든 업로드 않습니다 max_file_uploads 그래서 200

에 설정 한 사용하고 웹 서버에 대한 php.ini 파일하지만 하나의 인스턴스와 다른 인스턴스에서 내 이미지 중 하나는 어떤 것도 업로드하지 않습니까?

코드가 길어서 해당 항목 만 포함시켜 보겠습니다.

additem.php :

//check if the submit button has been clicked 
    if(isset($_POST['submit'])){ 

     //validate the title and description 
     $title = validate_title($_POST['title']); 
     $desc = validate_desc($_POST['desc']); 

     //Get other posted variables 
     $cat = $_POST['cat']; 
     $year = $_POST['year']; 

     if($title && $desc != false){ 


      //check if an image has been submitted 
      if((!empty($_FILES["files"])) && ($_FILES['files']['error'][0] == 0)){ 

       // Insert the post 
       insert_post_db($title, $desc, $year); 

       // Get id of last inserted post 
       $post_id = get_postid(); 

       // loop through each individual image 
       foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){ 

        // Get the image info 
        $temp_dir = $_FILES['files']['tmp_name'][$key]; // Temporary location of file 
        $image_type = $_FILES['files']['type'][$key]; // Image filetype 
        $image_size = $_FILES['files']['size'][$key]; // Image file size 
        $image_name = $_FILES['files']['name'][$key]; // Image file name 

        // Get image width and height 
        $image_dimensions = getimagesize($temp_dir); // returns an array of image info [0] = width, [1] = height 
        $image_width = $image_dimensions[0]; // Image width 
        $image_height = $image_dimensions[1]; // Image height 

        // Check to make sure there are no errors in ther file 
        if($_FILES['files']['error'][$key] === UPLOAD_ERR_OK){ 

         // Make sure each filename name is unique when it is uploaded 
         $random_name = rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(1000,9999); 

         // Set the path to where the full size image will be stored 
         $path = 'img/fullsize/'.$random_name . $_FILES['files']['name'][$key]; 

         // Set the path to where the thumb image will be stored 
         $thumb_path = 'img/thumb_/'.$random_name .$_FILES['files']['name'][$key]; 

         // Set the Maximum dimensions the images are allowed to be 
         $max_width = 4040; 
         $max_height = 4040; 

         // Set the Maximum file size allowed (5MB) 
         $max_size = 5242880; 

         // Set the file extensions that are allowed to be uploaded and store them in an array 
         $allowed = array('image/jpeg','image/png','image/gif'); 

         // Check to make sure the image that is being uploaded has a file extension that we permit 
         if(in_array($image_type,$allowed)){ 
          // Check to make sure the Image dimensions do not exceed the maximum dimensions allowed 
          if(($image_width < $max_width) && ($image_height < $max_height)){ 
           // Check to make sure the Image filesize does not exceed the maximum filesize allowed 
           if($image_size < $max_size){ 

            // Check the shape of the Image (square, standing rectangle, lying rectangle) and assign a value depening on which it is 
            $case = image_shape($image_width, $image_height); 

            // Create the new thumbnail dimensions 
            list($thumb_width, $thumb_height, $smallestside, $x, $y) = thumb_dimensions($case, $image_width, $image_height, $smallestside, $x, $y); 

            // Create the thumbnails 
            create_thumbnail($image_type, $image_height, $image_height, $temp_dir, $thumb_path, $thumb_width, $thumb_height, $smallestside, $x, $y); 

            // move large image from the temporary location to the permanent one 
            move_uploaded_file($temp_dir, $path); 

            // Get the new randomly generated filename and remove the directory name from it 
            $file_name = substr($path, 4); 

            // Insert a record of the image in the Database 
            insert_image_db($file_name, $cat, $post_id,$image_size, $image_width, $image_height); 

            // Tell user image was successfully uploaded 
            echo "<p>Image uploaded ok.</p>"; 

            // Forward to the review post page 
            header('Location: reviewpost.php'); 
           }else{ 
            echo $_FILES['files']['name'][$key] . ': unsupported file size.'; 
           } 
          }else{ 
           echo $_FILES['files']['name'][$key] . ': unsupported image dimensions.'; 
          } 
         }else{ 
          echo $_FILES['files']['name'][$key] . ': unsupported filetype.'; 
         } 
        }else{echo 'file error';} 
       } 



      }else{ 
       //display error message if user didnt select an image to upload 
       echo '<p>There was an error processing your submission. Please select an image to upload.</p>'; 
      } 
     }else{ 
      //display error message if the title or description are incorrect length 
      echo errormessage($title, $desc); 
     } 
    } 

기능 : 내가 놓친 경우가 많은 코드가 있다고

// Find out what shape the image is 
    function image_shape($image_width, $image_height){ 
     if($image_width == $image_height){$case=1;} // square image 
     if($image_width < $image_height){$case=2;} // standing rectangle 
     if($image_width > $image_height){$case=3;} // lying rectangle 
     return $case; 

    } 

    // Set the dimensions of the new thumbnail 
    function thumb_dimensions($case, $image_width, $image_height){ 
     switch($case){ 
      case 1: 
       $thumb_width = 200; 
       $thumb_height = 200; 
       $y = 0; 
       $x = 0; 
       $smallestside = $image_height; 
      break; 
      case 2: 
       $thumb_height = 200; 
       $ratio   = $thumb_height/$image_height; 
       $thumb_width = round($image_width * $ratio); 
       $x = 0; 
       $y = ($image_height - $image_width) /2; 
       $smallestside = $image_width; 
      break; 
      case 3: 
       $thumb_width = 200; 
       $ratio   = $thumb_width/$image_width; 
       $thumb_height = round($image_height * $ratio); 
       $x = ($image_width - $image_height) /2; 
       $y = 0; 
       $smallestside = $image_height; 
      break; 
     } 
     return array($thumb_width, $thumb_height, $smallestside, $x, $y); 
    } 

    // Create a thumbnail of the image 
    function create_thumbnail($image_type, $image_width, $image_height, $temp_dir, $thumb_path, $thumb_width, $thumb_height, $smallestside, $x, $y){ 
     switch($image_type){ 
      case 'image/jpeg'; 
       $thumbsize = 200; 
       $img =  imagecreatefromjpeg($temp_dir); 
       $thumb = imagecreatetruecolor($thumbsize, $thumbsize); 
          imagecopyresized($thumb, $img, 0, 0, $x, $y, $thumbsize, $thumbsize, $smallestside, $smallestside); 
          imagejpeg($thumb, $thumb_path,100); 


      break; 
      case 'image/png'; 
       $thumbsize = 200; 
       $img =  imagecreatefrompng($temp_dir); 
       $thumb = imagecreatetruecolor($thumbsize, $thumbsize); 
          imagecopyresized($thumb, $img, 0, 0, 0, 0, $thumbsize, $thumbsize, $smallestside, $smallestside); 
          imagepng($thumb, $thumb_path, 0); 

      break; 
      case 'image/gif'; 
       $thumbsize = 200; 
       $img =  imagecreatefromgif($temp_dir); 
       $thumb = imagecreatetruecolor($thumbsize, $thumbsize); 
          imagecopyresized($thumb, $img, 0, 0, 0, 0, $thumbsize, $thumbsize, $smallestside, $smallestside); 
          imagegif($thumb, $thumb_path, 100); 

      break; 
     } 

    } 
    function insert_post_db($title, $desc, $year){ 
     //test the connection 
     try{ 
      //connect to the database 
      $dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw"); 
     //if there is an error catch it here 
     } catch(PDOException $e) { 
      //display the error 
      echo $e->getMessage(); 
     } 

     $stmt = $dbh->prepare("INSERT INTO mjbox_posts(post_year,post_desc,post_title,post_active,post_date)VALUES(?,?,?,?,NOW())"); 
     $stmt->bindParam(1,$year); 
     $stmt->bindParam(2,$desc); 
     $stmt->bindParam(3,$title); 
     $stmt->bindValue(4,"0"); 
     $stmt->execute(); 


    } 
    function insert_image_db($file_name, $cat, $post_id, $image_size, $image_width, $image_height){ 

     //test the connection 
     try{ 
      //connect to the database 
      $dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw"); 
     //if there is an error catch it here 
     } catch(PDOException $e) { 
      //display the error 
      echo $e->getMessage(); 
     } 

     //insert images 
     $stmt = $dbh->prepare("INSERT INTO mjbox_images(img_file_name,cat_id,post_id,img_size,img_width,img_height,img_is_thumb) VALUES(?,?,?,?,?,?,?)"); 
     $stmt->bindParam(1,$file_name); 
     $stmt->bindParam(2,$cat); 
     $stmt->bindParam(3,$post_id); 
     $stmt->bindParam(4,$image_size); 
     $stmt->bindParam(5,$image_width); 
     $stmt->bindParam(6,$image_height); 
     $stmt->bindValue(7,"0"); 
     $stmt->execute(); 
    } 

미안 해요, 난에만 적용 할 수있는 물건을 포함, 알려 시도 어떤 것. 감사합니다.

답변

0

각 파일 사이에 사용 타이머를 추가해보십시오. 나는 그것을 시도하고 그것은 나를 위해 작동합니다. 나는 0.3 초를 넣었다.

foreach($file .. $k => $v) { 
    // your stuff above.. 
    usleep(300000); // Sleep between each file, 1000000 = 1 second 
} 
관련 문제