从临时位置获取上传的文件内容是否明智?
我正在构建一个系统,用户可以在其中上传文件作为数据(不是永久存储,只是内容)。现在我想知道从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
获取数据的唯一机会是读取
'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.这是普遍接受的方法。假设 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.
您可以使用
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.
根据我的理解,您几乎可以保证 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.