www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Hide the download link of a file

  1. #1
    Join Date
    Jun 2006
    Posts
    18

    Hide the download link of a file

    Hi.

    I have searched through forums. and found two topics that might've helped me... but as I 've never "touched" PHP, they flied up from my head. I am going to start learning PHP from beginning in a few days, but I need this help ASAP. So please ... someone bear this noob....

    What I want to do is, create a code (I didn't say page, because I already have the page... ).. that will start a DOWNLOAD of a file on my serverm without showing its actual path. Not even if it is being downloaded from Flashget/DAcc/Gozilla etc..

    Thank you.

  2. #2
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Send the correct headers for the file, then output the file contents to the user using readfile("file_to_read.blah");

  3. #3
    Join Date
    Jun 2006
    Posts
    18
    I have got no idea what you said sorry

  4. #4
    Join Date
    May 2003
    Location
    Pensacola, FL
    Posts
    635
    http://www.webmasterworld.com/forum88/5326.htm You could use this same technique and make a dynamic page to call a variety of files. Something like download.php?file=1234 or whatever. The database would have an associated file path and name to "1234".

  5. #5
    Join Date
    Jun 2007
    Posts
    16
    Save this as get.php
    Code:
    <?php
      $id = $_GET['id'];
      $links = array(
        "download" => "http://www.domains.com/path/to/file.zip"
        );
    
      header("Location:".$links[$id]);
      exit;
    ?>
    On the page where they click the link to download, use this link:
    Code:
    <a href="get.php?download">Download</a>
    Change the link in the first scrpt to the link to the file.

    This sound hide the contents.

  6. #6
    Join Date
    Jun 2006
    Posts
    18
    Quote Originally Posted by mmumpower
    Save this as get.php
    Code:
    <?php
      $id = $_GET['id'];
      $links = array(
        "download" => "http://www.domains.com/path/to/file.zip"
        );
    
      header("Location:".$links[$id]);
      exit;
    ?>
    On the page where they click the link to download, use this link:
    Code:
    <a href="get.php?download">Download</a>
    Change the link in the first scrpt to the link to the file.

    This sound hide the contents.
    Thanks man, I have a feeling this will work, but when I did it, exactly as you said, nothing happened, just a blank page "with ddress ........../get.php?download . Please note that the link contains spaces, so I tried both with spaces and with "%20". And when I tried to enter multiple id's (links), I get this error.:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in .... get.php on line 4.

  7. #7
    Join Date
    Jun 2007
    Posts
    16
    Sorry about that.

    Seems there was an error in my coding, however, please use what JDM had suggested, as I know that will work for sure.

    If you need guidance on how to use what he had suggested, here you go:

    Change the link I gave you to:
    Code:
    <a href="get.php">Download</a>
    And on get.php, erase everything and put in there:
    Code:
    <? 
    $content_len=@filesize("file.zip"); 
    Header("Content-type: application/zip"); 
    Header("Content-type: octet-stream"); 
    Header('Content-Disposition: attachment; filename="file.zip"'); 
    if($content_len!=FALSE) 
    { 
    Header("Content-length: $content_len"); 
    } 
    readfile("file.zip"); 
    ?>
    Now, each place that you see file.zip in the code above, change it to the url to your file.

    That should do you just fine.

    Enjoy!

  8. #8
    Join Date
    Jun 2006
    Posts
    18
    Thank you so much for your help, just two more questions before trying that out:

    1. How can I use the "get.php" file to download multiple files by calling "get.php?f=1" or "get.php?f1" .

    2. The files I will be using are mp3, not zip, so shouldn't I change
    Code:
    Header("Content-type: application/zip"
    to something else?

    Thanks

  9. #9
    Join Date
    Jun 2007
    Posts
    16
    Glad to be of help!

    To get multiple files, you need this script:
    Code:
    <?
    
    $id = $_GET['id'];
    
    if ($id = "1") {
    $filename = "file1.mp3";
    }
    
    if ($id = "2") {
    $filename = "file2.mp3";
    }
    
    if ($id = "3") {
    $filename = "file3.mp3";
    }
    
    if ($id = "4") {
    $filename = "file4.mp3";
    }
    
    $conttype = mime_content_type($filename);
    
    $content_len=@filesize($filename); 
    Header("Content-type: ".$conttype); 
    Header("Content-type: octet-stream"); 
    Header('Content-Disposition: attachment; filename=$filename'); 
    if($content_len!=FALSE) 
    { 
    Header("Content-length: $content_len"); 
    } 
    readfile($filename); 
    ?>
    This takes care of the mp3 thing too.

    As you can see above, there is one thing that repeats:
    if ($id = "4") {
    $filename = "file4.mp3";
    }
    but with a different number.

    When you want to add another file, use:
    if ($id = "#") {
    $filename = "file#.mp3";
    }
    and change the # signs accordingly.

    To get the different files, use this link:
    Code:
    <a href="get.php?id=1">File 1</a>
    ...
    <a href="get.php?id=4">File 4</a>
    And that should take care of you!

    Hope that wasn't too confusing!

  10. #10
    Join Date
    Jun 2007
    Posts
    16
    Wait!

    I made an error!

    Please don't use it yet!

    I am correcting it now!

  11. #11
    Join Date
    Jun 2007
    Posts
    16
    OK.

    Here's the spiffeh:

    I've got a working script, but it involves renaming your files.

    Now, this script may not be what you want, provided the renaming, and if not, I'm sure that someone else can develop one/there may be one that exists.

    Here is what I have:
    Code:
    <?
    
    $id = $_GET['id'];
    
    $filename = "download/file_".$id.".mp3";
    
    header("Content-disposition: attachment; filename=".$filename.""); 
    
    header('Content-type: application/octet-stream');
    
    readfile($filename); 
    
    ?>
    That should be your get.php file.

    The files are in a subfolder called download and each file is named file_#.mp3 with the # sign being the ID number.

    To say, if I have file_1.mp3, file_2.mp3, and file_3.mp3 and I want to download file_2.mp3, then I go to http://www.domain.com/get.php?id=2 and it will grab the file_2.mp3 from inside of the download folder, which remains unseen.

    I'll look and see what other scripts I can find. My friend developed one similar to this and I think that it would work perfect. Until I can, this could serve your purpose.

  12. #12
    Join Date
    Jun 2006
    Posts
    18
    Thanks mmumpower, btw, what is wrong with the previous code you posted?, I've some programming knowledge (not of php of course), and what I can make out from that is, it gets variables contents, and if it matched with the specified string, it sets the other variable "filename" to specific string. then calls it. The renaming part is a bit problem for my case, , so I'll wait for your (or someone else's) next post.

    Thank you again

  13. #13
    Join Date
    Jun 2007
    Posts
    16
    mime_content_type($filename); doesn't work, for some odd reason.

    So once I worked around that, it wouldn't recognize the different file names.

    I did some googling, checking my steps, and I realized I used all if statements instead if one if and the rest elseif and it still wouldn't work.

    I tried every combination myself and google could come up with but I couldn't get it to work.

    I'm at work now and it's hard to devote full attention to the forums, but am head home in a few moments. I'll be sure to check it out again once I get home because that was just a temporary work-around if you had a small collection.

    But have no fear, I'll check it!

  14. #14
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Quote Originally Posted by mmumpower

    Code:
    <?
    
    $id = $_GET['id'];
    
    $filename = "download/file_".$id.".mp3";
    
    header("Content-disposition: attachment; filename=".$filename.""); 
    
    header('Content-type: application/octet-stream');
    
    readfile($filename); 
    
    ?>
    Quote Originally Posted by mrcoder
    Send the correct headers for the file, then output the file contents to the user using readfile("file_to_read.blah");
    Just like I said in the first reply

  15. #15
    Join Date
    Jun 2006
    Posts
    18
    Can anyone please help me on this one.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles