【问题标题】:PHP image upload and resize errorPHP图像上传和调整大小错误
【发布时间】:2010-01-05 14:24:09
【问题描述】:

我查看了该网站以找到解决问题的方法,但找不到答案。希望有人可以帮助我:

我有一个脚本可以在不刷新页面的情况下上传和调整图像大小。我在互联网上找到它并对其进行了调整以供我使用。该脚本可以在下面找到。

我的问题:

首先,upload.php 脚本有效,但知道我收到错误号 9。

我做了什么来尝试自己解决这个问题:

  1. 在本地机器和服务器上尝试过,都给出相同的错误号 9
  2. 删除了所有的 $_POST 并设置了“静态”变量,但没有用,但仍然是 9 号

有人可以帮我吗,tnx提前

Grtzzz

维姆

上传.php

<?php
/************************************************************            
 *-----------------------------------------------------------
 * Version  : 0.2
 * Author  : Wim Selles        
 * Date  : 2009-12-04
 * History  : Adjusted to use with AJAX upload
 *     Added check for allowed extension
 *     Added change filename to time 
 *     of uploading
 *-----------------------------------------------------------
 * Version  : 0.1
 * Author  : Eris        
 * Date  : 2005-11-19
 * History  : Initial version
 * Source  : http://www.phphulp.nl/php/scripts/9/464/
 *-----------------------------------------------------------
 ************************************************************/

if(!empty($_FILES['file'])){
 //---- filename generated by the server when uploading a file
 $tempfile     =  $_FILES['file']['tmp_name'];
 //---- directory with the final location 
 $dir      =  $_POST['uploadPath'];
 //---- Get the extension of the file    
 $ext      =  strtolower(substr(strrchr($_FILES['file']['name'], '.'), 1));
 $ext_image    =  array("gif", "jpg","jpeg","png");
 //---- new filename is time and extention
 $file      =  time().".".$ext;
 //---- resize / max height and width of image
 list($height,$width) = explode(',',$_POST['imageMess']);
 //---- The max filesize
 $limit_size    = $_POST['imageSize']*1024*1024;

 //---- error = 1 => file is not uploaded
 //---- error = 2 => No image file
 //---- error = 3 => File uploaded
 //---- error = 4 => Error during move upload file
 //---- error = 5 => File allready exsits
 //---- error = 6 => Error resizing jpg
 //---- error = 7 => Error resizing gif
 //---- error = 8 => Error resizing png
 //---- error = 9 => Imagecreate error for jpg
 //---- error = 10 => Resized (not error, but succes ;-))
 //---- error = 11 => Imagecreate error for gif 
 //---- error = 12 => Imagecreate error for png 
 //---- error = 13 => Filetype not allowed

 //----check if the file is realy uploaded
 if(filesize($tempfile)>=$limit_size){
  $error = 14;
 }elseif(!is_uploaded_file($tempfile)){
  $error = 1;
 }elseif(!in_array($ext, $ext_image)){
  //---- Check if file is allowed, if not, then show error
  $error = 13;
 }else{
  //---- get the dimensions of the file
  if(!$dim = getimagesize($tempfile)){
   $error = 2;
  }else{    
   //---- 0 = width 
   //---- 1 = height
   //---- 2 = type
   //---- we want to calculte if it is bigger then the maxsize if not keep it easy --> upload it
   if($dim[0] < $width && $dim[1] < $height){
    //----move upload file
    if(!file_exists($dir.$file)){
     if(@move_uploaded_file($tempfile,$dir.$file)){
      $error = 3;
     }else{
      $error = 4;    
     }
    }else{
     $error = 5;   
    }        
   }else{
    //---- we have to resize :(
    if($dim[0] > $dim[1]){
     $prop = $width / $dim[0];
     $dims[0] = $width;
     $dims[1] = round($dim[1] * $prop); 
    }else{
     $prop = $height / $dim[1];
     $dims[1] = $height;
     $dims[0] = round($dim[0] * $prop); 
    }
    //---- we know the new size
    if($dim[2] == 2){
     if(!$mimage = @imagecreatefromjpeg($tempfile)){
      $error = 6;
     }
    }
    //---- we know the new size
    if($dim[2] == 1){
     if(!$mimage = @imagecreatefromgif($tempfile)){
      $error = 7;
     }
    }
    //---- we know the new size
    if($dim[2] == 3){
     if(!$mimage = @imagecreatefrompng($tempfile)){
      $error = 8;
     }
    }
    $im = @imagecreatetruecolor($dims[0],$dims[1]);
    @imagecopyresampled($im, $mimage, 0, 0, 0, 0, $dims[0], $dims[1], $dim[0], $dim[1]);

    if(!file_exists($dir.$file)){
     if($dim[2] == 2){
      if(!@imagejpeg($im,$dir.$file,100)){
       $error = 9; // This is the error i still get
      }else{
       $error = 10;    
      }    
     }    
     if($dim[2] == 1){
      if(!@imagegif($im,$dir.$file)){
       $error = 11;
      }else{
       $error = 10;    
      }    
     }

     if($dim[2] == 3){
      if(!@imagepng($im,$dir.$file)){
       $error = 12;
      }else{
       $error = 10;      
      }    
       }
    }else{
     $error = 5;       
    }
    imagedestroy($im);
    imagedestroy($mimage);  
    //---- end resize
   }  
  }
 }
 ?>

 <script language="javascript" type="text/javascript">
     parent.stopUpload(<?php echo $error; ?>,'<?php echo "intranet/admin/uploadedImages/".$file;?>');
  //alert(<?php //echo $error2;?>);
    </script>

    <?php
}
?>

JS 文件:

$(document).ready(function(){
 //---- When the button is clicked to submit the file
 $('#submitfile').submit(startUpload);
});

function startUpload(){
 $('#f1_upload_process').show();
 return true;
}
stopUpload=function (response, filename){
 //---- The upload errors
 var uploadError = new Array;
 uploadError[1] =  'Bestand is niet geupload';       // file is not uploaded
 uploadError[2] = 'Dit is geen afbeeldingsbestand';     // No image file
 uploadError[4] = 'Error tijdens het verplaatsen van het bestand'; // Error during move upload file
 uploadError[5] = 'Bestand bestaat reeds';        // File allready exsits
 uploadError[6] = 'Error tijdens het resizen van een jpg bestand'; // Error resizing jpg
 uploadError[7] = 'Error tijdens het resizen van een gif bestand'; // Error resizing gif
 uploadError[8] = 'Error tijdens het resizen van een png bestand'; // Error resizing png
 uploadError[9] = 'Error tijdens het creeeren van een jpg bestand'; // Imagecreate error for jpg
 uploadError[11] = 'Error tijdens het creeeren van een gif bestand'; // Imagecreate error for gif
 uploadError[12] = 'Error tijdens het creeeren van een png bestand'; // Imagecreate error for png
 uploadError[13] = 'Bestandstype niet toegestaan';      // Filetype not allowed
 uploadError[14] = 'Het bestand is te groot';       // File to big
 //---- If fileupload went good, insert image into RTE
 if (response == 3 || response == 10){
  //alert(filename);
  $('#profilePhoto').attr("src",filename);
  $('#f1_upload_process').hide();
  $('#result').html('');
  $('#fileLocation').val('');
  return false;
 } else {
  $('#f1_upload_process').hide();
  $('#result').html(uploadError[response]);
  //alert('Response= ' + response + ' New filename= ' + filename);
 }
 return true;
}

形式:

<form action="intranet/admin/upload.php" method="post" enctype="multipart/form-data" target="upload_target" id="submitfile">

                <img id="profilePhoto" src="intranet/admin/uploadedImages/unknown.png" height="100px"/>

                <img id="f1_upload_process" src="include/css/images/ajax-loader3.gif" />

                <br />
    <br />

    <input name="file" type="file" id="fileLocation" />
    <input type="submit" name="submitBtn" value="Upload" />

    <br />
    <br />

    <p class="text">Toegestane extensies: *.jpg | *.gif | *.png</p>

    <br />

    <p id="result"></p>

   </form>

   <iframe id="upload_target" name="upload_target" style="width:0;height:0;border:0px solid #fff;"></iframe>

【问题讨论】:

    标签: php jquery file-upload


    【解决方案1】:

    尝试删除此行中的@:

     if(!@imagejpeg($im,$dir.$file,100)){
    

    它抑制了 imagejpeg 抛出的错误。没有它,您可能会发现问题所在。

    【讨论】:

    • 如果结果证明是内存问题(始终是我的第一个赌注),请记住 3000 x 3000 像素的 JPG 占用的不是 9 MB,而是至少 3 x 3000 x 3000 = 27 MB记忆。
    【解决方案2】:

    从以下行中删除 @

      if(!@imagejpeg($im,$dir.$file,100)){
    

    @ 抑制错误。如果它被删除并且display_errors 处于打开状态,您应该会看到更具描述性的错误消息。

    我的猜测是imagejpeg()由于权限不足而无法写入文件。

    【讨论】:

      【解决方案3】:

      该死的,

      我很傻:(

      按照建议,我删除了 @ 并亲眼看到我做错了什么。

      错误在我的目录结构中是这样的

      |主文件夹| |_|子文件夹| |上传.php ||图片文件夹|

      $dir = $_POST['uploadPath']; 是一个将文件移动到“mainfolder/subfolder/imageFolder”的文件夹,但它必须是“imageFolder/”,因为我在“子文件夹”中使用了 upload.php 文件。

      感谢您的帮助

      格子

      维姆

      【讨论】:

        猜你喜欢
        • 2011-01-10
        • 1970-01-01
        • 1970-01-01
        • 2013-09-19
        • 2011-10-24
        • 2020-06-13
        • 2020-04-12
        • 2011-04-16
        • 2014-03-14
        相关资源
        最近更新 更多