PHP read from uploaded text file?

China☆狼群 提交于 2019-12-03 02:41:00

问题


If I upload a text file via a form, is it possible to output its contents directly from the $_FILES variable rather than saving it onto the server first? I know this is a security risk, but it will only be run on a local machine.

Any advice appreciated.

Thanks.


回答1:


The file is saved to temp directory the moment it's uploaded, but you can use $_FILES['uploadedfile']['tmp_name'] to read it without having to save in a permanent place.




回答2:


Doing

file_get_contents($_FILES['uploadedfile']['tmp_name']); 

is valid however you should also check to make sure that the file was uploaded through a form and that no errors occurred during upload:

if ($_FILES['uploadedfile']['error'] == UPLOAD_ERR_OK               //checks for errors
      && is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) { //checks that file is uploaded
  echo file_get_contents($_FILES['uploadedfile']['tmp_name']); 
}

A helpful link is http://us2.php.net/manual/en/features.file-upload.php




回答3:


Unfortunately, no. At least not through the $_FILES variable. Sorry.

EDIT: It is always saved as the temp file in $_FILES and you'll always have to use that one for content.



来源:https://stackoverflow.com/questions/2201379/php-read-from-uploaded-text-file

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