PHP文件传输 [英] PHP File Transfer

查看:179
本文介绍了PHP文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的设备知道他们要从服务器下载的文件的名称.

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?

例如,假设我有一个页面ping.php,该页面接收到对"something.zip"的获取请求

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

ping.php知道something.zip的位置(服务器文件系统上的某个位置),但是我不允许用户访问文件系统,也不允许他们知道文件的位置(甚至需要)以使用Wireshark之​​类的东西对某人隐藏起来).

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.

如果有什么不同,我将在Linux机器上使用Apache服务器.

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

推荐答案

当文件位于无法通过Web访问的文件夹中时,可以创建PHP脚本来简化文件传输.这就是我通常在系统上处理文件下载的方式.

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.

为完整起见,这是我过去使用PHP进行文件下载的一些代码:

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

这篇关于PHP文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆