开发者

Wise to grab uploaded files contents from its temporary location?

开发者 https://www.devze.com 2023-03-22 19:39 出处:网络
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 locatio

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 fi开发者_开发技巧ne on localhost, but I'm afraid there could be some permission-issues etc on hosting? Any help is appreciated.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号