Link to home
Start Free TrialLog in
Avatar of Jason92s
Jason92sFlag for Afghanistan

asked on

Accessing Zip Files Outside Public_HTML Folder

I have a need to allow users to download ZIP files that reside outside the Public_HTML folder and found a PHP script that does just that - http://forums.phpfreaks.com/topic/129814-solved-accessing-images-and-files-that-are-located-outside-document-root/

It works great for PDF files but when I put a zip file in, the browser tries to save/open secure.php for some reason.  I shrunk the code down a bit as follow:

Link Page to download
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body >
<a href="secure.php?file=45int.zip">File Linke</a>
</body>
</html>

Open in new window


secure.php file:

<?php
  $folder = realpath('/home/rogersho/tester');
  $file = realpath($folder.'/'.$_GET['file']);
  header(sprintf("Content-type: %s;",getMimeType($file)));
  readfile($file);
  exit;

   function getMimeType ( $filename ) {
    //MIME MAP
$mime_extension_map = array(
    'pdf'           => 'application/pdf',
    'zip'           => 'application/zip'
);
    //Get Extension

    $ext = strtolower(substr($filename,strrpos($filename,'.') + 1));
    if(empty($ext))
      return 'application/octet-stream';
    elseif(isset($mime_extension_map[$ext]))
      return $mime_extension_map[$ext];
    return 'x-extension/' . $ext;
  }
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason92s

ASKER

Oh, now I see what you're saying.  Perfect, works great, thank you.
Great!  Thanks for the points and good luck with the project, ~Ray