duanliaoyu8419
2019-01-26 19:16
浏览 62
已采纳

在用于上载文件的代码中找不到文件索引

I have copied this code from w3school website in my edditor (link: https://www.w3schools.com/php/php_file_upload.asp) and this error is echoed:

Undefined index: fileToUpload

I can't find any syntax or semantic problem in it and in php.ini file file_uploads directive is set to on.

<?php
echo <<<htmlcode
<!DOCTYPE html>
<head>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
htmlcode;
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

if(isset($_POST["submit"])) {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

图片转代码服务由CSDN问答提供 功能建议

我已经从我的edditor中的w3school网站复制了这段代码(链接: https://www.w3schools.com/php/php_file_upload.asp )并且这个错误得到了回应:

未定义索引:fileToUpload

我在其中找不到任何语法或语义问题,并且在php.ini文件中将file_uploads指令设置为on。 \ n

 &lt;?php 
echo&lt;&lt;&lt; htmlcode 
&lt;!DOCTYPE html&gt; 
&lt; head&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; form action  =“upload.php”method =“post”enctype =“multipart / form-data”&gt; 
选择要上传的图像:
&lt; input type =“file”name =“fileToUpload”&gt; 
&lt; 输入类型=“提交”值=“上传图片”name =“submit”&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
htmlcode; 
 $ target_dir =“uploads /”;  
 $ target_file = $ target_dir。  basename($ _ FILES [“fileToUpload”] [“name”]); 
 
if(isset($ _ POST [“submit”])){
 if(move_uploaded_file($ _ FILES [“fileToUpload”] [“tmp_name”  ],$ target_file)){
 echo“文件”。 基本名($ _ FILES [ “fileToUpload”] [ “名称”])。  “已上传。”; 
}其他{
 echo“抱歉,上传文件时出错。”; 
} 
} 
?&gt; 
   \  n 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duan6301 2019-01-26 19:49
    已采纳

    The problem is

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    

    When you run the page this block will run too. And when you didn't click submit button page read fileToUpload and see there is nothing named fileToUpload so you get the error.

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    
    if(isset($_POST["submit"])) {
       if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
          echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been 
    uploaded.";
       } else {
        echo "Sorry, there was an error uploading your file.";
       }
     }
    

    Add this lines in to if condition

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    

    like

    if(isset($_POST["submit"])) {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
     {
        echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been 
     uploaded.";
      } else {
        echo "Sorry, there was an error uploading your file.";
    }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dte49889 2019-01-26 19:46

    Put your variables and other code inside the if statement brackets the code is trying to run before you have even uploaded your image so the isset will make sure you uploaded the image first before running any php code.

    Also in you html the action="yourfilename.php" make sure its the name of your file so if itsupload.phpmake sure the action isupload.php`

    if(isset($_POST["submit"])) {
          $target_dir = "Uploads/";
          $target_file = $target_dir . basename ($_FILES["fileToUpload"]["name"]);
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                echo "The file ". basename ($_FILE["fileToUpload"]["name"]). " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
        ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题