Notice: Undefined index: image in
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>
user1203497
Updated on October 29, 2020Comments
-
user1203497 over 3 years
Possible Duplicate:
Undefined index: fileHi 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 over 11 yearsYou have 2 times an document in the code... Is this realy what you have? or is copy space gone wrong???
-
Mathlight over 11 yearsand now you have the echo line not in the code...
-
Martin Mbae almost 5 yearsJust change your form to include enctype="multipart/form-data" like <form method="post" enctype="multipart/form-data">
-
-
user1203497 over 11 yearsYes, 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 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...