2013-04-29 62 views
1

我的表單很簡單,我認爲上傳php很簡單,但是當我測試它的結果是不尋常的。我可以上傳任何文件和任何大小,它會工作。我以爲我寫了它來限制某些文件和大小......我哪裏錯了?上傳器文件上傳任何東西

形式:

<form enctype="multipart/form-data" action="upload_file.php" method="POST"> 
Please choose a file: <input name="uploaded" type="file" /><br /> 
<input type="submit" value="Upload" /> 
</form> 

upload_file.php:

$target = "uploads/"; 
    $target = $target . basename($_FILES['uploaded']['name']) ; 
    $ok = 1; 
    $uploaded = $_POST['uploaded']; 
//This is our size condition 
    if ($uploaded_size > 3000){ 
     echo "Your file is too large.<br>"; 
     $ok=0; 
    } 

//This is our limit file type condition 
    if ($uploaded_type == "text/php"){ 
     echo "No PHP files are allowed for upload.<br>"; 
     $ok = 0; 
    } 

//Here we check that $ok was not set to 0 by an error 
    if ($ok == 0){ 
     Echo "Sorry your file was not uploaded"; 
    } 

//If everything is ok we try to upload it 
    else{ 
     if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ 
      echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded"; 
     } 
     else{ 
      echo "Sorry, there was a problem uploading your file."; 
     } 
    } 
+1

你在哪裏$ uploaded_type和$ uploaded_size var? – Yordi 2013-04-29 20:16:21

+1

這裏是一個非常簡單明確的文章http://www.w3schools.com/php/php_file_upload.asp – Vector 2013-04-29 20:18:55

+0

@Vector:哦不,不建議w3schools.com作爲參考。它充滿了錯誤,請參閱http://w3fools.com – 2013-04-29 20:19:25

回答

3

你的代碼是完全錯誤的。無處你定義$uploaded_size$uploaded_type,等等......所以代碼歸結爲:

if ($uploaded_size > 3000 { 

相當於

if (0 > 3000) { // undefined variables are typecast to 0 

其計算結果爲假,所以$ok停留1並不會觸發錯誤。

強烈建議您閱讀PHP手冊頁上處理文件上傳:http://php.net/manual/en/features.file-upload.php

+0

我認爲$ uploaded_size&$ uploaded_type是一個預先確定的php命令。 Errr,...我會再讀一遍。 – 2013-04-29 20:19:38

+0

在我看來,它似乎非常含糊或高於我的exp水平。如果資源都是先進的,我怎麼能學會它? ::沮喪:: – 2013-04-29 20:45:52

1

你需要使用像

 if ($_FILES["file"]["size"] > 3000) ... 

或定義$ uploaded_size = $ _FILES [ 「文件」] [ 「大小」]在檢查之前。此外,類似你需要使用$ _FILES [ 「文件」] [ 「型」]

 $uploaded_size = $_FILES["file"]["size"]; 
    $uploaded_type = $_FILES["file"]["type"]; 
    ... 
+0

謝謝你清楚如何使它成爲一個變量。 – 2013-04-29 20:24:06

+0

「file」是我上面的示例中的輸入名稱=「上傳」,正確嗎? 所以它會讀$ uploaded_type = $ _FILES [「uploaded」] [「size」] – 2013-04-29 20:40:17

+0

$ uploaded_type = $ _FILES [「uploaded」] [「type」];對。 – Ekim 2013-04-29 20:45:25

0

試試這個:

$target = "uploads/"; 
$target = $target . basename($_FILES['uploaded']['name']) ; 
$ok = 1; 
$uploaded = $_POST['uploaded']; 
//This is our size condition 
if ($uploaded_size > 3000){ 
    echo "Your file is too large.<br>"; 
    $ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type == "text/php"){ 
    echo "No PHP files are allowed for upload.<br>"; 
    $ok = 0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok == 0){ 
    Echo "Sorry your file was not uploaded"; 
    die(); 
} 

//If everything is ok we try to upload it 
else{ 
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ 
     echo "The file ". basename($_FILES['uploadedfile']['name']). " has been  uploaded"; 
    } 
    else{ 
     echo "Sorry, there was a problem uploading your file."; 
     die(); 
    } 
} 

添加die()函數告訴代碼停止。另外,您的$ uploaded_type和$ uploaded_size var在哪裏?

+0

謝謝,我會補充一點。 – 2013-04-29 20:22:16