Hi,
The problem I am having is that I cannot retreive pictures I have put into my MySQL db.
I can upload pictures to my db, but I cannot retrive them to view them on a page.
At the moment I have 4 pages.
- up.html:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-UK">
<head>
<title>WAT PHP Lecture Notes - Image Upload to MySQL</title>
<meta http-equiv="Expires" content="Tue, 10 Sep 2002 00:00:00 GMT"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="Author" content=""/>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>
Your name: <input type="text" name="username" /><br />
Small image to upload: <input type="file" size="40" name="userfile" /><br />
<input type="submit" value="Upload file" />
</p></form>
</body></html>
- upload.php
<body>
<h1>Uploading Images to MySQL</h1><p>
You submitted this file:<br /><br />
Temporary name: <?php echo $FILES[userfile][tmp_name] ?><br />
Original name: <?php echo $FILES[userfile][name] ?><br />
Size: <?php echo $FILES[userfile][size] ?> bytes<br />
Type: <?php echo $FILES[userfile][type] ?></p>
<?php
//require file that states the values for the db connection, i.e. Password etc. Makes them secure
require '/home/involve/my.private';
function prntErr($errMesg) {
printf("<p><strong> %s </strong></p>\n", $errMesg);
}
if ( !ereg( "gif|png|x-png|jpeg", $FILES[userfile][type]) ) {
prntErr("Sorry only browser compatible images allowed");
} else if ( strlen($username) < 3 ) {
prntErr("Sorry username too short<br />min 3 characters");
} else if ( $FILES[userfile][size] > 5096 ) {
prntErr("Sorry file too large");
} else if ( !($link=mysql_connect($host, $user, $passwd)) ) {
prntErr("Error connecting to database");
} else if ( !(mysql_select_db($dbName)) ) {
prntErr("Error selecting database");
} else if ( !($handle = fopen ($FILES[userfile][tmp_name], "r")) ) {
prntErr("Error opening temp file");
} else if ( !($image = fread ($handle, filesize($FILES[userfile][tmp_name]))) ) {
prntErr("Error reading temp file");
} else {
$ext = explode('.', $userfile_name);
$filename = $username . '.' . $ext[count($ext)-1];
fclose ($handle);
$image = mysql_escape_string($image);
//Insert image selected into db
$query = "INSERT INTO hero (file,img) VALUES ('$filename','$image')";
if ( !(mysql_query($query,$link)) ) {
prntErr("Error writing image to database");
prntErr(sprintf("Error %s : %s", mysql_errno(), mysql_error()));
} else {
prntErr("Image successfully copied to database") ;
}
}
?>
</body>
These two pages work as when I look in my db table it has entered in the new file id and name, and alot of gobbledigook, which is obviously the image being stored.
However my next 2 pages are supposed to retreive all my images and print them out to the screen. However all I get is a blank image with the red cross, and the gobbledigook from the table.
3.ImageShow.php
<title>WAT PHP Lecture Notes - Image Download from MySQL</title>
<meta http-equiv="Expires" content="Tue, 10 Sep 2002 00:00:00 GMT"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="Author" content=""/>
</head>
<body>
<h1>Images Stored in MySQL</h1>
<snip>
<?php
//require file that states the values for the db connection, i.e. Password etc. Makes them secure
require '/home/involve/my.private';
function prntErr($errMesg) {
printf("<p><strong> %s </strong></p>\n", $errMesg);
}
if ( !($link=mysql_connect($host, $user, $passwd)) ) {
prntErr("Error connecting to database");
} else if ( !(mysql_select_db($dbName)) ) {
prntErr("Error selecting database");
} else {
$query = "SELECT img FROM hero ORDER BY file";
if ( !($result = mysql_query($query,$link)) ) {
prntErr("Error reading database");
prntErr(sprintf("Error %s : %s", mysql_errno(), mysql_error()));
} else {
for ( $i = 0 ; $i < mysql_num_rows($result) ; $i++ ) {
$row = mysql_fetch_row($result);
echo "<img src=\"getImage.php?file=$row[0]\" alt=\"$row[0]\"/> \n";
}
}
}
?>
</snip>
</body></html>
- getImage.php
<?php
//require file that states the values for the db connection, i.e. Password etc. Makes them secure
require '/home/involve/my.private';
$link = mysql_connect($host, $user, $passwd);
mysql_select_db($dbName);
$query = "SELECT img FROM hero WHERE file='$GET[file]'";
$result = mysql_query($query,$link);
$row = mysql_fetch_row($result);
$ext = explode('.', $GET[file]);
$type = $ext[count($ext)-1];
if ( $type == 'gif' )
header("Content-Type: image/gif");
else if ( $type == 'jpg' )
header("Content-Type: image/jpeg");
else if ( $type == 'jpeg' )
header("Content-Type: image/jpeg");
else if ( $type == 'png' )
header("Content-Type: image/png");
echo $row[0];
?>
I can't see why it won't display the images!!!
Sorry there's alot of code, but thought it would be better to post it all, so you can see what I have already done!!
Thanks for any help!!