简体   繁体   中英

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? In the following code, $fileContentsString is the contents of a phar file that was loaded from the network. 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.

$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. Opening another file handle on php://temp will give you another part of the memory, and thus a new "file".

You need a memory-only file system like PEAR's Stream_Var package which lets you access variables as a file system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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