I'm trying to read a file content from a zip file with ZipArchive with the method getFromName. My file name has a double dot in it ( ..
). Here's my code:
$zip = new ZipArchive();
$zip->open('book.zip');
$content = $zip->getFromName('book/html/../README.md');
var_dump($content);
The structure of my zip file is this:
Archive: book.zip
Length Date Time Name
--------- ---------- ----- ----
0 2016-06-01 11:09 book/
0 2016-06-01 11:02 book/html/
1855 2016-06-01 11:02 book/html/composer.json
2354 2016-06-01 11:09 book/README.md
--------- -------
4209 4 files
My PHP script is in the same directory as my zip file. When I try to list the file in the folder that I have zipped with Linux command like this:
ls -l book/html/../README.md
It works perfectly. I don't understand why ZipArchive can't read this relative path correctly.
Note : I know that I can read the file with the absolute path: book/README.md
but I really need to use the relative path with ..
because the file paths are loaded dynamically from another place and I don't have a control on them.
I spent a day or so on a similar issue trying to read relative file paths including ".." change directories. ZipArchive is unable to handle those paths if the file is not extracted. The best solution I came up with without having to extract the Zip, was to use the following function to get the full path for the file you want: http://php.net/manual/en/function.realpath.php#84012 . After getting the full file path, ZipArchive::getFromName() will be able to find the file you're looking for.
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.