2013-07-23 137 views
0

有人能告訴我爲什麼我的腳本不會上傳圖片文件。另外,文件大小是否有一些限制?圖片不會上傳PHP

基本上它應該工作的方式是每次上傳圖片腳本都應該改變最大尺寸,問題是當我試圖上傳任何圖片時,if語句似乎失敗了,它只是跳到其他。

,第一部分是PHP:

if (isset($_FILES['image']['name'])) { 

$title = sanitizeString($_POST['title']); 
$title = nl2br($title); 
$saveto = str_replace(" ", "", $title); 
$saveto = str_replace("'", "", $saveto); 
$saveto = "blogImages/$saveto.jpg"; 
if (move_uploaded_file($_FILES['image']['tmp_name'], $saveto)) { 
    echo "You Have Succesfully Uploaded an Image!"; 
    $typeok = true; 

    switch($_FILES['image']['type']) 
     { 
      case "image/gif": $src = imagecreatefromgif($saveto); break; 
      case "image/jpeg": // Both regular and progressive jpegs 
      case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break; 
      case "image/png": $src = imagecreatefrompng($saveto); break; 
      default:   $typeok = FALSE; break; 
     } 

    if ($typeok) 
      { 
       list($w, $h) = getimagesize($saveto); 

       $max = 800; 
       $tw = $w; 
       $th = $h; 

       if ($w > $h && $max < $w) 
       { 
        $th = $max/$w * $h; 
        $tw = $max; 
       } 
       elseif ($h > $w && $max < $h) 
       { 
        $tw = $max/$h * $w; 
        $th = $max; 
       } 
       elseif ($max < $w) 
       { 
        $tw = $th = $max; 
       } 

       $tmp = imagecreatetruecolor($tw, $th); 
       imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h); 
       imageconvolution($tmp, array(array(-1, -1, -1), 
        array(-1, 16, -1), array(-1, -1, -1)), 8, 0); 
       imagejpeg($tmp, $saveto); 
       imagedestroy($tmp); 
       imagedestroy($src); 
       } 

} else { 
    echo "You have not uploaded an Image"; 
} 

這是HTML:

<body> 
<form id = 'blogPostInput' method='post' action = 'inputBlog.php'  enctype='multipart/form-data'> 
<h3> Enter or Edit a Blog </h3> 
<br/> 
Blog Post Title: 
<br/> 
<input type='text' value='' name='title' maxlength='64'/> 
<br/> 
Enter an Image or Video URL 
<br/> 
<input type='text' value='' name='video' maxlength = '128'/> 
<br/> 
<input type='file' name='image' maxlength='150' /> 
Enter the Blog Post's Text 
<br/> 
<textarea name='blogPostContent' cols='100' rows='5'></textarea> 
<br/> 
<input type='hidden' id='timeStamp' name='timeStamp' maxlength = '64'/> 
<br/> 
<input type='submit' value= 'Save Blog Post'/> 
</form> 

</body> 
+0

什麼是最大文件大小設置爲:請參閱http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size的詳細信息 –

+0

當你調試它時發生了什麼? – noj

+0

你真的打算使用相對路徑來寫你上傳的文件嗎?腳本用戶對這個目錄有權限嗎? –

回答

1

您應該驗證相對文件路徑被用來指向正確的預定位置,並且還驗證正在運行此腳本的用戶對該目錄具有適當的寫入權限。

+0

正如我所說,('+ 1')在這裏麥克,要走的路。 –