apache 直接解析文件上传,无需写入文件系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1381295/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:08:16  来源:igfitidea点击:

Parse file upload directly without writing to the file system

phpapachefile-upload

提问by jimyi

With Apache/PHP5, is it possible to get the contents of an uploaded file directly without having it written to the file system?

使用Apache/PHP5,是否可以直接获取上传文件的内容而无需将其写入文件系统?

Not much on Google about this, but it appears that files are always written to a temporary directory once they are uploaded.

谷歌上对此并不多,但似乎文件在上传后总是被写入临时目录。

回答by Gavrisimo

Not sure i understand you, but i will try to answer.

不确定我是否理解你,但我会尽力回答。

http://www.w3schools.com/PHP/php_file_upload.asp

http://www.w3schools.com/PHP/php_file_upload.asp

What you can learn here is that every file is uploaded to php's temp directory. Then it is up to your script to move/copy that file to some permanent web accessible directory, because file that was uploaded to php's temp dir is deleted after the script end executing.

您可以在这里了解到每个文件都上传到 php 的临时目录。然后由您的脚本将该文件移动/复制到某个永久的 Web 可访问目录,因为在脚本结束执行后,上传到 php 的临时目录的文件将被删除。

回答by Imagist

On Linux you can create filesystem partitions in memory. If you can ensure that the uploaded file is written to the partition in memory, if will be stored in memory but act as if it were stored in the filesystem, making both Apache/PHP5 and you happy.

在 Linux 上,您可以在内存中创建文件系统分区。如果你能保证上传的文件被写入内存中的分区,if 将被存储在内存中,但就像存储在文件系统中一样,让 Apache/PHP5 和你都开心。

The problem is that anysolution which writes files to memory rather than to the filesystem requires severe limitations on the size and quantity of the files. You will see a speed boost by avoiding writing to disk, but if you use enough memory to push other data into the pagefile or swap, your "optimization" will become counterproductive very quickly.

问题是任何将文件写入内存而不是文件系统的解决方案都需要对文件的大小和数量进行严格限制。通过避免写入磁盘,您将看到速度提升,但如果您使用足够的内存将其他数据推送到页面文件或交换中,您的“优化”将很快适得其反。

回答by inakiabt

You can:

你可以:

<?php
if (!empty($_FILES))
{
    echo "<pre>";
    print_r($_FILES);
    echo file_get_contents($_FILES['file']['tmp_name']);
    echo "</pre>";
}
?>
<form id="myForm" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Send" />
</form>

Output:

输出:

Array
(
    [file] => Array
        (
            [name] => mytextfile.txt
            [type] => text/plain
            [tmp_name] => D:\PHP\wamp\tmp\php1283.tmp
            [error] => 0
            [size] => 1473
        )

)
My text file My text file My text file My text file My text file My text file 
My text file My text file My text file My text file 
My text file My text file My text file My text file My text file My text file 
My text file My text file My text file 
My text file My text file My text file My text file My text file 

I don't know if it's restricted by some php.ini variable.

我不知道它是否受到某些 php.ini 变量的限制。