How to URL Link to file outside root directory

流过昼夜 提交于 2019-12-25 02:12:54

问题


So i have a file uploader that allows people to upload files to the server and then download them via a URL.

I want to store uploaded files outside of the Root. (One level up from the root in a folder called Uploads)

Code snippet

$server = "http://www.mysite.com";

$name = $_FILES['file']['name'];
$temp = $_FILES['file']['tmp_name'];
$size = $_FILES['file']['size'];

$destination = '../uploads/'. $random;
mkdir($destination);
move_uploaded_file($temp, $destination."/".$name);

$final = $server."/".$destination."/".$name;

This...

$destination = 'Uploads/' . $random ;

Didn't work (File not found error). ($random is defined by the way..)


回答1:


The simple answer is to make a symbolic link to ../Uploads in the web root:

ln -s ../uploads Uploads

Of course this makes the uploads directory web accessible. If you want to prevent the uploads directory from being web accessible, do not make the symlink, but instead add a rewrite rule that redirects requests to /Uploads/ to a php script that will pass the data through, perhaps only after checking for certain conditions (like if the user is logged in).



来源:https://stackoverflow.com/questions/7862307/how-to-url-link-to-file-outside-root-directory

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