我有一個讓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>
你必須學會的第一步是要監控你的HTTP服務器錯誤日誌文件,如果你經歷的事,你沒有想到的。 _你不能在沒有監視日誌文件的情況下在web環境下編程php!_ – arkascha
檢查這一個:http://www.larryullman.com/books/php-for-the-web-visual-quickstart-guide-4th-edition/ errata/ – fernando
在您真實的活動項目中,請小心使用'$ _FILES ['the_file'] ['name']'。將其視爲用戶輸入並採取適當的安全措施。否則,不要使用用戶代理提供的文件名是一個好主意。 –