Home  >  Article  >  Backend Development  >  使用 PHP 将文件上传到内存

使用 PHP 将文件上传到内存

WBOY
WBOYOriginal
2016-06-06 20:50:201247browse

PHP处理文件上传通常使用

<code class="lang-php">move_uploaded_file( $file['tmp_name'], $new_file )
</code>

, 将上传到服务器的临时文件命名到指定的目录下。

那么,有没有办法让PHP把上传的文件流直接写入到内存里,而不是先在磁盘上生成一个临时文件? (我这么做是因为程序运行的环境不允许本地磁盘写入)

回复内容:

PHP处理文件上传通常使用

<code class="lang-php">move_uploaded_file( $file['tmp_name'], $new_file )
</code>

, 将上传到服务器的临时文件命名到指定的目录下。

那么,有没有办法让PHP把上传的文件流直接写入到内存里,而不是先在磁盘上生成一个临时文件? (我这么做是因为程序运行的环境不允许本地磁盘写入)

改源码,重新编译,别无他法了..

可以通过设置 php.ini 中的 upload_tmp_dir 来指定文件上传路径。因此,只要把这个路径设置为一个内存的地址即可。对于linux可以使用 /dev/shm ,对于windows可以使用诸如 RamDisk 等软件。

直接用/dev/shm 这个设备,直接就是内存.这个内存的一半大小.

<code>tmpfs                 7.5G     0  7.5G   0% /dev/shm

</code>

<code>$memfile = file_get_contents( $file['tmp_name'] );
</code>

$memfile 就是内存里面的文件 ... php 执行完成之后文件销毁 ...

PHP没有办法传递到内存,这个玩意对PHP别无选择

http://www.qixing318.com/article/php-upload-pictures-rename-6-kinds-of-schemes.html
PHP上传图片重命名6种方案

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn