2017-02-22 109 views
1

我有一個讓PHP上傳文件的問題。這幾天我一直在摔跤。我相信解決方案很簡單,但我是一個新的編碼器。無法用PHP上傳文件。頁面只是刷新

我從一本書中複製練習(PHP for the Web 4th edition)。當我嘗試用這個腳本上傳任何東西時,什麼都不會發生。該頁面只是刷新。沒有錯誤打印或任何東西。

我在Windows 10上使用WAMP。下面是代碼。有什麼東西會跳出來嗎?

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Upload A File</title> 
    </head> 
    <body> 
<?php // Script 11.4 - upload_file.php 
// address error reporting 

error_reporting(E_ALL & ~E_NOTICE); 

// Check if the form was submitted 

if ($_SERVER['REQEST_METHOD'] == 'POST') { 

    // Move file to final destination 

    if (move_uploaded_file ($_FILES['the_file']['tmp_name'], "../uploads/{$_FILES['the_file']['name']}")) { 

     echo '<p>Your file has been uploaded.</p>'; 

    } else { // Problem! 

     echo '<p style="color: red;"> Your file could not be uploaded because: '; 

     // Print an error message if file relocation didn't work 

     switch ($_FILES['the_file']['error']) { 
      case 1: 
       echo 'The file exceed the upload_max_filesize setting in php.ini'; 
       break; 
      case 2: 
       echo 'The file exceed the MAX_FILE_SIZE setting in the HTML form'; 
       break; 
      case 3: 
       echo 'The file was only partially uploaded'; 
       break; 
      case 4: 
       echo 'No file was uploaded'; 
       break; 
      case 6: 
       echo 'The temporary folder does not exist.'; 
       break; 
      default: 
       echo 'Something unforseen happened.........'; 
       break; 
     } 

     // Complete the error message and close both conditionals 

     echo '.</p>'; // Complete the end of paragraph 

    } // End of move_uploaded_file() IF 

} // End of submission IF 

?> 

     <form action="upload_file.php" enctype="multipart/form-data" method="post"> 
      <p>Upload a file using this form:</p> 
      <input type="hidden" name="MAX_FILE_SIZE" value="300,000"> 
      <p><input type="file" name="the_file"></p> 
      <p><input type="submit" name="submit" value="Upload This File"></p> 
     </form> 
    </body> 
</html> 
+0

你必須學會​​的第一步是要監控你的HTTP服務器錯誤日誌文件,如果你經歷的事,你沒有想到的。 _你不能在沒有監視日誌文件的情況下在web環境下編程php!_ – arkascha

+0

檢查這一個:http://www.larryullman.com/books/php-for-the-web-visual-quickstart-guide-4th-edition/ errata/ – fernando

+0

在您真實的活動項目中,請小心使用'$ _FILES ['the_file'] ['name']'。將其視爲用戶輸入並採取適當的安全措施。否則,不要使用用戶代理提供的文件名是一個好主意。 –

回答

0

你有在這一行一個錯字:

if ($_SERVER['REQEST_METHOD'] == 'POST') { 

應該

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

邊注:不要壓抑通知。您應始終使用error_reporting(E_ALL)或至少設置error_reporting(E_ALL & ~E_STRICT)設置,它將幫助您學習如檢查是否設置變量或數組索引的良好實踐。它會要求你寫一些額外的樣板代碼,但以後會爲你節省很多痛苦。在這種情況下,你會馬上發現Undefined index "REQEST_METHOD" in line 7

+0

好的。我剛剛重新格式化了代碼,錯過了這一點。 –

+0

是的,糾正我的錯字修復了這個問題。最初我得到了第7行的通知。我只是覺得劇本太挑剔了,因爲我還沒有發送任何內容。我需要注意細節。 –

0

什麼跳到我身上?

)那麼,Zasada先生是正確的,$_SERVER['REQUEST_METHOD']有一個拼寫錯誤(信用在信用到期)。另外,缺少<html>標記(儘管這不會影響PHP)。

)在您的最終項目中,使用用戶代理提供的文件名$_FILES['the_file']['name']}在不採取安全防範措施(過濾器/驗證)的情況下是不明智的。有些人會完全避免使用用戶提供的文件名。

)文件移動語句中缺少is_uploaded_file($_FILES['the_file']['tmp_name'])。有人可能會說move_uploaded_file()就夠了,但我說快速複查不會受傷。 :-)

if(is_uploaded_file($_FILES['the_file']['tmp_name']) && 
    move_uploaded_file ($_FILES['the_file']['tmp_name'], "../uploads/{$_FILES['the_file']['name']}")){} 

PHP Manual: is_uploaded_file()

PHP Manual: move_uploaded_file()