Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help! PHP + MySQL - Downloading pdfs etc.

Options
  • 03-09-2008 11:18am
    #1
    Registered Users Posts: 386 ✭✭


    Hi,

    I really hope that someone can help me as it is doing my nut in...

    I have a page with a MySQL where I want to store small pdfs and docs etc to then present these for downloading.

    MySQL appears to store the files properly, storing them as medium blobs, and filetypes are identified properly as "application/pdf" and "application/msword" etc.

    However when I try to download one of these files through the script below I am only presented with an empty "download.php" with file type being "php_auto_file" however if I store and try to download a standard .exe file it presents me with the actual file and the type is "Application, filesize".
    <?
    if(isset($_GET['id']))
    {
    	include 'includes/config_file.php';
    	include 'includes/opendb.php';
    
    	$id      = $_GET['id'];
    	$query   = "SELECT name, type, size, content FROM prodfiles WHERE id = '$id'";
    	$result  = mysql_query($query) 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;
    
    	include 'includes/closedb.php';	
    	exit;
    }
    
    ?>
    <html>
    <head>
    <title>File Download</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <?
    	include 'includes/config_file.php';
    	include 'includes/opendb.php';
    
    $query  = "SELECT id, name FROM prodfiles";
    $result = mysql_query($query) 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>
    <?		
    	}
    }
    include 'includes/closedb.php';
    ?>
    </body>
    </html>
    

    Is there anyone that has a clue where I'm going wrong?

    Database structure:
    CREATE TABLE `prodfiles` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(100) default NULL,
      `type` varchar(100) default NULL,
      `size` int(11) NOT NULL,
      `content` mediumblob NOT NULL,
      `description` varchar(50) NOT NULL,
      `category` varchar(50) NOT NULL,
      PRIMARY KEY  (`id`)
    )
    

    Thanks in advance!


Comments

  • Registered Users Posts: 3,594 ✭✭✭forbairt


    I'd strongly recommend against storing the files in the database

    I'd store links to the files which are stored on disk

    Are you sure you've the files entered correctly into the database ?

    save one of the exe's and try running it .. my guess is you're sending the correct header info ... but the file content is crap


  • Registered Users Posts: 386 ✭✭JanneG


    Dowloading and running any uploaded exe file is working perfectly, so yes the files are stored properly...

    I know it's not ideal to be storing the files in the database but it would be very small files and it's to make sure that it's as user friendly as possible for certain users...


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    on the download.php file, right at the very top add a header
    [php]
    header("Content-type:application/pdf");
    [/php]


Advertisement