Jump to content

Php & Mysql: Undefined Index


tinfanide

Recommended Posts

<html><head><title>PHP & MySQL: Upload an image</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> <?phpmysql_connect("localhost","root","") or die(mysql_error());mysql_select_db("databaseimage") or die(mysql_error()); $file = $_FILES['image']['tmp_name'];if(!isset($file)){echo 'Please select an image.';} else {  $image = file_get_contents($_FILES['image']['tmp_name']);  echo $image_name = $_FILES['image']['name'];  }?></body></html>

Notice: Undefined index: image in C:\xampp\htdocs\_test\index.php on line 18

<html><head><title>PHP & MySQL: Upload an image</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><?phpmysql_connect("localhost","root","") or die(mysql_error());mysql_select_db("databaseimage") or die(mysql_error());$file = $_FILES['image']['tmp_name'];if(!isset($file)){echo 'Please select an image.';} else {  $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));  $image_name = addslashes($_FILES['image']['name']);  $image_size = getimagesize($_FILES['image']['tmp_name']);   if($image_size==FALSE){   echo "That's not an image.";   }   }?>

Notice: getimagesize() [function.getimagesize]: Read error! in C:\xampp\htdocs\_test\index.php on line 25 What are the PHP errors about?

Link to comment
Share on other sites

You should be testing to see if $_FILES['image'] exists first. Remove this line, because it's implying that there already is an index "image" in the files array.

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

And then put this instead:

if(!isset($_FILES['image'])) {

The problem with your getimagesize() function is that, for some reason it can't read the file. Maybe the file isn't an image or it doesn't exist.

Link to comment
Share on other sites

Yes, the first problem is solved.But for the problem with getimagesize(), if the file is not an image, then it should only echo That's not an image. But now it returnsNotice: getimagesize() [function.getimagesize]: Read error! in C:\xampp\htdocs\_test\index.php on line 24That's not an image.

Link to comment
Share on other sites

This is all I found from the PHP manual:

If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE.
There's nothing wrong in your code, but it isn't able to read the file. Maybe there are permission problems. Does this occur with different images?
Link to comment
Share on other sites

It does occur in other images.The reason I wondered was because the video tutorial I've been following never shows any error message on his browser (Chrome) (maybe that's the key) I don't know...But the fact is that it still works.I mean I still manage to upload the image info to MySQL database and show the image on the browser. Here is the complete code. Please have a look if ya like: index.php

<html><head><title>PHP & MySQL: Upload an image</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> <?phpmysql_connect("localhost","root","") or die(mysql_error());mysql_select_db("databaseimage") or die(mysql_error());if(!isset($_FILES['image'])){echo 'Please select an image.';} else {  $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));  $image_name = addslashes($_FILES['image']['name']);  $image_size = getimagesize($_FILES['image']['tmp_name']);   if($image_size==FALSE){   echo "That's not an image.";   } else {    if(!$insert = mysql_query("INSERT INTO store VALUES ('','$image_name','$image')")){	 echo "Problem uploading image.";	 } else {	  $lastid = mysql_insert_id();	  echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>";	  }    }   }?></body></html>

get.php

<?phpmysql_connect("localhost","root","") or die(mysql_error());mysql_select_db("databaseimage") or die(mysql_error());$id = addslashes($_REQUEST['id']);$image = mysql_query("SELECT * FROM store WHERE id=$id");$image = mysql_fetch_assoc($image);$image = $image['image'];header("Content-type: image/jpeg");echo $image;?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...