Hide Real File Path

Discussion in 'PHP' started by h_j5005, Aug 26, 2008.

  1. #1
    Hello,
    Any body know how can I write a php to read a path and convert that to the virtual link >>

    Orginal Link : domain.com/1.rar
    Convert to >> domain.com/file.php?f=1.rar

    and how can I read from external link ?

    Orginal Link : externaldomain.com/1.rar
    Convert to >> mydomain.com/file.php?f=1.rar
     
    h_j5005, Aug 26, 2008 IP
  2. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #2
    A lot of people use PHP's header tag to force a download, and then the script will close the window using javascript. Take a look at a PHP tutorial website to take a look at the header() function. As for the link just have the page have a click link saying click here to download, and opens domain.com/file.php?f=1.rar into a new window (_blank) and then have file.php use the header() function to force download of the file after retrieving the real path from a MySQL database, and then close the window using javascript after the header tag works its magic.

    That'd be the way i'd approach this little idea. Never tried it, but i've read posts by people that have used the method before.

    E.G database_

    _________
    id || location
    1 || URL TO DOWNLOAD1
    2 || URL to download 2

    file.php

    if($_GET){

    // include connect to mysql and retrieve the URL via the id to the file
    //then use the header() function to force a download to the URL provided
    //finally use a javascript to close the window
    // include the exit function just in case

    } ELSE {

    //just close the window or redirect the user to the index page

    }

    then have the download page

    LINK 1 ( <A href="file.php?id=1" target="_blank">File 1</A>

    etc
     
    Grit., Aug 26, 2008 IP
  3. h_j5005

    h_j5005 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    $filename = "file.rar";
    $myFile = "http://domain.com/file.rar";
    
    $mm_type="application/force-download";
    
    function httpStreamSize($fd) 
    { 
      $meta_data = stream_get_meta_data($fd); 
      foreach($meta_data['wrapper_data'] as $response)  
        if (preg_match('#^Content-Length\s*:\s*(\d+)$#i', $response,$m))  
          return (int)$m[1]; 
      return null; 
    } 
    
    $fd=fopen($myFile,'r');
    $size = httpStreamSize($fd); 
    
    header("Cache-Control: public, must-revalidate");
    header("Pragma: hack"); // WTF? oh well, it works...
    header("Content-Type: " . $mm_type);
    header("Content-Length: " .$size );
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header("Content-Transfer-Encoding: binary\n");
    
    readfile($myFile);
    ?>
    PHP:
    I find that , but not support resume , any body can change that to support resume when user download from download.php ?
    :confused:
     
    h_j5005, Aug 26, 2008 IP
  4. h_j5005

    h_j5005 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    any body ?
     
    h_j5005, Aug 26, 2008 IP
  5. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #5
    is it essential that it supports resume? how big are the files your are hosting?
     
    Grit., Aug 26, 2008 IP
  6. subkid

    subkid Banned

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That can be done, but for as you are streaming the downloaded file, the original URL must also support resume. Also, you need to not only accept resume yourself and provide download from range, but you need to request the file with those variables as well.

    Basically, the request to the remote file must also include the requested range.

    It should be more or less like this:
    <?php
    $filename = "file.rar";
    $myFile = "http://domain.com/file.rar";
    
    $mm_type="application/force-download";
    
    function httpStreamSize($fd) 
    { 
      $meta_data = stream_get_meta_data($fd); 
      foreach($meta_data['wrapper_data'] as $response)  
        if (preg_match('#^Content-Length\s*:\s*(\d+)$#i', $response,$m))  
          return (int)$m[1]; 
      return null; 
    } 
    
    $fd=fopen($myFile,'r');
    $size = httpStreamSize($fd); 
    
    header("Cache-Control: public, must-revalidate");
    header("Pragma: hack"); // WTF? oh well, it works...
    header("Content-Type: " . $mm_type);
    header("Accept-Ranges: bytes");  // Resumable?: YES! - It will tell that to download managers!
    if(isset($_SERVER['HTTP_RANGE']))
    {
    // Resume requested
    list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
    $range = str_replace("-", "", $range);
    $data_left = $size - $range; // Will try to find out the piece left
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $data_left");
    } else {
    // Normal download!
    header("Content-Length: ".$size);
    }
    header('Content-Disposition: attachment; filename="'.$filename.'"');     
    header("Content-Transfer-Encoding: binary\n");
    
    readfile($myFile);
    ?>
    
    PHP:

    Of course, this part must be changed:
    readfile($myFile);
    Code (markup):
    Because now it need to read from the new range ($data_left)
    And we don't want to read it all again if the user just needs 1% don't we? ;)

    Left that task to other since now i'm out of time.

    Bye!
     
    subkid, Aug 26, 2008 IP