Page 1 of 1
downloading images from mysql
Posted: Thu Mar 23, 2006 3:01 pm
by dru_nasty
I am testing this script here.
http://www.php-mysql-tutorial.com/php-mysql-upload.php
All works fine for me. I am able to upload a file to the db. The problem comes when trying to download the file.
Here is the download test file from the original tutorial.
http://www.php-mysql-tutorial.com/examples/download.php
I'm getting errors that the files are corrupt when I try and download them.
Here is the page I made to download
http://docmann.com/get.php. I'm getting "The requested URL http:// was not found on this server." when trying to download that.
Is this a common issues with something? Or could it just be a mistake in syntax somewhere.
Or does anyone know of a better tutorial to use where the example actually works?

Posted: Thu Mar 23, 2006 3:27 pm
by pickle
Can we see the code of download.php? It sounds like you're not sending the correct mime headers.
Posted: Thu Mar 23, 2006 4:30 pm
by dru_nasty
Code: Select all
<?
if(isset($_GET['id']))
{
$conn = mysql_connect("hostname", "username","password");
mysql_select_db("dbname", $conn);
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result = mysql_query($query,$conn) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;
exit;
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
$conn = mysql_connect("hostname", "username","password");
mysql_select_db("dbname", $conn);
$query = "SELECT id, name FROM upload";
$result = mysql_query($query,$conn) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?
}
}
?>
</body>
</html>
Posted: Thu Mar 23, 2006 4:39 pm
by feyd
I'm able to fetch the link.. although it wants me to download it..
Code: Select all
[feyd@home]>php -r "var_export(get_headers('http://docmann.com/download.php?id=1'));"
array (
0 => 'HTTP/1.1 200 OK',
1 => 'Date: Thu, 23 Mar 2006 22:37:31 GMT',
2 => 'WWW-Authenticate: Basic realm="Password Protected Area"',
3 => 'P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"',
4 => 'Content-Disposition: attachment; filename=blocks.jpg',
5 => 'Content-length: 29433',
6 => 'Connection: close',
7 => 'Content-Type: image/jpeg',
)
PHP 5.1.2
Posted: Thu Mar 23, 2006 5:39 pm
by dru_nasty
I'm sorry Feyd, I'm not sure what that all means. I very infant with php and the like.
Posted: Thu Mar 23, 2006 6:02 pm
by feyd
that's the headers sent from download.php which your script is generating the link to.
Posted: Fri Mar 24, 2006 9:43 am
by pickle
Don't send this header:
Code: Select all
Content-Disposition: attachment; filename=blocks.jpg
The 'attachment' tells the browser to download the file rather than view it.
Posted: Fri Mar 24, 2006 11:37 am
by dru_nasty
pickle wrote:Don't send this header:
Code: Select all
Content-Disposition: attachment; filename=blocks.jpg
The 'attachment' tells the browser to download the file rather than view it.
Well that's the plan, for it to download to the computer. This is being used for a printing company's website so customers can upload not so large files. Then they can download them to their system.
Actually after a couple more uploads it seems to be working.
http://docmann.com/get.php It was the first file (blocks.jpg) that was getting the error. But you'll see that I uploaded that same file again and it works. Maybe something happened in that first transmission...not sure.