Reputation: 41
Problem: After download, the file doesn't contain the data. i.e it become blank.
So please help me for this.
<?php
session_start();
include_once 'oesdb.php';
$id=$_REQUEST['id'];
if(isset($_REQUEST['id']))
{
$sql=executeQuery("SELECT * FROM file where id=$id");
$rows = mysql_fetch_array($sql);
$file =$rows['file'];
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile('uploads/'.$file);
exit;
}
?>
Upvotes: 4
Views: 8532
Reputation: 6718
The problem is here:
header('Content-Length: ' . filesize($file));
Content-Length
receives zero value and browser downloads zero-length file, as you told him. If $file
is path relative to upload/
, you should do this:
header('Content-Length: ' . filesize('upload/'.$file));
Be sure that filezise()
returns correct size and readfile()
realy outputs it.
But the other problem is that you mentioned UPLOAD
folder and using uploads
. They are not same and case is important. Also, may be using relative paths in 'uploads/'.$file
is not a good idea, it is better to use absolute path. For example, '/var/www/upload/'.$file
.
Upvotes: 0
Reputation: 85
Why not create a HTACCESS file in uploads folder then states
Allow From 127.0.0.1
Deny From All
Then just create a URL, use HTML5's new download feature, do something like this:
<a href="uploads/filenamehere.txt" download="filenamehere.txt">click to download</a>
It saves time trying to use PHP to make a download script.
Upvotes: 2
Reputation: 3901
try replacing this:
$file =$rows['file'];
by this:
$file = "uploads/".$rows['file'];
and this:
readfile('uploads/'.$file);
by this
readfile($file);
if still not working put the value returned by the readfile
function
IMPORTANT
Please take in consideration the sql injection issues (see comment of Ondřej Mirtes)
Upvotes: 0