简体   繁体   English

php FILE POST上传没有保存

[英]php FILE POST upload without save

I am using a plain text file in a PhP script. 我在PhP脚本中使用纯文本文件。 Is there any way to process the file in the PhP script without saving it locally? 有没有办法在PhP脚本中处理文件而不在本地保存? Everywhere I see simply uploading a file and saving it. 我到处都看到只是上传文件并保存。 I just need to pull some names off of the file and be done with it. 我只需要从文件中提取一些名称并完成它。 I have everything else working if I use a local copy of the file, so saving it, then deleting it works. 如果我使用文件的本地副本,我有其他一切工作,所以保存它,然后删除它的工作原理。 I was just wondering if there was a way to skip the saving a copy and just getting that information directly. 我只是想知道是否有办法跳过保存副本并直接获取该信息。

We upload the file here. 我们在这里上传文件。

<html>
<body>
    <form action="test.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label><input type="file" name="file" id="file" /> <br />
        <input type="submit" name="submit" value="Submit" />
    </form>
</body>
</html>

and in the script working with a copy of the file saved locally, I simply use 并且在脚本中使用本地保存的文件副本,我只是使用

$file = fopen($_FILES['file']['name'], "r");

If its text file (assuming size is not that huge) usually you could read it by 如果它的文本文件(假设大小不是那么大)通常你可以读它

$contents= file_get_contents($_FILES['file']['tmp_name']);

If you are not sure which type of upload it was, check the request method 如果您不确定它的上传类型,请检查请求方法

if(strtolower($_SERVER['REQUEST_METHOD'])=='post')
    $contents= file_get_contents($_FILES['file']['tmp_name']);
elseif(strtolower($_SERVER['REQUEST_METHOD'])=='put')
    $contents= file_get_contents("php://input");
else
    $contents="";

When the form is submitted, check the $_FILES['input_name']['tmp_name'] property. 提交表单时,请检查$_FILES['input_name']['tmp_name']属性。 This is the path to your file saved in a temporary /tmp system path. 这是保存在临时/tmp系统路径中的文件的路径。 You can proceed reading the name using, say, file_get_contents() and then simply forget the file. 您可以使用file_get_contents()继续读取名称,然后只是忘记该文件。 System will take care of removing it. 系统将负责删除它。


Just to stand out of the other answers, you could theoretically read the file without even uploading it using JavaScript, see http://www.html5rocks.com/en/tutorials/file/dndfiles/ . 只是为了脱离其他答案,理论上你可以在不使用JavaScript上传文件的情况下阅读文件,参见http://www.html5rocks.com/en/tutorials/file/dndfiles/ Then submit only the data you need as part of AJAX request. 然后仅提交您需要的数据作为AJAX请求的一部分。

The file has to be saved somewhere before reading in this case is the temporary directory; 在这种情况下读取文件是临时目录之前,必须将文件保存在某处; You can get the contents of the file from the temporary directory, then if you really need to you delete. 您可以从临时目录中获取文件的内容,然后如果您确实需要删除。 See the following code: 请参阅以下代码:

$file = file_get_contents($_FILES["ufiley"]["tmp_name"]);

unlink($_FILES["ufiley"]["tmp_name"]); 

//contents would be stored in $file

When you upload a file, before you save it locally it get's saved to a temporary file. 上传文件时,在本地保存文件之前,它会保存到临时文件中。 The location of which can be accessed by: 其位置可通过以下方式访问:

$_FILES['uploadedfile']['tmp_name'] 

You can choose not to save the file and fopen() the temporary file, as long as you do this within the same script that recieves the POST . 您可以选择不保存文件和fopen()临时文件,只要您在收到POST的同一脚本中执行此操作即可。

不,它必须保存才能被读取,我通常的常规与你的相同,上传,阅读和删除。

You could just use $_FILES["file"]["tmp_name"] which would be the temp file that was uploaded. 你可以使用$_FILES["file"]["tmp_name"] ,这将是上传的临时文件。 Once your script is done running, PHP should even clean up after itself and delete the temp file. 一旦脚本运行完毕,PHP甚至应该自行清理并删除临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM