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
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
Comment