View Full Version : help please... !!

03-02-2010, 03: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'];

// 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!

// 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
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


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!

03-02-2010, 04:02 PM
Start with #2 from the forum guidelines (http://www.codingforums.com/postguide.htm):

2) Type in a subject that summarizes your question- A great way to turn off other members wanting to help you is to type an incomplete or silly subject for your post. Examples of poor subject titles include:

- "Help!"
- "I'm a newbie...be gentle"
- "How can I..."
- "Urgent...deadline tomorrow!"

The above subjects either have absolutely nothing to do with the question itself, or are grossly incomplete (ie: "How can I..."). When asking for help, enter a subject that summarizes your question, period! Don't use silly, incomplete, or "bait" subjects. Some examples of good titles are:

- "How do I round a number to 2 decimal places using JavaScript?"
- "What's the difference between require() and include() in PHP?"
- "Can someone suggest a good, free HTML editor?"