problem with displaying image

Hi, i’ve been working with my website for quite some time now.

Its a simple upload button which uploads your selected image to a MYSQL server + a folder on my computer (the website is not live!!)
Now the problem is that I can’t echo out the image or display it in anyway.

Im gonna show my code below.
Just some quick info-
It works to upload the picture to the server AND the folder called ‘imagess’ on my computer but displaying the image wont work. Please help!

Server database name is: server_upload
table name: storeimg

I will show the code below:

this is the file called indexform.php-

[php]

Upload an image File: <?php //error_reporting(E_ALL ^ E_NOTICE); //connecting phase mysql_connect("localhost","roott","hej") or die(mysql_error()); mysql_select_db("server_upload") 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."; else { if ($insert = mysql_query("INSERT INTO storeimg VALUES ('','$image_name','$image')")) echo "Success uploading.

Your image:

"; else { $lastid = mysql_insert_id(); echo "Image uploaded.

Your image:

"; } } } //blobtel if (isset($_FILES['image'])) { $errors = array(); $allowed_ext = array('jpg', 'jpeg', 'png', 'gif'); $file_name = $_FILES['image']['name']; $file_ext = strtolower(end(explode('.', $file_name))); $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; if ($file_size > 2097152) { $errors[] = 'File size must be under 2mb'; } if (empty($errors)) { if (move_uploaded_file($file_tmp, 'imagess/'.$file_name)) { echo ''; } } else { foreach ($errors as $error) { echo $error, '
'; } } } ?>

[/php]


And this is the file called get2.php:

please note that i’ve hidden alot of my previous attempts with // but I want them to stay there for now.
[php]

<?php //connect to database. Username and password need to be changed $connection=mysql_connect("localhost", "roott", "hej"); //Select database, database_name needs to be changed mysql_select_db("server_upload"); //get decoded image data from database $result=mysql_query("SELECT * FROM storeimg WHERE id='".$_GET['id']."'"); //fetch data from database $image=mysql_fetch_array($result); $encoded=$image['image']; //note: "$data['data']" is the row "data" in the table we made. //The image ID would be "$data['img']" for example //close connection mysql_close($connection); //decode and echo the image data echo base64_decode($encoded); //mysql_connect("localhost","roott","hej") or die(mysql_error()); //mysql_select_db("server_upload") or die(mysql_error()); //$query = "SELECT MAX(id) FROM storeimg"; //$result = mysql_query($query); //$row = mysql_affected_rows($result); //echo $row[0]; //$result = mysql_query("SELECT MAX(id) FROM storeimg"); //$row = mysql_fetch_row($result); //$image = $image['image']; //header("Content-type: image/jpeg"); //echo $image; //$image = mysql_real_escape_string($_REQUEST[id]); //$rs = mysql_query("select * from storeimg where lastid=\"". addslashes($image)."jpg\""); //$row = mysql_fetch_assoc($rs); //$imagebytes = $row[imgdata]; //header("Content-type: image/jpeg"); //print $imagebytes; ?>[/php]

Looking forward to your help!
And the problem was that I cant seem to echo out / display the image on the website.

Im new to this website and I cant seem to find the edit button but i’ve provided you with SS to show you how it looks.

This SS was taken after i’ve uploaded an image (please note that when i upload an image it shows up in MYSQL and the map called ‘imagess’ but not on screen)

hello Couwae, i think id is define as integer in database table so please remove single qoutes //remove this line $result=mysql_query("SELECT * FROM storeimg WHERE id='".$_GET['id']."'"); //use this line $result=mysql_query("SELECT * FROM storeimg WHERE id=".$_GET['id']);

i hope this will helpful for you…
Reply your feedback
SR

Hi Sarthak!
I tried that but it still shows up with an empty image like I showed in the screenshot, got any other tips on how to do it? I was thinking of doing something like echo:ing out the last picture uploaded to the server. But Im unsure of how!

:slight_smile:
| Image | http://i.imgur.com/UztTL.png | Image |

Well, usually you use a IMG tag… to display the pix.
Also, normally, you do not store a picture inside a database, you store the location and
the name in the database and store the picture in a folder. That way the pictures are
viewable using the IMG tag… Hope that helps…

@ErnieAlex

Yeah, I guess that’s what I’ve done. Im not entirly sure. I’ve got a folder where the picture gets saved as it gets uploaded to the database.

Can you help me with a code which displays the image stored in the folder? In this case this folder is called ‘imagess’

Well, you have something like this going on:
$result=mysql_query(“SELECT * FROM storeimg WHERE id=’”.$_GET[‘id’]."’");
//fetch data from database
$image=mysql_fetch_array($result);
Next, you would add something like;
$imagename=$image[‘image’]; // To get the name of the picture…
Then, you have the name of the picture to load. Basically, you need to place the name into a direct link to the picture to make it visible on the web page. The general layout is text.
Not hard to place a name it that and put into a format for display. Something like this:
echo “” . $imagename . “”;
Note: this uses the picture name for the display. Of course, later on you can add a description field of your picture in your database and display that instead. Also, the name of the picture inside your database must have the extension of the filetype included. If it is a JPG, you need “.jpg” at the end of the picture name. If ALL of your pictures are JPG, you can add this in the name like this:
$imagename=$image[‘image’] . “.jpg”;
If all of the pictures have different extensions, you would have to store that in the database. Some food for thought as they say…

Hope all that helps. Post if you have more questions…

Sponsor our Newsletter | Privacy Policy | Terms of Service