download file from mysql database

Page 1 of 1

14 Replies - 3891 Views - Last Post: 05 February 2013 - 12:19 PM Rate Topic: -----

#1 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

download file from mysql database

Posted 22 March 2010 - 11:58 PM

I'm trying to make a download feature for my website, but the problem here is that the script I use doesn't seem to work. Every time I try to run it, it shows ";}else{while(list($id, $name), which baffles me since I can't find a solution to it.

The code for the download page is:

//download.html

<html>
<head>
<title>Download File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'conn.php';

$query = "SELECT id, name FROM upload";
$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=<?php=$id;?>"><?php=$name;?></a> <br>
<?php
}
}
?>
</body>
</html>



and here's the download code:

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

include 'conn.php';

$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

exit;
}

?>



Is This A Good Question/Topic? 0
  • +

Replies To: download file from mysql database

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: download file from mysql database

Posted 23 March 2010 - 01:03 AM

View the source of "Download.html" in a browser, you'll probably see the whole PHP Code.

Simply change this file extension from .html to .php, it seems like it's not being parsed.
Was This Post Helpful? 0
  • +
  • -

#3 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

Re: download file from mysql database

Posted 23 March 2010 - 01:13 AM

both codes are stored in two separate files. the first one is download.html and the second one is download.php, I've tried merging them together, but all I got is a blank page.

This post has been edited by hdr: 23 March 2010 - 01:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: download file from mysql database

Posted 23 March 2010 - 01:57 AM

Are you sure your hosting supports PHP?
Was This Post Helpful? 0
  • +
  • -

#5 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

Re: download file from mysql database

Posted 23 March 2010 - 06:55 AM

yes, I'm sure of that. Everything else in PHP works.

turns out in the download.html file, echo "Database is empty <br>"; the <br> tag is the culprit, but after I removed it, now I can only see a blank page.

This post has been edited by hdr: 23 March 2010 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: download file from mysql database

Posted 23 March 2010 - 09:04 AM

You're missing my point. By default PHP won't run from .html files... The <br is not the culprit, the > is... PHP isn't being parsed, thus when you open the <?php tag it thinks you've opened a HTML tag until it's closed by the <br>... That's why you see the code immediately after it.

Rename your current download.php to process.php and the Download.html to download.php.. Simple!
Was This Post Helpful? 0
  • +
  • -

#7 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: download file from mysql database

Posted 23 March 2010 - 09:05 AM

Do me a favour.
At the start of your code add this.

error_reporting(E_ALL);

And let us know if it spits out errors.
Was This Post Helpful? 0
  • +
  • -

#8 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

Re: download file from mysql database

Posted 23 March 2010 - 09:11 AM

I've done that, and I've also added mysql_error() and var_dump as well, and all it shows is a blank page. I even went as far as to run the SQL query on phpmyadmin, which seems to work over there.
Was This Post Helpful? 0
  • +
  • -

#9 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: download file from mysql database

Posted 23 March 2010 - 09:28 AM

Well the guy before me spotted the problem. You have php in your .html file. Sort that out and let us know if you have any further problems. Well spotted Rudi.
Was This Post Helpful? 0
  • +
  • -

#10 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

Re: download file from mysql database

Posted 23 March 2010 - 09:34 AM

no, the first file was supposed to be a html file, but the second one was supposed to be the one doing the processing, ie the download.php file. It's a habit of mine to separate forms from processing code, namely form.html and form.php, the first one was .html and the second is .php
Was This Post Helpful? 0
  • +
  • -

#11 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: download file from mysql database

Posted 23 March 2010 - 09:55 AM

If you want php in your .html file, and you refuse to listen, then i suggest you go bother some other people.
Was This Post Helpful? 0
  • +
  • -

#12 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

Re: download file from mysql database

Posted 23 March 2010 - 10:10 AM

I did try that once, but I ended up with a blank page as well, ok?
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: download file from mysql database

Posted 23 March 2010 - 10:18 AM

You don't appear to be getting this at all. This PHP code in here:
//download.html

<html>
<head>
<title>Download File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'conn.php';

$query = "SELECT id, name FROM upload";
$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=<?php=$id;?>"><?php=$name;?></a> <br>
<?php
}
}
?>
</body>
</html>


WILL. NEVER. EXECUTE.

Read this recent post of mine.
Was This Post Helpful? 0
  • +
  • -

#14 david1842  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 13

Re: download file from mysql database

Posted 05 February 2013 - 12:11 PM

Hi!

How can i download files bigger than 50 MB, this is my code, when i try to download files > 50mb the process stoped...




ini_set("memory_limit","16384M");    
set_time_limit(0); 
require_once("connection_db.php");
$id_file=$_REQUEST['id_doc'];

$qry="Select file_name,size,type,file from file where id='$id_file'";
$res=mysql_query($qry) or die(mysql_error()." qry::$qry");
$obj=mysql_fetch_object($res);

//header
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-type: {$obj->type}");
header('Content-Disposition: attachment; filename="'.$obj->file_name.'"');
header("Content-Transfer-Encoding: binary");
print $obj->file;
		
//close the connection
mysql_close();




Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#15 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 436
  • View blog
  • Posts: 1,840
  • Joined: 30-April 10

Re: download file from mysql database

Posted 05 February 2013 - 12:19 PM

why did you post to a thread that is from 2010?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1