1

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

3
  • 1
    You have 2 times an document in the code... Is this realy what you have? or is copy space gone wrong???
    – Mathlight
    Oct 28, 2012 at 12:48
  • and now you have the echo line not in the code...
    – Mathlight
    Oct 28, 2012 at 12:54
  • Just change your form to include enctype="multipart/form-data" like <form method="post" enctype="multipart/form-data"> Jun 6, 2019 at 8:13

2 Answers 2

11
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>
2
  • 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. Oct 28, 2012 at 13:03
  • 1
    @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...
    – Mathlight
    Oct 28, 2012 at 13:08
0
echo $file = $_FILES['image']['tmp_name'];

should be

echo $_FILES['image']['tmp_name'];
 or 
if(!empty($_FILES) && isset($_FILES['image'])){
  echo $_FILES['image']['tmp_name'];
    }

you can also use

print_r($_FILES);

Not the answer you're looking for? Browse other questions tagged or ask your own question.