Notice: Undefined index: image in

65,500
echo $file = $_FILES['image']['tmp_name'];

should be

if(isset($_FILES['image'])){
    echo $_FILES['image']['tmp_name'];
}

This checks first if $_FILES['image'] is set. If not, this wil not be run. Therefore, you will not have an error that it is out of index.

Because you first have to submit the form before $_FILES['image'] will be set...

Also, the input tag is self closing, so your form will not be:

<form action="index.php" method="POST" enctype="multipart/form-data">
    File:
    <input type="file" name="image"> <input type="submit" value="Upload"> 
</form>

but:

<form action="index.php" method="POST" enctype="multipart/form-data">
    File:
    <input type="file" name="image" /> <input type="submit" value="Upload" /> 
</form>
Share:
65,500
user1203497
Author by

user1203497

Updated on October 29, 2020

Comments

  • user1203497
    user1203497 over 3 years

    Possible Duplicate:
    Undefined index: file

    Hi I'm learning right now how to upload images to database, but I'm getting this error/notice

    Notice: Undefined index: image in C:\xampp\htdocs\Pildibaas\index.php on line 19

    Here is my index.php whole code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Image upload</title>
    </head>
    
    <body>
        <form action="index.php" method="POST" enctype="multipart/form-data">
            File:
            <input type="file" name="image"> <input type="submit" value="Upload"> 
        </form>
    
    
    <?php
    mysql_connect ("localhost", "root", "") or die (mysql_error());
    mysql_select_db ("databaseimage") or die (mysql_error());
    
    
    ?>
    </body>
    
    </html>
    

    Line 19 (this line gives error) cut out from index.php:

    echo $file = $_FILES['image']['tmp_name'];
    

    From google found that i need to change premission of tmp folder, but it allready shoud have all premissions it needs.

    In tutorial he dont get this error

    thank you

    • Mathlight
      Mathlight over 11 years
      You have 2 times an document in the code... Is this realy what you have? or is copy space gone wrong???
    • Mathlight
      Mathlight over 11 years
      and now you have the echo line not in the code...
    • Martin Mbae
      Martin Mbae almost 5 years
      Just change your form to include enctype="multipart/form-data" like <form method="post" enctype="multipart/form-data">
  • user1203497
    user1203497 over 11 years
    Yes, thank you that fix error/notice. But souldnt it upload something into tmp folder? So if i would do something like echo $image = file_get_contents($_FILES['image']['tmp_name']) i would get echod out lot of rubish and symbols.
  • Mathlight
    Mathlight over 11 years
    @user1203497, afcourse you get a lot of rubisch. first youve got to move the file to somewhere: move_uploaded_file($_FILES['image']['tmp_name'], $newDirForImage); and then youve got to echo the url in an image tag: echo '<img src="'.$newDirForImage.$fileName.'" />'; Hope you understand what i mean...