2010-01-05 58 views
0

我看了網站找到我的問題的解決方案,但找不到答案。希望有人可以幫助我:PHP圖片上傳和調整大小錯誤

我有一個腳本,可以在不刷新頁面的情況下上傳和調整圖像大小。我在互聯網上找到它並對其進行調整以供我使用。腳本可以在下面找到。

我的問題:

首先upload.php程序的工作,但知道我得到錯誤數字9

我做了什麼來試圖解決它自己:

  1. 在本地機器和服務器上嘗試過,兩者都給出相同的錯誤編號9
  2. 刪除所有$ _POST並設置'static'vars,d idn't工作,並仍然得到了9號

有人可以幫我,TNX提前

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([email protected]($im,$dir.$file,100)){ 
     $error = 9; // This is the error i still get 
     }else{ 
     $error = 10;  
     }  
    }  
    if($dim[2] == 1){ 
     if([email protected]($im,$dir.$file)){ 
     $error = 11; 
     }else{ 
     $error = 10;  
     }  
    } 

    if($dim[2] == 3){ 
     if([email protected]($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> 

回答

2

嘗試刪除該行的@:

if([email protected]($im,$dir.$file,100)){ 

它抑制錯誤imagejpeg拋出。沒有它,你可能會看到你的問題。從以下行

+1

如果它原來是內存的問題(永遠是我的第一個賭注),請記住,一個3000×3000像素的JPG吃起來不是9 MB,但至少3 x 3000 x 3000 = 27 MB的內存。 – 2010-01-05 14:40:47

0

刪除@

if([email protected]($im,$dir.$file,100)){ 

@抑制錯誤。如果它被刪除並且display_errors處於打開狀態,則應該看到一個更具描述性的錯誤消息。

我的猜測是imagejpeg()由於權限不足而無法寫入文件。

0

媽,

我笨:(

至於建議我刪除了@,並用自己的眼睛看到我做了什麼錯。

錯誤是在我的目錄結構它是這樣的

| mainFolder | | _ |子文件夾| | 上傳。php | | imageFolder |

$dir = $_POST['uploadPath'];是一個文件夾,將文件移動到「mainfolder/subfolder/imageFolder」,但它需要是「imageFolder /」,因爲我使用「子文件夾」中的upload.php文件。

THX的所有幫助

Grtz

維姆