Results 1 to 7 of 7

Thread: code for downloading file

  1. #1
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    when button was clicked then save dialog box should open

    code for downloading file in php
    Last edited by jscheuer1; 07-09-2011 at 03:14 PM. Reason: merge threads/posts

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I don't think you need any special code - just zip up the file and point the href of an anchor tag at it. Zip files should always prompt a download/save box.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Mar 2011
    Posts
    2,174
    Thanks
    60
    Thanked 121 Times in 117 Posts
    Blog Entries
    4

    Default

    I don't undersand what you mean. Are you trying to find a script to download something in php? Also what type of file?

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Please clarify your exact requirements. I already answered your other post about this but now I'm thinking there may be more to it.

    If browsers are behaving strangely maybe this is a header issue?

    Maybe you are enquiring about a zip-and-download script/function? Try pclzip: http://www.phpconcept.net/pclzip/ and tutorial: http://www.techiepark.com/tutorials/...-for-download/

    Or do you need something else?
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default

    PHP Code:
    <?php
     
    // place this code inside a php file and call it f.e. "download.php"
    $path $_SERVER['DOCUMENT_ROOT']."/path2file/"// change the path to fit your websites document structure
    $fullPath $path.$_GET['download_file'];
     
    if (
    $fd fopen ($fullPath"r")) {
        
    $fsize filesize($fullPath);
        
    $path_parts pathinfo($fullPath);
        
    $ext strtolower($path_parts["extension"]);
        switch (
    $ext) {
            case 
    "pdf":
            
    header("Content-type: application/pdf"); // add here more headers for diff. extensions
            
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
            
    break;
            default;
            
    header("Content-type: application/octet-stream");
            
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        }
        
    header("Content-length: $fsize");
        
    header("Cache-control: private"); //use this to open files directly
        
    while(!feof($fd)) {
            
    $buffer fread($fd2048);
            echo 
    $buffer;
        }
    }
    fclose ($fd);
    exit;
    // example: place this kind of link into the document where the file download is offered:
    // <a href="download.php?download_file=some_file.pdf">Download here</a>
    ?>

  6. The Following 2 Users Say Thank You to gurmeet For This Useful Post:

    james438 (07-27-2011),jscheuer1 (07-09-2011)

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code works well gurmeet. Thanks. I have some questions though:

    • Can we redirect to another page and/or echo page content on completion?

    • If not, can we do something concurrently to show the user something?

    • Shouldn't fclose ($fd); be inside the conditional that opened the file and established the $fd handle?

    • Is the buffering necessary? If so why?

    • Similarly is the echoing of the buffer value really needed, and if so why?
    Last edited by jscheuer1; 07-23-2011 at 01:37 AM. Reason: add last two questions
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #7
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    i created a downloads module for my jcow social network site which uses the following code:
    PHP Code:
     
    $fileserver_path 
    'your/downloads/folder/';    // change this to the directory your files reside
    $req_file          basename($_GET['file']);
    $whoami             basename(__FILE__);    // you are free to rename this file 

    if (empty($req_file)) {
        print 
    "Usage: $whoami?file=&lt;file_to_download&gt;";
        exit;
    }

    /* no web spamming */
    if (!preg_match("/^[a-zA-Z0-9._-]+$/"$req_file$matches)) {
        print 
    "I can't do that. sorry.";
        exit;
    }

    /* download any file, but not this one */
    if ($req_file == $whoami) {
        print 
    "I can't do that. sorry.";
        exit;
    }

    /* check if file exists */
    if (!file_exists("$fileserver_path/$req_file")) {
        print 
    "File <strong>$req_file</strong> doesn't exist.";
        exit;
    }

    if (empty(
    $_GET['send_file'])) {
        
    header("Refresh: 5; url=download.php?file=$req_file&send_file=yes");
    }
    else {
        
    header('Content-Description: File Transfer');
        
    header('Content-Type: application/force-download');
        
    header('Content-Length: ' filesize("$fileserver_path/$req_file"));
        
    header('Content-Disposition: attachment; filename=' $req_file);
        
    readfile("$fileserver_path/$req_file");
        exit;
    }

    mysql_query("UPDATE `your_downloads_tbl` SET `download_total` = download_total+1 WHERE `download_file` = '$req_file'");
    echo 
    '
    <h3>Downloading '
    .$req_file.'...</h3>';
    echo 
    '
    <p>Your download should begin shortly. If it doesn\'t start, 
       follow this <a href="'
    .$fileserver_path.''.$req_file.'">link</a>.</p>'
    and here is an example link to the page to download, eg. say we wanna download the file 'forums.zip':
    Code:
    <a href="http://yourdomain.com/download.php?file=forums.zip">Download Forums</a>
    Last edited by liamallan; 07-23-2011 at 08:24 AM.

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
  •