Error when Uploading image to website using PHP

Hello Mentor,

Greetings!!

I am a “newB” and I am working on image upload in my website. Where I created the form using html I’m using W3Schools . I’m working with their PHP File Upload example right now, and I ran into trouble.Here is the code:

<form action="ImageGallery.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename: </label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
   
?>

I can’t understand for some reason it won’t work. it came back with an error:

Notice: Undefined index: file in C:\xampp\htdocs\bubble\ImageGallery.php on line 67

Notice: Undefined index: file in C:\xampp\htdocs\bubble\ImageGallery.php on line 73
Upload:

Notice: Undefined index: file in C:\xampp\htdocs\bubble\ImageGallery.php on line 74
Type:

Notice: Undefined index: file in C:\xampp\htdocs\bubble\ImageGallery.php on line 75
Size: 0 kB

Notice: Undefined index: file in C:\xampp\htdocs\bubble\ImageGallery.php on line 76
Stored in:

I’m confused and if someone could take a look at it and tell me what is going wrong I would appreciate it. Thanks for the help

First off, those are not errors, they are notices that mean to alert you to poor coding practices. In this case, simply opening the page will generate those notices, because you haven’t submitted the form yet. Since the form hasn’t been submitted, the $_FILES array is not populated.

To help remove those notices, try this:

<form action="ImageGallery.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename: </label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
  if (isset($_FILES["file"]))
  {
    if ($_FILES["file"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }   
?>

Hello Mentor,

The above code worked properly thank you for that.

But now wen I have to store imaage into database its not getting stored with below code.
And wat datatype should I use to stor image to database. Should I use “BLOB” or simple "VARCHAR "

<?php
  if (isset($_FILES["file"]))
  {
    if ($_FILES["file"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
      echo "Upload: " .addslashes( $_FILES["file"]["name"]) . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
	  $image_size = getimagesize($_FILES['file']['tmp_name']);
      echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br>";;
	  echo $image = addslashes(file_get_contents($_FILES["file"]["tmp_name"]));
	  
	   $imgtype = $image['mime'];
	  
	  if($image_size==FALSE)
	  echo "That's not an image";
	  else
	  {
		 if(! $insert = mysql_query("INSERT INTO picture VALUES ('','Name','$image','detail')"))
		 echo "Problem uploadind Image";
		 else
		 {
			 $lastid  = mysql_insert_id();
			 echo "Image Uploaded.<p>Your Image:  </p><img src=getimage.php?id=$lastid>";
			 
			 
			 }
		  
		  } 
	  
    }
  }   
?>

You should be using a BLOB column type. Though myself and many others would argue against storing the image data in a table. Just write the image to a folder and store the path in the table (this route would use varchar for the column type).

Seconded, I usually create an image / uploads table that holds an ID (pk) and a path - you can then write a simple php page that you query with the ID and that returns the full path to the uploaded image (e.g. getimage.php?id=2141 would return yourwebsite.com/path-to-img-with-id-2141).

Just a suggestion, sorry if it’s a bit off-topic :wink:

thanks but it helped