PHP图像上传和调整大小错误

时间:2010-01-05 14:24:10

标签: php jquery file-upload

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

我有一个脚本可以在没有pagerefresh的情况下上传和调整图像大小。我在互联网上找到它并调整它以供我使用。该脚本可以在下面找到。

我的问题:

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

我自己尝试解决这个问题:

  1. 在本地机器和服务器上尝试使用相同的错误编号9
  2. 删除了所有$ _POST并设置了'静态'变量,没有用,仍然有9号
  3. 有人可以提前帮助我吗

    Grtzzz

    维姆

    upload.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>
    

3 个答案:

答案 0 :(得分:2)

尝试删除此行中的@:

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

它抑制了imagejpeg抛出的错误。没有它,你可能会看到你的问题。

答案 1 :(得分:0)

从以下行中删除@

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

@可以抑制错误。如果它已被删除且display_errors已启用,您应该会看到更具描述性的错误消息。

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

答案 2 :(得分:0)

该死,

我很蠢:(

如我所知,我删除了@并亲眼看到了我做错了什么。

错误发生在我的目录结构中,就像这样

| mainFolder |  | _ |子文件夹|      | upload.php的      | | imageFolder |

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

感谢您的所有帮助

Grtz

维姆