2017-02-22 44 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()