PHP File Transfer

我只是一个虾纸丫 提交于 2019-12-12 04:04:37

问题


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


来源:https://stackoverflow.com/questions/36485594/php-file-transfer

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