文件上传未定义索引错误 php

File Upload undefined index error php

我在代码中提到 "Error line" 该位置行显示此错误" 注意:未定义索引:第 33 行 C:\xampp\htdocs\Lab\register2.php 中的文件”。但在我的数据库中,数据增加意味着主键(id)。我需要在 PHP 中解决它,请帮助我。

<?php 

 if(isset($_POST['btn_upload']))
{

 $fullname=$_POST["fullname"];

 $file = rand(1000,100000)."-".$_FILES['file']['name']; //Error line
 $file_loc = $_FILES['file']['tmp_name']; //Error line


 $info = pathinfo($_FILES['file']['name']); //Error line
 $ext = $info['extension'];  //Error line
 $newname = $userid.".".$ext;  //Error line

 $target = 'uploads/'.$newname;
 move_uploaded_file( $_FILES['file']['tmp_name'], $target); //Error line






 require_once "config.php";
 $db=get_connection();
 $sql="INSERT INTO user(Path) VALUES('$newname')";
 mysql_query($sql); 
}

?>




 <!DOCTYPE html>
 <html>
   <title></title>

    <body>
        <center>
        <form action="#" method ="POST">

        Full Name : <input type="text" name="fullname"/><br/>
        Phone : <input type="number" name="phone"/><br/>
        Address :<textarea rows="4" cols="30" name="textin"></textarea><br>
        Profile Picture : <input type="file" name="file" /> <button type="submit" name="btn_upload">Upload Image</button><br/>


        <input type= "submit" name ="back" value="Back"/><br/>
        <input type= "submit" name ="next1" value="Next"/>


          </form>

      </center>
   </body>

  </html>

您的表单应该有 enctype="multipart/form-data" 作为表单元素。

所以你应该

<form action="#" method ="POST" enctype="multipart/form-data">

如果文件内容与表单一起提交,则文件输入应由适当的内容类型标识(例如,"application/octet-stream")

来源和学习更多关于Form Elements here

注:

从表单元素中删除 # 符号,因为它不是必需的。

建议:

不要将 w3schools 用于所有基础学习,请尝试使用 PHP The Right Way

enctype 属性指定表单数据在提交给 server.The 时应如何编码 enctype 属性只能在 method="post".

时使用
<body>
    <center>
    <form action="#" method ="POST" enctype="multipart/form-data">
     // Your Code
      </form>

  </center>
 </body>
<form action="#" method ="POST" enctype="multipart/form-data">
   //Your form Code
</form>

enctype="multipart/form-data" 这将允许您的表单提交文件(图片、pdf、office 文件等...)

补充说明

in php.ini(第 912 行)

file_uploads=On

Read This Tutorial