fl00d
05-19-2008, 06:19 AM
Hi,
I've been working on an image upload script. I've got the upload part working correctly, but I can't seem to properly recall the image from the database and show it. I've been following this tutorial: http://www.spoono.com/php/tutorials/tutorial.php?id=42
Basically, I've got an image upload form that uploads image, opens & reads image, base64_encodes the image and inserts it into a longtext MySQL field.
Then I've got getImage.php, whose job is to pick out the appropriate image (given through the id param - getImage.php?id=x) from the database, decoded the data and then echo it.
Finally, I've got view.php whose job is to actually show the image, through the normal HTML call like so:
<img src="getImage.php?id=2" />
The problem is that view.php doesn't show the image, or can't find it. (Shows the broken image logo). I can't figure out why. At one point, it was working, but then I switched databases and BOOM, broken again :/
Here are the files (all are basic/lacking security... 'cause their basic atm :P)
uploadImage.php
<?php
include_once("/home/flawclan/public_html/xxxxx/class/startclasses.php");
######################################################
#getImage.php v1.0 #
#read file, encode it, decode it and display it again#
######################################################
$upload = $_POST['upload'];
if(isset($upload)){
//PHP file upload vars
$fileError = $_FILES['imageFile']['error'];
$fileName = $_FILES['imageFile']['name'];
$fileSize = $_FILES['imageFile']['size'];
$fileType = $_FILES['imageFile']['type'];
$fileTempName = $_FILES['imageFile']['tmp_name'];
//Custom vars
$maxAllowedSize = 200000;
$finalLocation = "/home/flawclan/public_html/xxxx/projects/getimage/files/{$fileName}";
//check if file was uploaded
try{
if(is_uploaded_file($fileTempName)){
//check extension
if(($fileType != "image/gif") || ($fileSize > $maxAllowedSize)){
throw new Exception("Invalid file!");
//move the file
}else{
$moveFile = move_uploaded_file($fileTempName,$finalLocation);
if(!$moveFile){
throw new Exception("Could not move file!");
}else{
#echo "File sucessfully uploaded!";
#echo "<br />Normal upload: <img src=\"http://www.xxxx.com/test/files/{$fileName}\" />";
}
}
}else{
echo "Upload error: ".$fileError;
}
}catch (Exception $e){
echo $e->getMessage();
}
//try and open it, encode it
$fh = fopen($finalLocation,"r"); //rb = read binary
$binary = fread($fh,$fileSize);
fclose($fh);
$encodedBinary = chunk_split(base64_encode($binary));
$ip = $_SERVER['REMOTE_ADDR'];
$sql= "INSERT INTO `xxxx_images` (`id`,`image`,`posted_ip`) VALUES ('NULL','$encodedBinary','$ip')";
$q = mysql_query($sql);
if(!$q){
die(mysql_error());
}else{
echo 'File has been uploaded.';
}
unlink($finalLocation);
}else{
echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\" enctype=\"multipart/form-data\" >";
echo "File: <input type=\"file\" name=\"imageFile\" >";
echo "<br /><input type=\"submit\" name=\"upload\" value=\"Upload\" >";
echo "</form>";
}
?>
getImage.php
<?php
include_once("/home/flawclan/public_html/xxxx/class/startclasses.php");
#############################################
#getImage.php #
#gets images from databases depending #
#on page and displays them #
#############################################
$imageId = $_GET['id'];
$errors = array("invalid" => "Invalid image tag!",
"notfound" => "Image not found!"); //error message array
//check if vars are set
if(!empty($imageId)){
try{
//check to make sure id is integer
if(!is_numeric($imageId)){
throw new Exception($errors['invalid']);
}
//if so, setup sql and run query
$sql = "SELECT `id`,`image` FROM `xxxx_images` WHERE `id`='$imageId' LIMIT 1";
$query = mysql_query($sql);
//check query failed
if(!$query){
throw new Exception($errors['invalid']);
//else check for rows
}else{
$num = mysql_num_rows($query);
if($num == 0){
throw new Exception($errors['notfound']);
}elseif($num == 1){
while($row = mysql_fetch_array($query)){
$showImage = $row['image'];
if(empty($showImage)){
$showImage = "http://www.xxxx.com/ad/images/yourad.gif";
echo $showImage;
die();
}else{
$showImage = base64_decode($showImage);
echo $showImage;
}
}
}else{
throw new Exception($errors['invalid']);
}
}
}catch(Exception $e){
echo $e->getMessage();
}
}else{
header("Location: http://www.xxxx.com");
}
?>
view.php
<img src="getImage.php?id=2" />
Any suggestions?
Thanks
-fl00d
I've been working on an image upload script. I've got the upload part working correctly, but I can't seem to properly recall the image from the database and show it. I've been following this tutorial: http://www.spoono.com/php/tutorials/tutorial.php?id=42
Basically, I've got an image upload form that uploads image, opens & reads image, base64_encodes the image and inserts it into a longtext MySQL field.
Then I've got getImage.php, whose job is to pick out the appropriate image (given through the id param - getImage.php?id=x) from the database, decoded the data and then echo it.
Finally, I've got view.php whose job is to actually show the image, through the normal HTML call like so:
<img src="getImage.php?id=2" />
The problem is that view.php doesn't show the image, or can't find it. (Shows the broken image logo). I can't figure out why. At one point, it was working, but then I switched databases and BOOM, broken again :/
Here are the files (all are basic/lacking security... 'cause their basic atm :P)
uploadImage.php
<?php
include_once("/home/flawclan/public_html/xxxxx/class/startclasses.php");
######################################################
#getImage.php v1.0 #
#read file, encode it, decode it and display it again#
######################################################
$upload = $_POST['upload'];
if(isset($upload)){
//PHP file upload vars
$fileError = $_FILES['imageFile']['error'];
$fileName = $_FILES['imageFile']['name'];
$fileSize = $_FILES['imageFile']['size'];
$fileType = $_FILES['imageFile']['type'];
$fileTempName = $_FILES['imageFile']['tmp_name'];
//Custom vars
$maxAllowedSize = 200000;
$finalLocation = "/home/flawclan/public_html/xxxx/projects/getimage/files/{$fileName}";
//check if file was uploaded
try{
if(is_uploaded_file($fileTempName)){
//check extension
if(($fileType != "image/gif") || ($fileSize > $maxAllowedSize)){
throw new Exception("Invalid file!");
//move the file
}else{
$moveFile = move_uploaded_file($fileTempName,$finalLocation);
if(!$moveFile){
throw new Exception("Could not move file!");
}else{
#echo "File sucessfully uploaded!";
#echo "<br />Normal upload: <img src=\"http://www.xxxx.com/test/files/{$fileName}\" />";
}
}
}else{
echo "Upload error: ".$fileError;
}
}catch (Exception $e){
echo $e->getMessage();
}
//try and open it, encode it
$fh = fopen($finalLocation,"r"); //rb = read binary
$binary = fread($fh,$fileSize);
fclose($fh);
$encodedBinary = chunk_split(base64_encode($binary));
$ip = $_SERVER['REMOTE_ADDR'];
$sql= "INSERT INTO `xxxx_images` (`id`,`image`,`posted_ip`) VALUES ('NULL','$encodedBinary','$ip')";
$q = mysql_query($sql);
if(!$q){
die(mysql_error());
}else{
echo 'File has been uploaded.';
}
unlink($finalLocation);
}else{
echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\" enctype=\"multipart/form-data\" >";
echo "File: <input type=\"file\" name=\"imageFile\" >";
echo "<br /><input type=\"submit\" name=\"upload\" value=\"Upload\" >";
echo "</form>";
}
?>
getImage.php
<?php
include_once("/home/flawclan/public_html/xxxx/class/startclasses.php");
#############################################
#getImage.php #
#gets images from databases depending #
#on page and displays them #
#############################################
$imageId = $_GET['id'];
$errors = array("invalid" => "Invalid image tag!",
"notfound" => "Image not found!"); //error message array
//check if vars are set
if(!empty($imageId)){
try{
//check to make sure id is integer
if(!is_numeric($imageId)){
throw new Exception($errors['invalid']);
}
//if so, setup sql and run query
$sql = "SELECT `id`,`image` FROM `xxxx_images` WHERE `id`='$imageId' LIMIT 1";
$query = mysql_query($sql);
//check query failed
if(!$query){
throw new Exception($errors['invalid']);
//else check for rows
}else{
$num = mysql_num_rows($query);
if($num == 0){
throw new Exception($errors['notfound']);
}elseif($num == 1){
while($row = mysql_fetch_array($query)){
$showImage = $row['image'];
if(empty($showImage)){
$showImage = "http://www.xxxx.com/ad/images/yourad.gif";
echo $showImage;
die();
}else{
$showImage = base64_decode($showImage);
echo $showImage;
}
}
}else{
throw new Exception($errors['invalid']);
}
}
}catch(Exception $e){
echo $e->getMessage();
}
}else{
header("Location: http://www.xxxx.com");
}
?>
view.php
<img src="getImage.php?id=2" />
Any suggestions?
Thanks
-fl00d