php PHP从上传的文本文件中读取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2201379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP read from uploaded text file?
提问by Dan
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.
如果我通过表单上传文本文件,是否可以直接从 $_FILES 变量输出其内容,而不是先将其保存到服务器上?我知道这是一个安全风险,但它只能在本地机器上运行。
Any advice appreciated.
任何建议表示赞赏。
Thanks.
谢谢。
回答by MANCHUCK
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
一个有用的链接是http://us2.php.net/manual/en/features.file-upload.php
回答by Marek Karbarz
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.
该文件在上传时保存到临时目录,但您可以使用$_FILES['uploadedfile']['tmp_name']它来读取它而无需保存在永久位置。
回答by Teekin
Unfortunately, no. At least not through the $_FILES variable. Sorry.
抱歉不行。至少不是通过 $_FILES 变量。对不起。
EDIT: It is always saved as the temp file in $_FILES and you'll always have to use that one for content.
编辑:它始终保存为 $_FILES 中的临时文件,您将始终必须使用该文件作为内容。

