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 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing a file to the user

    How can I use PHP to send a file to the user (i.e. Pop-up a download box)?

    I have a page (download.php) that has a string passed to it called id. It will use mySQL to find out the URL of the file to be downloaded. I can do all this. I just don't know how to then make the file available to the user to download.

    Sounds simple, huh?
    skinme.net / v2.php (design nearly completed)

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well what you can do is do like most software download sites do and take the file and generate a page like so:

    <html>
    <head>
    <meta http-equiv="refresh" content="3, <?php echo $filename ?>">
    </head>
    <body>
    <h3>Your file will begin downloading in a moment.....</h3>
    </body>
    </html>

    Putting the file name into the location part of a meta refresh tag will cause the download prompt box to open up.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there not a way of doing this without changing the current page (ie by not opening a new page)?

    For example, on DeviantArt (a site I use daily) you click on a download link and although it links to a new page (download.php?id=xxx rather than browse.php?section=blah) the current page doesn't change:

    http://www.deviantart.com/download.php?id=557223 and click on the big picture of the winamp2 skin.

    Thanks.
    skinme.net / v2.php (design nearly completed)

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm... I'm having problems with the method you gave up there, Spookster.

    I've tested it to check it outputs the correct URL by moving it the body and echoing the string containing the URL.

    However, when I run the script as you suggested, outputing the URL as the second argument in the HTTP request, I do not get a download box. I see the blue progress bar in the status bar go to full every 3seconds.
    skinme.net / v2.php (design nearly completed)

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Slight typo on my part with the meta tag.

    <meta http-equiv="refresh" content="3, URL= <?php echo $filename ?>">
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I did think to check the META tag, but I got distracted.
    skinme.net / v2.php (design nearly completed)

  • #7
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just thought I'd share the solution I arrived at:

    download.php
    PHP Code:
    <?php

    header
    ('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    header('Content-type: application/octet-stream');
    header("Content-Disposition: inline; filename=\"".$filename."\"");
    header('Content-length: '.(string)(filesize($path2file)));
    $fd=@fopen($path2file,'r');
    fpassthru($fd);

    ?>
    Where $filename contains "myfile.zip" and $path2file = $DOCUMENT_ROOT."/files/".$filename (or whatever the path to the file is).

    Please note: there is nothing but this code in the file (ie no HTML). It will turn itself into myfile.zip and use HTTP to identify itself as a file to be downloaded.
    skinme.net / v2.php (design nearly completed)

  • #8
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Download.php

    Ok this wasn't written by me, however I found it extremly usefull.


    Original Author: Author: Robert Fogt
    E-mail: webmaster@bluesparks.com

    PHP Code:
    <?

    // The URL to the directory where all the files are located.
    // Do not add a / at the end of the url.
    $filedirectory 'http://downloads.yourdomain.com';

    // The Path to the directory where all files are located.
    // Do not add a / at the end of the path.
    $filepath '/home/username/public_html/downloads';

    // The template filename. Place in same directory as the download.php file.
    $template 'index.html';

    ///////////////////////////////////
    //////////// End setup ////////////
    ///////////////////////////////////

    // For our error messages
    function die_nice($message){
      die(
    "
        <html><head><title>ERROR!!</title></head><body>
        <p>&nbsp;</p>
        <center>
        <table border=\"1\" width=\"400\" bordercolor=\"#FF0000\" cellspacing=\"0\" cellpadding=\"3\">
          <tr>
            <td><font color=\"#000080\" face=\"Arial\">The program encountered an Error!!</font></td>
          </tr>
          <tr>
            <td><p>&nbsp;</p>
            <p><font face=\"Arial\"><b>$message</b></font></p>
            <p>&nbsp;</p></td>
          </tr>
        </table>
        </center>
        </body></html>
    "
    );
    }

    // Check to see if the program was called correctly
    if (!$filedie_nice("Program was called incorrectly, it should be called like:<br>download.php?file=filename");

    // Check to see if the file the want to download exists
    if ( !file_exists("$filepath/$file") ) die_nice("The file you are trying to download does not exist:<br>$file");

    // read in the template file
    $fp fopen ("$template""r") or die_nice("Could not open template file: $template");
    $contents fread ($fpfilesize ($template));
    fclose ($fp);

    // replace the macros
    $contents str_replace("[FILENAME]"$file$contents);
    $contents str_replace("[FILESIZE]"number_format(filesize("$filepath/$file")), $contents);
    $contents str_replace("[MODIFIEDDATE]"date("F j Y"filemtime("$filepath/$file")), $contents);
    $contents str_replace("[DOWNLOAD]""${filedirectory}/$file"$contents);

    // Now display the template
    print stripslashes($contents);

    ?>
    Ok once you have your .php code written the all you need is the .html template to be used.

    Set the variables in the .html template and b0bs your uncle.

    The html code:
    Code:
    FileName: [FILENAME]
    FileSize: [FILESIZE] bytes
    Last Modified: [MODIFIEDDATE]
    
    <a href="[DOWNLOAD]">I agree - Download the File</a>
    The best thing about the html part of this code is that you can use any formating you choose as long as you include the variables.

    Example: Download HTML Template

    Hope this is useful.

    Azzaka


  •  

    Posting Permissions

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