问题描述
我在PhP脚本中使用纯文本文件.有什么方法可以在PhP脚本中处理文件而不将其保存在本地?我看到的每个地方都只是上传文件并保存.我只需要从文件中提取一些名称并使用它即可.如果我使用文件的本地副本,那么其他所有工作都可以进行,因此保存它,然后将其删除即可.我只是想知道是否有一种方法可以跳过保存副本并直接获取该信息的方法.
I am using a plain text file in a PhP script.Is there any way to process the file in the PhP script without saving it locally? 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.
我们在这里上传文件.
<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="";
这篇关于php文件POST上传而不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!