PHP File Transfer

2019-09-04 06:16发布

问题:

Suppose I have a device that knows the name of a file they want to download from my server.

How can I transfer said file to that device without giving the device access to the file system?

For example, suppose I have a page ping.php which receives a get request for "something.zip"

ping.php knows the location of something.zip (somewhere on the server's file system), but I can't allow the user access to the file system, or allow them to know the location of the file (it even needs to be hidden from somebody using something like wireshark).

How can I solve this problem?

It might be an easy solution, I'm just not extremely well versed in these matters.

If it makes any difference I'll be using an Apache server on a Linux box.

回答1:

You can create a PHP script to facilitate the file transfer while the file is sitting in a folder that is not accessible via the Web. This is how I commonly handle file downloads on my system.

There are any number of sample scripts that you may use to do the actual file transfer. The key is to put that file outside the web-accessible file system.

For completeness, here's some code I've used in the past to do a file download in PHP:

$filename="filetodownload.xyz";
$cf = realpath("/non-webaccessible-folder/".$filename);
$file=$cf;

header('Content-Disposition: attachment; filename="' . basename($cf) . '"');
header("Content-Length: " . filesize($cf));
header("Content-Type: application/octet-stream");
readfile(realpath($cf));