从临时位置获取上传的文件内容是否明智?

发布于 2024-11-26 09:57:57 字数 202 浏览 1 评论 0原文

我正在构建一个系统,用户可以在其中上传文件作为数据(不是永久存储,只是内容)。现在我想知道从 PHP 提供的临时位置获取文件内容是否明智,如下所示:

file_get_contents($_FILES['file']['tmp_name'])

在本地主机上工作正常,但我担心托管上可能存在一些权限问题等?任何帮助表示赞赏。

I'm building a system, where user can upload a file as data (not for permanent storing, just the contents). Now I'm wondering whether it's wise to grab the files contents from its temporary location provided by PHP, like this:

file_get_contents($_FILES['file']['tmp_name'])

Works fine on localhost, but I'm afraid there could be some permission-issues etc on hosting? Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界等同你 2024-12-03 09:57:57

获取数据的唯一机会是读取'tmp_name' 变量中给出的文件。没有其他办法可以做到这一点。 PHP 会为您将文件放在那里,因此您可以放心它是可读的。如果不是,则服务器配置已损坏。

The only chance for you to get the data is by reading the file given in the 'tmp_name' variable. There's no other way to do it. PHP puts the file there for you, so you can be assured it's readable. If it's not, the server configuration is broken.

生生漫 2024-12-03 09:57:57

这是普遍接受的方法。假设 upload_tmp_dir 位置是世界可读/可写的位置(必须接受上传),您将能够在上传后读取它。

That's the generally accepted way to do it. Assuming the upload_tmp_dir location is one that is readable/writable by the world (which it would have to be to accept the upload) you will be able to read it once it is uploaded.

岁月如刀 2024-12-03 09:57:57

您可以使用 upload_tmp_dir 配置上传文件的位置 指令,这样您就知道您不会收到任何权限错误。但是,您无法在运行时使用 ini_set 进行设置。

如果您想将上传的文件移动到新位置,您应该使用move_uploaded_file

You can configure the location of the uploaded files with the upload_tmp_dir directive, so you know that you won't get any permission errors. However, you cannot set this at run-time using ini_set.

If you want to move the uploaded file to a new location, you should use move_uploaded_file.

迷乱花海 2024-12-03 09:57:57

根据我的理解,您几乎可以保证 tmp 目录中的文件至少在 PHP 进程终止之前都将处于活动状态,但是我会使用 fopen() 而不是 file_get_contents。

关于权限,该 PHP 进程将从标头写入该文件,因此您可以 100% 保证同一进程也具有读取访问权限。

From my understanding you could pretty much guarantee the files in the tmp directory will be alive at least until that PHP process dies, however i'd use fopen() rather than file_get_contents.

Regarding the permissions, that PHP process will have written the file there from the headers so you can 100% guarantee the same process also has read access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文