I put together the following blocs of code for uploading pictures into a database and displaying them on a webpage. The pictures are supposed to be displayed on the member’s only page of a website I’m working on, upon logging in, and they are supposed to be the member’s uploaded picture. I created several members and and used one of my existing member accounts to test the uploading process. The picture upload process appeared to have been successful when I checked on myphpadmin. Yet, when I login with this account, no picture is displayed, instead, a tiny jpg icon is displayed at the top left corner of the box in which the picture was supposed to be displayed. Same thing when I login with the other accounts with which I haven’t yet uploaded a picture.
I’ll start with the code that installs the table in the database
[php]$query = "CREATE TABLE images (
image_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
member_id INT UNSIGNED,
like_id INT UNSIGNED,
image LONGBLOB NOT NULL,
image_name varchar(255) NOT NULL,
image_type varchar(4) NOT NULL,
image_size int(8) NOT NULL,
image_cartegory VARCHAR(20) NOT NULL,
image_path VARCHAR(300),
image_date DATE
)";[/php]
Then here is the code which allows the member to upload his picture:
[php]
[/php]
And here is the insertimage.php which inserts the image into our database: Note that I have to authenticate the user in order to register his session id which is used later on in the select query to identify him and select the right image that corresponds to him.
[php]<?php
//This file inserts the main image into the images table.
//address error handling
ini_set (‘display_errors’, 1);
error_reporting (E_ALL & ~E_NOTICE);
//authenticate user
//Start session
session_start();
//Connect to database
require (‘config.php’);
//Check whether the session variable id is present or not. If not, deny access.
if(!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
header("location: access_denied.php");
exit();
}
else{
// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES[‘image’]) && $_FILES[‘image’][‘size’] > 0) {
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
// Create the query and insert
// into our database.
$query = "INSERT INTO images (member_id, image_cartegory, image_date, image) VALUES ('{$_SESSION['id']}', 'main', NOW(), '$data')";
$results = mysql_query($query);
// Print results
print "Thank you, your file has been uploaded.";
}
else {
print “No image selected/uploaded”;
}
// Close our MySQL Link
mysql_close();
} //End of if statmemnt.
?> [/php]
On the page which is supposed to display the image upon login in, I inserted the following html code in the div that’s supposed to contain the image:
<div id="image_box" style="float:left; background-color: #c0c0c0; height:150px; width:140px; border- color:#a0a0a0;border-style:outset;border-width:1px; margin:auto; ">
<img src=picscript.php?imname=potwoods>
</div>
And finally, the picscript.php contained the select query:
[php]<?php
//address error handling
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
//include the config file
require('config.php');
$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("SELECT* FROM images WHERE member_id = '".$_SESSION['id']."' AND cartegoty = 'main' ");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[image];
header("Content-type: image/jpeg");
print $imagebytes;
?>[/php]
Now the million dollar question is, “What is preventing the picture from getting displayed?” I know this is a very lengthy and laborious problem to follow but I’m sure there is someone out there who can point out where I’m not getting it. Thanks.