473,569 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Viewing images stored in a database

Ok, I've written this script which is supposed to take an image
uploaded by a user and put it in to a database, then at a later date
be able to extract the image from the database and display the image
to the user.

The problem is that whenever I try to display the image, all I get is
an error message that the image contains errors. Can anyone tell me
where or why these errors might be coming up? Here is my script (I'm
pretty sure it's all there):
<?
include("header .inc");
if (!isset($_SESSI ON['username']) or ($_SESSION['username'] ==
"Guest")) {
header ("Location: ../index.php");
}
dbconnect();

if($id) {

$query = "select mimetype, data from files where id = $id";
$result = mysql_query($qu ery);

$data = mysql_result($r esult,0,"data") ;
$type = mysql_result($r esult,0,"mimety pe");

Header( "Content-type: $type");

$size = 150; // new image width
$src = imagecreatefrom string($data);
$width = imagesx($src);
$height = imagesy($src);
$aspect_ratio = $height/$width;

if ($width <= $size) {
$new_w = $width;
$new_h = $height;
} else {
$new_w = $size;
$new_h = abs($new_w * $aspect_ratio);
}

$img = imagecreatetrue color($new_w,$n ew_h);
imagecopyresize d($img,$src,0,0 ,0,0,$new_w,$ne w_h,$width,$hei ght);

// determine image type and send it to the client
if ($type == "image/pjpeg") {
imagejpeg($img) ;
} else if ($type == "image/jpeg") {
imagejpeg($img) ;
} else if ($type == "image/x-png") {
imagepng($img);
} else if ($type == "image/gif") {
imagegif($img);
}
imagedestroy($i mg);

}

if (isset($_POST['submit'])) {
if (isset($blob_na me)) {

if(!$blob_id = upload($blob, $blob_type, $blob_name,
NULL, $_SESSION['username'])) {
echo "Error uploading file";
} else {
echo "File uploaded";
}
}
}

echo"
<form method=POST action=$PHP_SEL F
enctype=multipa rt/form-data>
<p>File to upload:<br>
<input type=file name=blob>
<input type='submit' name='submit' value='Upload'>
</form>
";
echo "<p></p>";
if ($data = getInfo()) {

echo '<table border="0" align="center">
<tr bgcolor="#bad1d 1">
<td>File Name</td>
<td><center>Fil e Size</center></td>
<td><center>Mim e Type</center></td>
<td><center>Che cksum</center></td>
<td><center>Ext ension</center></td>
<td><center>Upl oader</center></td>
<td><center>Dat e</center></td>
<td><center>Opt ion</center></td>
</tr>
';
for ($i=0; $i<count($data) ; $i++) {
echo '
<tr bgcolor=#CCCCCC >
<td><a
href="index.php ?id='.$data[$i]["id"].'">'.$data[$i]["file_name"].'</a></td>
<td>'.$data[$i]["file_size"].'</td>
<td>'.$data[$i]["mimetype"].'</td>
<td>'.$data[$i]["checksum"].'</td>
<td>'.$data[$i]["extension"].'</td>
<td>'.$data[$i]["uploader"].'</td>
<td>'.$data[$i]["date"].'</td>';
if ($_SESSION["username"] == $data[$i]["uploader"] ||
$_SESSION["level"] == "admin") {
echo '<td><a
href="index.php ?id='.$data[$i]["id"].'">Delete</a></td>';}
echo '</tr>
';
}
echo '</table>';
echo '<br>';
echo 'Number of files: ';
echo blobcount();
echo '<br>';
}
?>
<?
function upload($blob, $blob_type, $blob_name, $blob_id = 0,
$uploader) {
if ($blob_id < 1) {
return add($blob, $blob_type, $blob_name, $uploader);
} else {
return update($blob_id , $blob, $blob_type, $blob_name,
$uploader);
}
}

function add($blob, $blob_type, $blob_name, $uploader) {
if ($blob_id = dbinsert("INSER T INTO files (id, file_name,
data, file_size, mimetype, extension, checksum, uploader, date) VALUES
('', '".$blob_name." ', '".prepareFile( $blob)."',
'".filesize($bl ob)."', '".$blob_type." ',
'".getExtension ($blob_name)."' , '".generate_sfv _checksum($blob )."',
'".$uploader."' , NOW())")) {
return $blob_id;
} else {
echo 'Error adding file';
return false;
}
}

function generate_sfv_ch ecksum($blob) {
$sfv_checksum =
strtoupper(dech ex(crc32(file_g et_contents($bl ob))));
return $sfv_checksum;
}

function getExtension($f ilename) {
return ereg( ".([^\.]+)$", $filename, $r ) ? $r[1] : "";
}

function prepareFile($bl ob) {
$blob = addslashes(frea d(fopen($blob, "rb"),
filesize($blob) ));
$blob = base64_encode($ blob);
return $blob;
}

function getInfo($ID = false) {
if ($ID) {
return dbselect("SELEC T id, mimetype, extension,
file_size, checksum, file_name, uploader, date FROM files WHERE id =
'".$ID."'");
} else {
return dbselect("SELEC T id, mimetype, extension,
file_size, checksum, file_name, uploader, date FROM files");
}
}
?>

--Plex
Jul 17 '05 #1
2 2009
On Sat, 21 Aug 2004 02:37:59 -0700, Plex <in*****@thisis fake.com> wrote:
Ok, I've written this script which is supposed to take an image
uploaded by a user and put it in to a database, then at a later date
be able to extract the image from the database and display the image
to the user.

The problem is that whenever I try to display the image, all I get is
an error message that the image contains errors. Can anyone tell me
where or why these errors might be coming up? Here is my script (I'm
pretty sure it's all there):
if($id) {

$query = "select mimetype, data from files where id = $id";
$result = mysql_query($qu ery);

$data = mysql_result($r esult,0,"data") ;
$type = mysql_result($r esult,0,"mimety pe");

Header( "Content-type: $type");

$size = 150; // new image width
$src = imagecreatefrom string($data);
[snip]
function prepareFile($bl ob) {
$blob = addslashes(frea d(fopen($blob, "rb"),
filesize($blob )));
$blob = base64_encode($ blob);
As far as I can see, if you remove this line then the rest looks OK. For some
reason you're trying to use base64 encoded data as if it were the original raw
data; if you're uploading into a BLOB field you don't need this step.
return $blob;
}


--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
On Sat, 21 Aug 2004 11:43:50 +0100, Andy Hassall <an**@andyh.co. uk>
wrote:
On Sat, 21 Aug 2004 02:37:59 -0700, Plex <in*****@thisis fake.com> wrote:
Ok, I've written this script which is supposed to take an image
uploaded by a user and put it in to a database, then at a later date
be able to extract the image from the database and display the image
to the user.

The problem is that whenever I try to display the image, all I get is
an error message that the image contains errors. Can anyone tell me
where or why these errors might be coming up? Here is my script (I'm
pretty sure it's all there):
if($id) {

$query = "select mimetype, data from files where id = $id";
$result = mysql_query($qu ery);

$data = mysql_result($r esult,0,"data") ;
$type = mysql_result($r esult,0,"mimety pe");

Header( "Content-type: $type");

$size = 150; // new image width
$src = imagecreatefrom string($data);

[snip]
function prepareFile($bl ob) {
$blob = addslashes(frea d(fopen($blob, "rb"),
filesize($blo b)));
$blob = base64_encode($ blob);


As far as I can see, if you remove this line then the rest looks OK. For some
reason you're trying to use base64 encoded data as if it were the original raw
data; if you're uploading into a BLOB field you don't need this step.
return $blob;
}


I took out that line (I think it's a remnant from when I was trying
something else to get it to work), and uploaded another picture. This
time I also put in a couple lines to output the data receive from the
database into a file, so I could check if the data was somehow getting
corrupted.

Taking out that encode line, the file output worked (images uploaded
with it didn't). However, even thought the file output worked, I am
still getting the error message about the file containing errors, so I
think the problem may be somewhere in processing and outputting the
data, but I don't know enough about the image functions to know where
the problem might be (or if they're even the problem).

--Plex
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3486
by: Srdjan Pejic | last post by:
Hello, I have a problem that I have not been able to solve, even after searching the web. I have stored couple of images in the MySQL database and I am trying to get them displayed on a web page with PHP. Now, before we go any further, I want to make it clear that I am very aware of the current debate about storing images in the database....
0
1074
by: Paul | last post by:
I have tiff images stored in a sql database as image datatype. Through code, I will do a select statement on the table and need to output a selected tiff in my browser. I realize that browsers do not support tiffs, but is there a way to output it as binary. I have tried every example I can find on the net and still not getting the result I...
9
9763
by: charliewest | last post by:
Hello - I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#) is there any way to temporarily save an image to a session object, and after running some other operations, later retrieve the image from the session object, convert it back to an image, and re-save it to the database? Thanks?
10
2302
by: Neo Geshel | last post by:
I am seeking to hand-roll my own blog in ASP.NET 2.0 and SQLExpress 2005. Why? Because I can. Because I will gain experience. The one thing that has me stumped at square one is inline images. That is, images inline with the actual content of the blog itself. Is there an example that I can be pointed to, where I can examine some code and ...
10
5289
by: NH | last post by:
I have a girdview with paging enabled. How can I add a message in the footer to say "Viewing records 1-15 of 45" etc Thanks
3
2698
by: leen85 | last post by:
helo everybody...i'm lina...i need your help...i have a problem in Access...i have 38000 images for my company employee which size about 5GB to stored in database...as we know we cant stored too many images bcoz it cause bloating in access..so,i want to stored the path of the images only in database...i have stored the path and the images can be...
0
2064
by: leen85 | last post by:
helo everybody...i'm lina...i need your help...i have a problem in VB and Access...i have 38000 images for my company employee which size about 5GB to stored in database...as we know we cant stored too many images bcoz it cause bloating in access..so,i want to stored the path of the images only in database...i have stored the path and the images...
3
2461
by: najimou | last post by:
Hi everyone I will be having a split database, running on 2 computers via mapped drive. computer "A" will have one front end and the back end located in c: \mydatabse 2 tables have links to many images stored as c:\mydatabse\images \images.jpg so computer A has no problem accessing the picture
7
1978
by: Keith Hughitt | last post by:
Hi all, I am having trouble preloading images in a javascript application, and was wondering if anyone had any suggestions. Basically I have a bunch of images stored in a database as BLOBs. At any given point in time a subset of those images is displayed on- screen. At certains times I want to swap out those on screen with new ones from...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.