Jump to content

Trouble getting the image into folder and showing it

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Padawan

Padawan

    Newbie

  • Members
  • PipPip
  • 15 posts
Hello guys,

I'm working out a simple image upload script that will upload and show the image on the same page. It worked perfect until I decided to store the file into folder, instead of the database as a BLOB.

I need to store the image URL path into the database as a VARCHAR and that's the only thing that actually works here. I can see the path stored into the DB, but there is no image in the folder, nor it's shown on the page. Some guidance here will be really appreciated, thank you! :)

Here's the code for index.php and get.php which suppose to show the uploaded image. But I need the direct path to the image, with, for an example .jpg extension.

Code for index.php

<form action="index.php" method="POST" enctype="multipart/form-data">


	File: 

		<input type="file" name="image"> 

		<input type="submit" value="Upload">

</form>


<?php error_reporting(E_ALL ^ E_NOTICE);

	

	// connect to database

	

	mysql_connect ("localhost", "root", "") or die (mysql_error());

	mysql_select_db ("dbimage");

	

	// file properties 

	

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

	

	// checking if the file is submitted

	

	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']);

			$image_path = "/imgs/".basename($image_name); // File destination 

			

			if ($image_size == FALSE)

				echo "That's not an image";

				

			else 

			{	

				if(move_uploaded_file($image_name, $image_path))

				echo "File uploaded";

				

				if (!$insert = mysql_query ("INSERT INTO store VALUES ('', '$image_name', '', '$image_path')"))

					echo "Problem uploading image";

					

				else 

				{

					$lastid = mysql_insert_id ();

							echo "Image uploaded! <p>View image:</p> <img src=get.php?id=$lastid>";

				}	

			}	

								

		}

?>	

get.php code

<?php error_reporting(E_ALL ^ E_NOTICE);


	mysql_connect ("localhost", "root", "") or die (mysql_error());

	mysql_select_db ("dbimage");

	

	$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;

?>

Thank you!

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Hello, sorry for the spammer

So it's been a while since I worked with uploaded files, but here somes hints

$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); 
This line may cause problems, do not addslashes on paths, you will change the directory with a new slash

But your main problem (I think) is here
$image_path = "/imgs/".basename($image_name); // File destination  
I'm guessing you are on a shared hosting server, and I'm pretty sure you don't have access to the root path in write
so you will need to use relative path
$image_path = "./imgs/".basename($image_name); // File destination  
see the dot at the start of the string, yeah small error are the most frustrating

This is what I see for the write part

Now the the read part (get.php)
At the end you will echo $image; but $image only contain the path of image, not the image
I know you have an error here, but not sure how to solve it (I'll take a look tomorow if you still didn't find out)
but if my memory is correct, it will be something like that

insted of echo $image;


$data = file_get_contents($image);

echo $data;


Be sure you have the 2 php files in the same folder.

#3
Padawan

Padawan

    Newbie

  • Members
  • PipPip
  • 15 posts
Thank you, Vaielab.

I think the problem might be in the conditional statement. I also tried removing the SQL query, to test if the image is inserted into a folder, but still nothing. The destination folder and index.php are on localhost, in a same folder that has the name of the 'project'.

I'm stuck. :S

#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Well you should go step by step.
Now I see, you are trying to test 2 files at once, and both have bugs
Start by testing the upload, and see if you are able to upload the file

You should change the error_reporting for error_reporting(-1);
And be sure you have display_errors to on in your php configs (or htaccess) do a phpinfo(); to see the value

#5
Padawan

Padawan

    Newbie

  • Members
  • PipPip
  • 15 posts
I fixed the code so it can upload the image to a folder, and store the path into the database, so it works now.

But at this point, how do I actually fetch and show the actual image?

Here's the code of the uploadimage.php script.

<?php error_reporting(E_ALL ^ E_NOTICE);


	include ('includes/db.php');

	

	// preparing the variables 

	

	$name = $_FILES['image']['name'];

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

	$type = $_FILES['image']['type'];

	$size = $_FILES['image']['size'];

	

	$path = 'uploads/' . md5( rand (0, 1000) . rand (0, 1000) . rand (0, 1000) . rand (0, 1000) ) .'.jpg';

	

	// echo the date

	

	echo '

		'. $name .' <br />

		'. $temp .' <br />

		'. $type .' <br />

		'. $size .' <br />

		'. $rand. ' <br />

	';


	// uploading the files

	

	if (move_uploaded_file ($temp, $path)) {

		$insert = mysql_query ("INSERT INTO store VALUES ('', '.$name.', '', '.$path.')");

		} else "error uploading query to database";

		

	

	// back link

	

	echo '<a href="index.php">Go back</a>';

?>


#6
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Something like this
(Took the code from http://www.configure...splay_image.php )
$imagepath="phpimages/dog.jpg";

$image=imagecreatefromjpeg($imagepath);

header('Content-Type: image/jpeg');

imagejpeg($image);

You will have to fetch $imagepath from the database, and than display it.
But this code will only work for jpg (and jpg variant)
If you wish to work with png or gif you will have to use other methods: imagecreatefrompng, imagecreatefromgif, imagepng, imagegif

#7
Padawan

Padawan

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks again Vaielab. But I'm still doing something wrong, because the image is shown like this:

Posted Image

Here's the code:

get.php

<?php


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


$imagepath= mysql_query ("SELECT * FROM store WHERE id=$id && image_url=$image_url");

$image=imagecreatefromjpeg($imagepath);

header('Content-Type: image/jpeg');

imagejpeg($image);


echo ($image);


?>

The lines in index.php

             if (!$lastid = mysql_insert_id ()) {

				echo "Error showing image";

            } else { echo "Image uploaded! <p>View image:</p> <img src=get.php?id=$lastid>"; 

			}

What should be changed here to make it work?

Thanks.

Edited by Padawan, 14 December 2011 - 05:41 AM.
Added some code





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users