6 Replies - 1979 Views - Last Post: 05 January 2012 - 04:20 AM Rate Topic: -----

#1 nkate16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-January 12

I can't display my image when i tried to upload it in my localhost

Posted 04 January 2012 - 10:40 AM

Here's my code for upload.php


<form action="upload.php" method="post" enctype="multipart/form-data">

	FILE:
	<input type="file" name="image" /> <input type="submit" value="Upload" />
</form>
<?php
//connect to database
include 'connection_db.php';
//file properties

 $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.";
	
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>";
	
	}

}
 
}	


	
	

?>






And here's my code for getting the image.
get.php

<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('guest_book') or die(mysql_error());

$id = stripslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM store WHERE id = $id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpg");

echo $image;




?>




It always display a problem uploading and when i tried another pictures, it works but nothing image is displayed and when i check my table in my localhost the id there is 0. What is the meaning of this? Thank You :) Hope you can help.

This post has been edited by Atli: 04 January 2012 - 11:08 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: I can't display my image when i tried to upload it in my localhost

#2 Duckington   User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: I can't display my image when i tried to upload it in my localhost

Posted 04 January 2012 - 11:24 AM

You are just trying to store the string of the file's name, you are not actually storing the image anywhere.

Unless you are meaning to store the full path to the image from your local machine, in which case the way you are doing it would only get the filename.jpg, not the full/path/to/the/filename.jpg

Also, you are doing your INSERT wrong. You have specified the values to insert, but not the fields in which to insert them:

INSERT INTO `table` (field, field, field) VALUES ('value', 'value', 'value');



Additionally, make sure your `id` field is set to AUTO_INCREMENT if it's still entering "0"
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: I can't display my image when i tried to upload it in my localhost

Posted 04 January 2012 - 11:26 AM

How, exactly, is your table set up?

Looking at that INSERT statement, I would guess that the reason why it is failing is because you are using an AUTO_INCREMENT PK field, and an empty string is not a valid value for it. - If you have an AI key in your table, you should leave that column out of the query.

And since we're on that topic, you should always specify the columns you are inserting into. It's not strictly neccessary but it is a very good habit to get into. It allows you to alter the table structure later without having to rewrite every single query that uses it.

/* Instead of */
INSERT INTO `tbl` VALUES('', 'image name', 'image data...');

/* Do something like this */
INSERT INTO `tbl`(`name`, `data`) VALUES ('image name' , 'image data...');




Also, on a different topic, you should ALWAYS use the mysql_real_escape_string function on user input before putting it into a mysql_query call! The addslashes function is NOT ENOUGH. - This is one of the most basic security measure you should get used to doing right away. (Either that or move on to MySQLi and/or PDO.)


One more thing. On line 35 in the first code you fail to provide the image ID for the image src attribute. The second code expects it. (In fact, it falls flat on it's face without it. Validate you user input before using it!)
Was This Post Helpful? 1
  • +
  • -

#4 nkate16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-January 12

Re: I can't display my image when i tried to upload it in my localhost

Posted 05 January 2012 - 02:09 AM

but still I can't display my image in my upload.php page. I want to display the image after I uploaded it. Thank You for :)
Was This Post Helpful? 0
  • +
  • -

#5 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: I can't display my image when i tried to upload it in my localhost

Posted 05 January 2012 - 03:39 AM

Show us the updated code.
Was This Post Helpful? 0
  • +
  • -

#6 nkate16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-January 12

Re: I can't display my image when i tried to upload it in my localhost

Posted 05 January 2012 - 03:58 AM

this is my get.php code



<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('guest_book') or die(mysql_error());

$this_Stud_ID =$_REQUEST['id'];

$image = mysql_query("SELECT * FROM store WHERE id = $this_Stud_ID");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpg");

echo $image;




?>

This post has been edited by Atli: 05 January 2012 - 04:13 AM
Reason for edit:: Use [code] tags when posting code!

Was This Post Helpful? 0
  • +
  • -

#7 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: I can't display my image when i tried to upload it in my localhost

Posted 05 January 2012 - 04:20 AM

Ok. This is basically the exact same code you posted before, except without the scripslahshes call. (Which has absolutely no effect on numbers...)

First, consider what would happen if no $_REQUEST['id'] value is passed. Follow the logic of that script, assuming it has no value.

My point here is that you need to make sure you actually get a value before you try to execute a SQL query based on that value. You also need to escape the value, or type-cast it, before using it to avoid SQL Injection.


Second, what about the code that calls this script? Have you made sure the ID of the image is actually pasted to the get.php code?


P.S.
Remember to use [code] tags when posting code!

Posted Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1