Jump to content

Recommended Posts

Here's the concept.

 

A user registers, receives an email with an URL like http://site.com/download.php?id=#####. The number will be unique to each person that registers.

 

Once the user (or anyone) accesses that download, I will mark that ID in the database and make the link not work anymore.

 

My question is, on the download.php page.. how can I access the download file so that the user can download it, but WITHOUT revealing the URL of the actual file?

 

Thanks

 

Wes

Link to comment
https://forums.phpfreaks.com/topic/108594-unique-hidden-downloads/
Share on other sites

Are you talking about something like this? (i found this online somewhere)

 

<?php
$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)
{
  Header("Content-length: $content_len");
}

readfile("file.zip");
?>

Found this for you

 

<?php

function force_download($filename, $name ) {
      $filesize = filesize($filename);
      if($filesize) {
          ini_set('zlib.output_compression', 'Off');
   
          /* IE FIX : FireFox Compatible */
          header("Content-Type: application/application/octet-stream\n");
          header("Content-Disposition: attachment; filename=\"$name\"");
          /* Read It */
          $contents = fread(fopen($filename, "rb"), filesize($filename));
          /* Print It */
          echo $contents;
          exit;
      } else {
          return 0;
      }
   }

?>

Store file outside of webroot, bring it in using readfile or the function below if the file is large and causing memory errors

 

<?php
function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   }
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile() does.
   }
   return $status;

}
?> 

Found this for you

 

<?php

function force_download($filename, $name ) {
      $filesize = filesize($filename);
      if($filesize) {
          ini_set('zlib.output_compression', 'Off');
   
          /* IE FIX : FireFox Compatible */
          header("Content-Type: application/application/octet-stream\n");
          header("Content-Disposition: attachment; filename=\"$name\"");
          /* Read It */
          $contents = fread(fopen($filename, "rb"), filesize($filename));
          /* Print It */
          echo $contents;
          exit;
      } else {
          return 0;
      }
   }

?>

 

I got an error:

Warning: filesize() [function.filesize]: stat failed for http://site.com/PMSE_2.5.zip in /home/my/path/to/dl.php on line 46

 

 

 

-----------------------------

 

 

 

Store file outside of webroot, bring it in using readfile or the function below if the file is large and causing memory errors

 

<?php
function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   }
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile() does.
   }
   return $status;

}
?> 

 

Well, kind of worked. But instead of downloading the file, it just outputted alot of weird characters.. what should i do to get around this?

I tried it with the second argument being both true and false..

 

 

 

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.