retrieving images from mysql database using php


Posted on 16th Feb 2014 07:03 pm by admin

So I've been trying to figure out how to store images in a mysql database, and as far as i can tell the images are stored but getting them out seems to be the problem.

when i try to go to the page on my webhost it says that

"Can not select the database: Access denied for user 'testimg'@'localhost' to database 'testImages'"

and when i goto show.php?id=1 on my local mamp install it gives me all kinds of weird symbols

ÿÀ�™Ì�ÿÄ�È�������������������������� �!1AQaq"2‘¡±BR#Ábr‚’Ñ¢Â3CS$á²Òsƒ“%ðñc£Ã4â³T56Dt”E&Ód¤´ÄUu•7���!1AQaqð‘¡"2±ÁÑáñBRÂbr⢉#3ÿÚ� ��?�Ü/é -è

i've checked everything in the code a million times and searched google and this forum for anything that can help me but i haven't been able to find something that has helped me understand what exactly is going on and why


heres the upload form

<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="1500000" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>


this is my insert.php that processes the image after its submitted


<?php

include './database.php';


$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db ($database);


if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

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

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));

$data = addslashes($data);
fclose($fp);

$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);

print "Thank you, your file has been uploaded.";

}else{
print "No image selected/uploaded";
}

mysql_close($link);
?>

and heres the show.php that displays the image using the id

<?php

include './database.php';

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT image FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_assoc($query);
$content = $row['image'];

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

}

?>

No comments posted yet

Your Answer:

Login to answer
244 Like 13 Dislike
Previous forums Next forums
Other forums

Working with Dates, help.
Sooo to make a long story short, here's what im trying to accomplish.

I need to create a drop

Oracle11g Patch issue
Hi all,

I'm getting the following error while installing Oracle11g Patch 11.1.0.7.0 on IBM-AI

Redistributing dependent dlls
Hai all ,

I have created an application in VC++ using VS2008 in a development machine which r

moving mouse to display image coordinates
I have an existing MFC application that shows an image in the main window.
I'd like to be able to

SAP Management Console is blank
Dear All ,

I am facing problem in my des sever suddely in SAP Management Console is blank and

b+ tree
Hi
can every body help me about b+ tree ?(insert & delete)

Help With editting and deleting form
So look at this image :

http://img194.imageshack.us/img194/8272/snapshot5f.png
This table

How many users online in the last 24-hours using time()?
I'm fairly new to PHP. I have a MySQL table called 'users' and a field called 'loggedin' which is in

whats wrong with my code please help!!!
this is the error


Warning: mysql_close(): supplied argument is not a valid MySQL-Link res

Server side $_SESSION
how does one keep the session completely server side. no cookies to the browser at all. i need this

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash