downloading images from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

downloading images from mysql

Post 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?
:?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Can we see the code of download.php? It sounds like you're not sending the correct mime headers.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

I'm sorry Feyd, I'm not sure what that all means. I very infant with php and the like.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's the headers sent from download.php which your script is generating the link to.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post 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.
Post Reply