Magento Expert Forum - Improve your Magento experience

Results 1 to 6 of 6

PHP download manager simple - force downloading

  1. #1
    Moderator shunavi's Avatar
    Join Date
    Mar 2013
    Posts
    130
    Thanks
    10
    Thanked 29 Times in 15 Posts

    Post PHP download manager simple - force downloading

    A force-download script can give you more control over a file download than you would have providing a direct link. Using a force-download script, you can:

    • Validate that a person is logged in
    • Increment a counter in a text file
    • Connect to your database and log IP information, increment a counter, and so on.



    The code itself is pretty basic:

    The Code

    PHP Code:
    // grab the requested file's name
    $file_name $_GET['file'];

    // make sure it's a file before doing anything!
    if(is_file($file_name)) {

        
    /*
            Do any processing you'd like here:
            1.  Increment a counter
            2.  Do something with the DB
            3.  Check user permissions
            4.  Anything you want!
        */

        // required for IE
        
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression''Off');    }

        
    // get the file mime type using the file extension
        
    switch(strtolower(substr(strrchr($file_name'.'), 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 ($file_name)).' GMT');
        
    header('Cache-Control: private',false);
        
    header('Content-Type: '.$mime);
        
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
        
    header('Content-Transfer-Encoding: binary');
        
    header('Content-Length: '.filesize($file_name));    // provide file size
        
    header('Connection: close');
        
    readfile($file_name);        // push it out
        
    exit();


    This file alone isn't secure. You will want to validate that the file doesn't provide access to your website code, files you don't want downloaded, and so on. That code will be specific to your website and needs.

    Do you use a force-download script? What processing do you do inside the script?

    View more threads in the same category:


  2. #2
    Administrator david's Avatar
    Join Date
    Nov 2012
    Posts
    260
    Thanks
    23
    Thanked 40 Times in 33 Posts

    Default

    Good sharing code, thanks shunavi. You rock !!!

  3. #3
    New member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by david View Post
    Good sharing code, thanks shunavi. You rock !!!
    I'e been trying to find a way of simplifying downloads for our choir's Training Files, as most of them are P.C illiterate & find the notion of using Right Click & Save As far too techy, so I've been trying to find a way of forcing a download, rather than an auto play, and despite all sorts of various scripts & adding lines to .htaccess I was still getting nowhere.

    I was also hoping to tart the link page up by using buttons for the downloads, but that added even more problems, so eventually I stuck with the simplest hyperlink method of all namely:
    <a href="/MP3 Directory/Song Name.mp3" download>Song Name</a>

    No script - no .htaccess - just a simple hyperlink.

  4. #4
    Administrator david's Avatar
    Join Date
    Nov 2012
    Posts
    260
    Thanks
    23
    Thanked 40 Times in 33 Posts

    Default

    With your code:

    HTML Code:
    <a href="/MP3 Directory/Song Name.mp3" download>Song Name</a>
    There are some problems can be happen, you need to check:

    • Try to remove "space" in file name: sometimes it will be problem what we will not predict
    • The URL start with "/" will be from document root of domain so that make sure that folder "MP3 Directory" is in document root of domain
    • .htaccess deny access mp3 file: sometimes .htaccess configuration only allow some special file extension like .css, .js, .html, .jpg .... and it will not allow .mp3. In this case, to make sure the reason come from .htaccess let 's try to remove or rename .htaccess and try to access file again
    • In all of reasons above, try to press Ctrl + F5 everytime you try.


    Good luck

  5. #5
    New member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by david View Post
    With your code:

    HTML Code:
    <a href="/MP3 Directory/Song Name.mp3" download>Song Name</a>
    There are some problems can be happen, you need to check:

    • Try to remove "space" in file name: sometimes it will be problem what we will not predict
    • The URL start with "/" will be from document root of domain so that make sure that folder "MP3 Directory" is in document root of domain
    • .htaccess deny access mp3 file: sometimes .htaccess configuration only allow some special file extension like .css, .js, .html, .jpg .... and it will not allow .mp3. In this case, to make sure the reason come from .htaccess let 's try to remove or rename .htaccess and try to access file again
    • In all of reasons above, try to press Ctrl + F5 everytime you try.


    Good luck
    The items I entered here were just simplified variables to demonstrate its usage. A fuller, actual example of one of the links I'm using is:
    <a href="/members/downloads/Summer_2014/All_Parts/All_Parts_-_She.mp3" download>All Parts </a>

    As all this is being done is a closed directory, only accessible to members (by using .htacces & .htpasswd) it doesn't really have to bother about the frills required by on open site.

    Although leading on from this a little (although still connected to the basic idea) is the potential to have the user check multiple boxes to select the files they wish to download, then click 'Download' to it all in one fail swoop.although I imagine that's quite a bit more complicated.

  6. #6
    New member
    Join Date
    Aug 2014
    Location
    India
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Thanks for sharing the Code.

Similar Threads

  1. BJ Synegy - Clean and simple Joomla template
    By flamefox15 in forum Joomla
    Replies: 3
    Last Post: 16-03-2015, 09:42 AM
  2. Simple uploader Using PHP
    By speed2x in forum PHP programming
    Replies: 0
    Last Post: 14-07-2013, 06:19 AM
  3. How do I Solve This Simple Magento JQuery Problem!
    By indusstar777 in forum Programming Questions
    Replies: 4
    Last Post: 14-06-2013, 09:37 AM
  4. A simple php captcha script
    By david in forum PHP programming
    Replies: 3
    Last Post: 12-06-2013, 10:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

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