PHP download management
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: PHP download management

  1. #1
    The Prancing Pirate
    Join Date
    Jul 2004
    Posts
    548

    PHP download management

    Hi,

    I'm currently learning PHP, and I'd like to put it into practice to help me learn. Now, I've got a vague idea of how to make a download script so that if the value of a certain variable is '1', the first download is selected, if it's '2', the second is selected, and so on... But, all the time, the download source's URI is not revealed.

    As I was saying, I have a vague idea of how to do it - but I know it wrong. I was thinking of something like this:
    PHP Code:
    <?php

    $dl 
    '0';

    switch(
    $dl){
        case 
    '1':
            echo 
    'data/download1.zip';
        case 
    '2':
            echo 
    'data/download2.zip';
    }

    ?&
    gt
    I can already see most of you cringing after that! Ok, so it's pretty obvious this doesn't work. So, could someone please tell me what I can do to make it work?

    Thanks in advance,

    -jk
    TAZForum <---- click

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    I'm guessing you're doing this through php to have a download counter and hide where the file is on the server? If my second guess is correct you can also have your downloads in a folder that is no accessible since what the link I'm going to give you will show you works locally to serve local files to remote users.
    http://www.antionline.com/showthread...r=1#post880750
    I'm not sure how it's really done, but that would be a simple way to do it. Hope that helped.

  3. #3
    The Prancing Pirate
    Join Date
    Jul 2004
    Posts
    548
    Hi heretic, thanks for replying!

    I checked out the thread you linked to - and I've tried to implement what it had, but it isn't working. I get an error in 'line 19' (which just so happens to be '&lt;/html&gt;', and there's nothing wrong with that).

    Here's the code I'm using:
    PHP Code:
    &lt;html&gt;
    &
    lt;head&gt;
    &
    lt;title&gt;Downloads&lt;/title&gt;
    &
    lt;/head&gt;
    &
    lt;body&gt;
    &
    lt;?php 

    $dl 
    '0'
     
    switch(
    $dl){ 
        case 
    '1'
            
    header('Content-Type: application/zip'); 
            echo 
    file_get_contents('/data/download1.zip'); 
        case 
    '2'
            
    header('Content-Type: application/zip'); 
            echo 
    file_get_contents('/data/download2.zip'); 
    ?&
    gt;
    &
    lt;/body&gt;
    &
    lt;/html&gt
    I also tried removing the header lines, but that leaves me with a similar error. Any ideas?

    Thanks,

    -jk

    [edit]

    I'm guessing you're doing this through php to have a download counter and hide where the file is on the server
    Yup, that's right!

    [edit2]

    Wait a minute...I left out a closing brace. I'll see if it works now...
    TAZForum <---- click

  4. #4
    The Prancing Pirate
    Join Date
    Jul 2004
    Posts
    548
    Ah ok, now I've fixed the code by adding a closign brace to the switch statement. But, I still can't get the download to begin - I'm just taken to the blank page and that's where it stops.

    Here's the corrected code:
    PHP Code:
    &lt;?php 

    $dl 
    '0'
     
    switch(
    $dl){ 
        case 
    '1'
            echo 
    file_get_contents('/data/download1.zip'); 
            break;
        case 
    '2'
            echo 
    file_get_contents('/data/download2.zip'); 
            break;
    }
    ?&
    gt
    You can see what happens here.

    Thanks again,

    -jk
    TAZForum <---- click

  5. #5
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    Don't put the html stuff in, to do it that way you'll want to only have the echo of file_get_contents(). I think there's another way to load the page then have a download dialog box appear. Probably with js or something. But to do it how I was talking about the echo of file_get_contents has to be your only output.

  6. #6
    im not sure how well that would work to get zip files anyways, as i get the impression that get_file_contents is more designed for reading plaintext. now while i havent read an rfc on zip, the file is compressed. im not sure how that would affect trying to read the file as if it was plaintext and then trying to force the server to display it as a zip?

    now there is gzip/zip/rar support in PHP, although you may need to install some extensions or possibly PECL. If you really want to keep your files hidden, id suggest trying to dynamically zipping the files on each request. that way the user never really knows where they are comming from. Im not sure what kind of performance impacts that would have. wouldnt suggest it for a high volume website but for something that only gets downloaded a few times a day, shouldnt be difficult.
    Geoff

  7. #7
    The Prancing Pirate
    Join Date
    Jul 2004
    Posts
    548
    Ok, I get what I'm trying to do - I'm telling PHP to get the contents of the zip files and try to display it as plaintext, when it obviously isn't! Hehe....oops!

    I'm not doing this so much because I want to hide my downloads, but because I think it'd be useful to know for the future. Piping requests to a user without displaying the URI might come in handy someday.

    If you really want to keep your files hidden, id suggest trying to dynamically zipping the files on each request. that way the user never really knows where they are comming from. Im not sure what kind of performance impacts that would have. wouldnt suggest it for a high volume website but for something that only gets downloaded a few times a day, shouldnt be difficult.
    How would I do that?

    All I want to do is give the user a download popup - but to give them a URI like http://blah.com/download.php?dl=1, so they don't have a direct link to the content. I've seen it in action before, and they were using PHP for it (on a very large download site), so there should be some way of doing it...

    Thanks so far!

    -jk
    TAZForum <---- click

  8. #8
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    untested

    Code:
    &lt;?php
    
    if(!empty($_GET['file_id']) {
        
        // do any additional stuff
        
        switch ($_GET['file_id']) {
        case 0:
           header("Location: http://fileLocation.com");
           break;
        case 1:
           header("Location: http://fileLocation.com");
           break;
        case 2:
           header("Location: http://fileLocation.com");
           break;
        default:
           echo "No file found with that id";
        }
        
        exit();
        
    } else {
        echo "Please specify a file ID";
    }
    
    ?&gt;

  9. #9
    The Prancing Pirate
    Join Date
    Jul 2004
    Posts
    548
    Thanks valhallen for replying so quickly! Em, I tried setting a value to file_id using ?file_id=n (where n is the download) but it didn't work. How can I set a value to it properly?

    Cheers,

    -jk
    TAZForum <---- click

  10. #10
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    If you were using the same code as valhallen gave, then it may not have worked if the id you were trying was 0. 0 counts as empty. Otherwise I don't see why it didn't work for you. Could we see what you have?

Posting Permissions

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