Ok, new problem. I've got a file upload and db insertion thing working fine (I think - that may be the problem), but when I output the images, they are either cut off or broken. 1 image is about 300K, a couple are <100K, and one is < 1K (that one isn't being truncated). I've set my MAX_FILE_SIZE to be 900000 (900K). Anyone have an idea of what the problem would be? Below is the code.
Upload file
Code: Select all
<?PHP
echo <<<UPLOAD
<html>
<head>
</head>
<body>
<form enctype = "multipart/form-data" action = "$PHP_SELF" method = "POST">
<input type = "hidden" name = "MAX_FILE_SIZE" value = "900000">
<input name = "userfile" type = "file" />
<input type = "submit" name = "upload_submit" value = "Upload">
</form>
</body>
</html>
UPLOAD;
//read as binary
$file = $_FILES['userfile']['tmp_name'];
$filehandle = fopen($file,'rb');
$data = fread($filehandle,filesize($file));
$data = addslashes($data);
fclose($filehandle);
//insert into db
$conn = mysql_connect('XXX','XXX','XXX');
$db = mysql_select_db('XXX',$conn);
$filetype = $_FILES['userfile']['type'];
$query = <<<SQL
INSERT
INTO
temp_table
VALUES
('',
'$data',
'$filetype')
SQL;
$result = mysql_query($query,$conn) or die(mysql_error());
?>
Index page (The viewing page)
Code: Select all
<?PHP
$conn = mysql_connect('XXX','XXX','XXX');
$db = mysql_select_db('XXX',$conn);
$query = <<<SQL
SELECT
id
FROM
temp_table
SQL;
$result = mysql_query($query,$conn);
while($row = mysql_fetch_assoc($result))
{
echo <<<IMAGE
<img src = "temp_image.php?image_id=$row[id]" border = "1">
IMAGE;
}
?>
The image.php file (the one that reads the data from the db and outputs it).
Code: Select all
<?PHP
$image_id = $_GET['image_id'];
$conn = mysql_connect('XXX','XXX','XXX');
$db = mysql_select_db('XXX',$conn);
$query = <<<SQL
SELECT
*
FROM
temp_table
WHERE
id = '$image_id'
SQL;
$result = mysql_query($query,$conn);
$row = mysql_fetch_assoc($result);
header('Content-length: '.strlen($row[image]));
header("Content-type: .$row[mime]");
echo $row[image];
?>
Any help would be appreciated. Thanks for the help so far!!
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.