getting a file from outside the document root. PHP

2019-09-08 10:59发布

I am currently storing docx files and pdf files in a user upload folder outside the doc root. I intend for these files to be downloaded via a db link to the heavily scrambled file name.

Previously I have only obtained data from files outside the root with PHP - is it possible to retrieve whole files from this area and if so how does one go about it.

2条回答
我想做一个坏孩纸
2楼-- · 2019-09-08 11:15
<?php
$get_file=$_GET['file_name'];
$file = '/path/to/uploads/'.$get_file;

if (file_exists($file)) {
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

After many hours of searching I found this that seems to work. Unfortunately, although the symbolic link root seemed to be a good path to follow I am unsure of how to actually implement it - firebug goes into quirks mode when I try even the most basic script.

查看更多
闹够了就滚
3楼-- · 2019-09-08 11:26
<?php

$file_id = $_GET['id'];

$local_path = get_real_filelocation_from_id($file_id);

readfile($local_path);

The code for get_real_filelocation_from_id() is left as an exercise for the OP.

查看更多
登录 后发表回答