bottyz
03-02-2010, 02:39 PM
Hi all,
I'm pulling my hair out now as i've spent the best part of 8 hours trying to setup a download file script in php without any success. I have tried many different methods from many different sources and all perform the same.
basically, i have a user login area from which users can download software etc zip, jpg, pdf and so on. Obviuously, i don't want the users to know where the files are stored and also i don't want them accessible to the whole world.
My problem is that when i have a script which looks good on screen, when it comes to download say a zip file it will only download it as 1kb. Even if it states the actual size in the download dialog.
the current revision of my code is as follows:
// grab the requested file's name
//$filename = $_GET['file'];
// CODE FOR CHECKING USER DETAILS GOES HERE
// if file exists and user access granted:
// define the path to your download folder plus assign the file name
$path = 'files/'.$filename;
// check that file exists and is readable
//if (file_exists($path) && is_readable($path)) {
// make sure it's a file before doing anything!
if(is_file($filename))
{
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
//get the file size and send the headers
$size = filesize($filename);
// get the file mime type using the file extension
switch(strtolower(substr(strrchr($filename,'.'),1)))
{
case 'pdf': $mime = 'application/pdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpeg':
case 'jpg': $mime = 'image/jpg'; break;
default: $mime = 'application/force-download';
}
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($filename)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length:' .$size); // provide file size
header('Connection: close');
readfile($filename); // push it out
exit;
}
i've even tried hardcoding the filesize and it still picks it up wrong
I'm urgent for this now as a deadline is looming, so any help would be great!
I'm pulling my hair out now as i've spent the best part of 8 hours trying to setup a download file script in php without any success. I have tried many different methods from many different sources and all perform the same.
basically, i have a user login area from which users can download software etc zip, jpg, pdf and so on. Obviuously, i don't want the users to know where the files are stored and also i don't want them accessible to the whole world.
My problem is that when i have a script which looks good on screen, when it comes to download say a zip file it will only download it as 1kb. Even if it states the actual size in the download dialog.
the current revision of my code is as follows:
// grab the requested file's name
//$filename = $_GET['file'];
// CODE FOR CHECKING USER DETAILS GOES HERE
// if file exists and user access granted:
// define the path to your download folder plus assign the file name
$path = 'files/'.$filename;
// check that file exists and is readable
//if (file_exists($path) && is_readable($path)) {
// make sure it's a file before doing anything!
if(is_file($filename))
{
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
//get the file size and send the headers
$size = filesize($filename);
// get the file mime type using the file extension
switch(strtolower(substr(strrchr($filename,'.'),1)))
{
case 'pdf': $mime = 'application/pdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpeg':
case 'jpg': $mime = 'image/jpg'; break;
default: $mime = 'application/force-download';
}
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($filename)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length:' .$size); // provide file size
header('Connection: close');
readfile($filename); // push it out
exit;
}
i've even tried hardcoding the filesize and it still picks it up wrong
I'm urgent for this now as a deadline is looming, so any help would be great!