Hiding direct download link path from the browswer

5 replies
Hello people

I have made a link which download file from my server after login into the system. But when someone right clicks and copy link it it will give path of file so when they paste same link to browswer without login file is being downloaded, how can I avoid it?
#browswer #direct #download #hiding #link #path
  • Profile picture of the author skillboyz
    <?php
    $id = $_GET['id'];
    $links = array(
    "dl" => "http://www.domains.com/path/to/file.zip"
    );

    header("Location:".$links[$id]);
    exit;
    ?>

    Save this file as mydown.php and when you give hyperlink type this

    <a href="mydown.php?download">Download</a>
    {{ DiscussionBoard.errors[8302064].message }}
  • Profile picture of the author webmarketingrock
    I have tried it but blank page is displayed and its not downloading file at all please look into the matter
    {{ DiscussionBoard.errors[8302068].message }}
    • Profile picture of the author RobinInTexas
      Try this instead

      Code:
      <?php
      header('Location: http://www.domains.com/path/to/file.zip');
      exit;
      ?>
      Save this file as mydown.php and when you give hyperlink type this

      <a href="mydown.php?download">Download</a>
      Signature

      Robin



      ...Even if you're on the right track, you'll get run over if you just set there.
      {{ DiscussionBoard.errors[8302492].message }}
  • Profile picture of the author sktthemes
    Hello

    This will force a file to download cross browser, and will hide the original path of the file in PHP, do you can create counters of how many times this is downloaded or if someone has paid to get access to this file for example.
    Set two variables filePath and fileName and that's is it!, also has special handle to for PDF files.




    <?php


    if(substr($filePath,-1)!="/") $filePath .= "/";

    $pathOnHd = $filePath . $fileName;

    if ($download = fopen ($pathOnHd, "r")) {
    $size = filesize($pathOnHd);
    $fileInfo = pathinfo($pathOnHd);
    $ext = strtolower($fileInfo["extension"]);

    switch ($ext) {
    case "pdf":
    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=\"{$fileInfo["basename"]}\"");
    break;
    default;
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"{$fileInfo["basename"]}\"");
    }

    header("Content-length: $size");

    while(!feof($download)) {
    $buffer = fread($download, 2048);
    echo $buffer;
    }
    fclose ($download);
    }
    exit;
    ?>



    Example:
    filePath = "/myprivatedirectory/www.zriel.com";
    fileName = "Zriel.pdf";
    {{ DiscussionBoard.errors[8313447].message }}
    • Profile picture of the author jeremywallis
      I tried the above approach and it works great for a PDF file.

      But an XLSX file, whilst it appears to download, will not open. Says either the extension is wrong or the file is corrupt.

      And when I tried with a ZIP file, the file file appears to download, shows correct size etc, but is empty when I open it.

      I have tried changing the document type for the appropriate file types but no change in outcome.

      Anyone any ideas?

      Thanks,

      Jeremy.
      {{ DiscussionBoard.errors[11047862].message }}

Trending Topics