2012-06-29 66 views
1

我不斷收到無效的文件錯誤。任何人都可以看到這個腳本有什麼問題。我從W3學校得到它,文件夾「圖片/ 2012/Blackhall主/」確實存在PHP上傳問題與代碼

<?php 

    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg") 
    || ($_FILES["file"]["type"] == "image/png") 
    || ($_FILES["file"]["type"] == "image/jpg") 
    || ($_FILES["file"]["type"] == "image/pjpeg")) 
    && ($_FILES["file"]["size"] < 20000)) 
     { 
     if ($_FILES["file"]["error"] > 0) 
     { 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
     } 
     else 
     { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 

     if (file_exists("pics/2012/Blackhall Primary/" . $_FILES["file"]["name"])) 
      { 
      echo $_FILES["file"]["name"] . " already exists. "; 
      } 
     else 
      { 
      move_uploaded_file($_FILES["file"]["tmp_name"], 
      "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]); 
      echo "Stored in: " . "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]; 
      } 
     } 
     } 
    else 
     { 
     echo "Invalid file"; 
     } 
    ?> 
+1

1.你確定你沒有超過文件大小? 2.你有上傳文件夾的寫入權限嗎? – BBog

+0

'var_dump($ _ FILES)' - 你究竟得到了什麼? – deceze

+2

W3Schools在各個地方都是錯誤的... http://w3fools.com/ 9因爲你提到你從那裏得到了上面的腳本... – verisimilitude

回答

1

包括更多的錯誤檢查就像這樣:

<?php 

echo process_files(); 

function process_files() 
{ 
    $allowed_file_types=array('image/gif','image/jpeg','image/png','image/jpg','image/pjpeg'); 

    if(0==sizeof($_FILES)) return 'no files uploaded'; 

    if(!stristr($_FILES['file']['type'], 'image')) return 'file is not an image'; 

    if(!in_array($_FILES['file']['type'], $allowed_file_types)) return 'image type '.$_FILES['file']['type'].' is not allowed'; 

    if($_FILES['file']['size'] < 20000) return 'file size too large. Max:20000, File:'.$_FILES['file']['size']; 

    // rest of your code here! 
} 

?>