简体   繁体   中英

How to URL Link to file outside root directory

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM