1

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>
2
  • Try adding enctype="multipart/form-data" to your form element. stackoverflow.com/questions/4526273/… Commented Jun 30, 2015 at 4:09
  • Did you just updated your question instead of asking here about the previous comment? If the form doesn't submit the file the index isn't going to exist. or I guess you could do an empty/isset check seems kind of pointless though when you could just resolve the issue... you also could turn off error reporting or suppress the error (routes I wouldn't take). Commented Jun 30, 2015 at 4:14

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

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

1 Comment

There are so many other tutorials out there. Also the enctype has already been mentioned in the other 2 answers and a comment..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.