Obsfuscate download URL.

Discussion in 'PHP' started by blueparukia, Jul 19, 2010.

  1. #1
    OK, I have a directory and file within it I want people to download:

    /directory/file.ext

    I wanna have this as a file download, and am using the following code, which is a modified version of someone elses. It returns the download I want, but just an empty file.
    
    		$fullPath = '/directory/file.ext';
    		$fd = file_get_contents($fullPath);
    		$fsize = filesize($fullPath);
    		$path_parts = pathinfo($fullPath);
    		//$ext = strtolower($path_parts["extension"]);
    		
    		header("Content-type: application/octet-stream");
    		header("Content-Disposition: attachment;filename=\"".$path_parts["basename"]."\"");
    		header("Content-length: $fsize");
    		header("Cache-control: private"); //use this to open files directly
    		echo $fd;
    		exit;
    PHP:
    Somethings going to be wrong and I'm guessing it's the file_get_contents and echo $fd thing.

    Thank you.
     
    blueparukia, Jul 19, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    why not use basename function instead of $path_parts = pathinfo($fullPath);
     
    gapz101, Jul 19, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
      $file = '/directory/file.ext';
      header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename=' . basename($file));
      header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file));
      ob_clean();
      flush();
      readfile($file);
      exit;
    ?>
    PHP:
     
    danx10, Jul 19, 2010 IP
  4. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #4
    Thank you, worked fine :)
     
    blueparukia, Jul 19, 2010 IP