我有一个让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']'。将其视为用户输入并采取适当的安全措施。否则,不要使用用户代理提供的文件名是一个好主意。 –