简体   繁体   English

从内存中读取phar文件?

[英]Read phar file from memory?

Is there a way that I can access a file from a phar without having to save the phar to disk first? 有没有一种方法可以从phar访问文件而不必先将phar保存到磁盘? In the following code, $fileContentsString is the contents of a phar file that was loaded from the network. 在以下代码中, $fileContentsString是从网络加载的phar文件的内容。 How can I take that string and read a file from it without having to save the string to disk as a file first? 如何获取该字符串并从中读取文件,而不必先将该字符串作为文件保存到磁盘? I've tried writing the string to php://temp and then using Phar::loadPhar to read it, but that fails. 我尝试将字符串写入php://temp ,然后使用Phar::loadPhar读取它,但是失败了。

$tmpFilePath = TMP_PATH . '/' . $filename;

// Save the file to TMP_PATH
$fp = fopen($tmpFilePath, 'w');
fwrite($fp, $fileContentsString);
fclose($fp);

$contents = file_get_contents('phar://' . $tmpFilePath . '/json.txt');

php://temp is a stream that can be used to store data in memory - but it can be only used via a file handle, not as a file system. php://temp是可用于将数据存储在内存中的流-但只能通过文件句柄使用,而不能作为文件系统使用。 Opening another file handle on php://temp will give you another part of the memory, and thus a new "file". php://temp上打开另一个文件句柄将为您提供另一部分内存,从而获得一个新的“文件”。

You need a memory-only file system like PEAR's Stream_Var package which lets you access variables as a file system. 您需要一个仅内存的文件系统,例如PEAR的Stream_Var软件包 ,该软件包使您可以将变量作为文件系统进行访问。

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

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