473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Images in a database

Ok, I had a more barebones version of this script working, but when I
tried to put it in with my larger script, it broke. I think the problem
lies somewhere in the uploading, but I'm not sure of this. Anyway,
here's what I have:

index.php
---------------------------------
<?
include("header.inc");
if (!isset($_SESSION['username']) or ($_SESSION['username'] == "Guest")) {
header ("Location: http://www.thisisfake.com/index.php");
}
dbconnect();
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Upload</title>
</head>

<body>
<center>
<?
if($id) {

switch ($action) {

case "delete":
$data = mysql_query("SELECT id,uploader FROM files WHERE id = '$id'");
$id = mysql_result($data,0,"id");
$uploader = mysql_result($data,0,"uploader");
if ($_SESSION["username"] == $uploader || $_SESSION["level"] ==
"admin") {
mysql_query("DELETE FROM files WHERE id = '$id'");
echo "File deleted";
echo '<META HTTP-EQUIV="refresh"
content="2;URL=http://www.thisisfake.com/upload/">';
} else {echo "Invalid permissions";}
break;
default: //Echo contents if image, else send as file download
$query = "select mimetype, data from files where id = $id";
$result = mysql_query($query);

$data = mysql_result($result,0,"data");
$type = mysql_result($result,0,"mimetype");
if ($type == "image/pjeg" || $type == "image/jpeg" || $type ==
"image/x-png" || $type == "image/png" || $type == "image/gif"|| $type ==
"image/bmp") {
echo base64_decode($data);
} else {
get($id); //This send the file as a download, instead of echoing the
contents
}
break;
}
} else {

// Listing of files and the form

echo"
<form method=POST action=upload.php enctype=multipart/form-data>
<p>File to upload:<br>
<input type=file name=file>
<input type='submit' name='submit' value='Upload'>
</form>
";

//All this stuff works
echo "<p></p>";
if ($data = getInfo()) {

echo '<table border="0" align="center">
<tr bgcolor="#bad1d1">
<td>File Name</td>
<td><center>File Size</center></td>
<td><center>Mime Type</center></td>
<td><center>Checksum</center></td>
<td><center>Extension</center></td>
<td><center>Uploader</center></td>
<td><center>Date</center></td>
<td><center>Option</center></td>
</tr>
';
for ($i=0; $i<count($data); $i++) {
echo '
<tr bgcolor=#CCCCCC>
<td><a
href="view.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?action=delete&id='.$data[$i]["id"].'">Delete</a></td>';}
echo '</tr>
';
}
echo '</table>';
echo '<br>';
echo 'Number of files: ';
echo blobcount();
echo '<br>';
}

}
?>
</center>
</body>
</html>


upload.php
----------------------------------------------
<?
include('header.inc');
dbconnect();

$type = $file_type;
if ($type == "image/pjeg" || $type == "image/jpeg" || $type ==
"image/x-png" || $type == "image/png" || $type == "image/gif") {
$handle = fopen($file,'rb');
$file_content = fread($handle,filesize($file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$uploader = $_SESSION['username'];
$sql = "INSERT INTO files (id, file_name, data, file_size, mimetype,
extension, checksum, uploader, date) VALUES ('', '".$file_name."',
'".$encoded."', '".filesize($file)."', '".$file_type."',
'".getExtension($blob_name)."', '".generate_sfv_checksum($file)."',
'".$uploader."', NOW())";
mysql_query($sql);
} else {
$handle = fopen($file,'rb');
$file_content = fread($handle,filesize($file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$uploader = $_SESSION['username'];
$sql = "INSERT INTO files (id, file_name, data, file_size, mimetype,
extension, checksum, uploader, date) VALUES ('', '".$file_name."',
'".$encoded."', '".filesize($file)."', '".$file_type."',
'".getExtension($blob_name)."', '".generate_sfv_checksum($file)."',
'".$uploader."', NOW())";
mysql_query($sql);
}
//}

header ("Location: http://www.thisisfake.com/upload");

?>


view.php
---------------------------------------------------------
<?
echo '<img src="http://www.thisisfake.com/upload/index.php?id='.$id.'">';
?>



I may have broken it further after the initial breaking, so if something
seems doubly broken that might be it. If you need more information just ask.
Jul 17 '05 #1
2 1883
Plex <in*****@thisisfake.com> wrote in
news:-M********************@comcast.com:

I may have broken it further after the initial breaking, so if
something seems doubly broken that might be it. If you need more
information just ask.

error messages, buddy, error messages. surely you're not expecting someone
to debug that line by line??
Jul 17 '05 #2
Ok, I fixed it myself, all it was was the index.php was outputting html
regardless of whether there was an image data request, so as a result
the data send included html in it, which made the image "corrupt".
Jul 17 '05 #3

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

Similar topics

4
by: yawnmoth | last post by:
i have a script that accesses images on a fairly frequent basis, however, i'm thinking that it would be better to have them in an sql database, instead, for the same reason that it is generally...
3
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...
6
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a...
3
by: Alan | last post by:
Hi, I'm converting a database application from Access 97 to C#/SQL Server. Old database contains some images in OLE fields. I've figured out that there's OLE header preceeding actual image data...
10
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....
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
3
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...
1
by: ttamilvanan81 | last post by:
Hai everyone, I need to provide the slideshow for the images. I have upload the images into database. Then i will retrive all the images from the database and provide the slideshow for those...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.