Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation help please... !!

    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:

    PHP Code:
        // 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!

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Start with #2 from the forum guidelines:
    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?"
    Are you a Help Vampire?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •