Anjan
Anjan

Reputation: 62

File Upload undefined index error php

I mentioned in Code that "Error line" that position line show this error" Notice: Undefined index: file in C:\xampp\htdocs\Lab\register2.php on line 33 ". But in my database data increased means primary key(id). And i need solve it in PHP Please help me .

<?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>

Upvotes: 1

Views: 1930

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

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

enctype="multipart/form-data" this will allow your form to submit file (image, pdf,office files, etc...)

Extra Note

in php.ini (in line 912)

file_uploads=On

Read This Tutorial

Upvotes: 0

Amanjot
Amanjot

Reputation: 2058

The enctype attribute specifies how the form-data should be encoded when submitting it to the server.The enctype attribute can be used only if method="post".

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

  </center>
 </body>

Upvotes: 0

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

You form should have enctype="multipart/form-data" as the form element.

So you should have

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

If the contents of a file are submitted with a form, the file input should be identified by the appropriate content type (e.g., "application/octet-stream")

Source and Learn more about Form Elements here

Note :

Remove the # symbol from the form element as it is not required.

Advice :

Do not use w3schools for all your basic learning, try using PHP The Right Way

Upvotes: 1

Related Questions