+ Reply to Thread
Results 1 to 9 of 9

Thread: download page querystring

  1. #1
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    download page querystring

    Okay, in my website you can download things...

    But to save work i want to have a single page for all downloads...

    like this::

    example.com/download.php?d=../../files/sambamusic.exe

    or:

    example.com/download.php?d=sambamusic

    regards
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  2. #2
    Brandon's Avatar
    Brandon is offline Former Senior Account Rep Brandon is on a distinguished road
    Join Date
    Jun 2006
    Location
    Tewksbury, MA
    Posts
    9,589

    Re: download page querystring

    Hello,

    I wrote this real quick, untested, but give it a shot it's a step in the correct direction.

    PHP Code:
    <?php

    // brandon long
    // brandon@x10hosting.com

    // example: ?id=filename

    // get the query string
    if (!$_GET['id'])
        
    // die off, could be an error page tooo
        
    die("Need to specifiy a file.");

    // set the diretory to were the files are located
    $dirOfFiles ''// for this example, we will say it's /home/brandon/downloads

    // Set the below variable to the ext.
    $fileEndExt '.exe';

    // below it will set the full path
    $pathToFile $dirOfFiles $_GET['id'] . $fileEndExt;

    // lets open the file

    if ($fileOpenType fopen($pathToFile'r')) {

       
    //get the file size
       
    $fileSizeInfo filesize($pathToFile);

       
    // setup some variables to exit
       
    header("Content-type: application/octet-stream");
       
    header("Content-Disposition: filename=\""$_GET['id'] . $fileEndExt ."\"");
       
    header("Content-length: $fileSizeInfo");

       
    // this outputs the file via the script
       
    while(!feof($fileOpenType)) {
          echo 
    fread($fd2048);
       }

       
    fclose($fileOpenType);
          

    } else {

     
    // file ain't there yo, die off
     
    die('No file named ' $_GET['id'] . $fileEndExt ' exists on the server.');

    }

    ?>
    Last edited by Brandon; 09-16-2010 at 05:43 PM.
    Thanks,
    Brandon Long

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: download page querystring

    Make sure you're not violating the Terms of Service by using your site for file storage.

    Better close that security hole with the likes of realpath and a path prefix check.
    PHP Code:
    ...
    $pathToFile realpath($dirOfFiles $_GET['id'] . $fileEndExt);
    if (
    substr_compare($pathToFile$dirOfFiles0strlen($dirOfFiles)) != 0) {
        
    // attack; file isn't in file storage hierarchy. Return a 404 response
        
    header('HTTP/1.0 404 Not Found');
        include(
    'errordoc/404.php'); // or wherever your custom 404 page is
    } else {
        
    // file is in download folder; safe to send
        
    ...

    Lastly, make sure you use the rewrite engine to hide the download script, since it isn't part of the identity of the resource.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: download page querystring

    Well, it did not work.

    I put the script in to this file: example.com/download.php and download folder to:http://example.com/files/


    I tried to download a file called kamp.exe stored in the files folder. example.com/download.php?kamp


    but the die message just showed up! die("Need to specifiy a file.");
    Last edited by gaptrast; 09-17-2010 at 01:25 AM.
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  5. #5
    Brandon's Avatar
    Brandon is offline Former Senior Account Rep Brandon is on a distinguished road
    Join Date
    Jun 2006
    Location
    Tewksbury, MA
    Posts
    9,589

    Re: download page querystring

    it would be example.com/download.php?id=kamp
    Thanks,
    Brandon Long

  6. #6
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: download page querystring

    ERRORS::


    Warning: filesize() [function.filesize]: stat failed for http://www.thefakevirus.com/pranks/avoid.exe in /home/gaptrast/public_html/download.php on line 27

    Warning: Cannot modify header information - headers already sent by (output started at /home/gaptrast/public_html/download.php:27) in /home/gaptrast/public_html/download.php on line 30

    Warning: Cannot modify header information - headers already sent by (output started at /home/gaptrast/public_html/download.php:27) in /home/gaptrast/public_html/download.php on line 31

    Warning: Cannot modify header information - headers already sent by (output started at /home/gaptrast/public_html/download.php:27) in /home/gaptrast/public_html/download.php on line 32

    one million of theese::

    Warning: fread(): supplied argument is not a valid stream resource in /home/gaptrast/public_html/download.php on line 36
    Last edited by gaptrast; 09-17-2010 at 01:58 AM.
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: download page querystring

    If something doesn't work the way you think, look into how it works. Filesystem functions (including the stat family) don't work with files retrieved via HTTP (or, indeed, many wrappers). Wrappers have to be based on the capabilities of the underlying transport mechanism. HTTP is a fairly simple protocol and doesn't support many filesystem concepts, such as permissions, ownership, ACLs, creation time and access time (user agents & proxies keep track of his). In any case, you shouldn't access local files via the network. It's an unnecessary waste of resources.

    What is the purpose of the download script? What functionality are you missing by linking to the files directly?
    Last edited by misson; 09-17-2010 at 03:07 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: download page querystring

    It worked when I did only write files/ instead of http://example.com/files/

    But I do not want the download to start immidiately, but have a download page saying Download will start shortly. I can put advertisments there...

    like this Example from sourceforge.net
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: download page querystring

    In that case, you don't need a script to read & output the file, you just need to redirect to the file. You can load the file in an iframe to download it, or use JS (or both, with the iframe in a <noscript> tag in case JS is disabled or unsupported). You should avoid a meta refresh, as it's been deprecated for various reasons.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 06-23-2010, 08:11 PM
  2. how to download my web page from your site
    By geryalva52 in forum Free Hosting
    Replies: 2
    Last Post: 06-20-2010, 02:02 PM
  3. Review our site/download page!!!
    By mpband in forum Review My Site
    Replies: 3
    Last Post: 02-01-2009, 03:03 PM
  4. Website trys to download the php page
    By Betty in forum Free Hosting
    Replies: 5
    Last Post: 12-16-2008, 02:58 PM
  5. Replies: 4
    Last Post: 11-09-2007, 07:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers