getting a file from outside the document root. PHP

浪子不回头ぞ 提交于 2019-12-13 04:17:13

问题


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.


回答1:


<?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.




回答2:


<?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.



来源:https://stackoverflow.com/questions/11398109/getting-a-file-from-outside-the-document-root-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!