I'm trying to see if a directory exists inside a ziparchive using statName and I have come across an odd quirk.
When I generate a zip file in php I add the files in this fashion: $this->zip->addFile($file, $path); where $file is C:\xampp\htdocs\images\uploads\about.png and $path is the internal path images/uploads/about.png. If I try and see if images/uploads/ exists when checking the zip in another routine using statName I get a false response even though the files and directories exist if opened by winzip.
For testing I made a new zip using winzip, and added a folder images and in and then uploads mimicking the above and checked if images/uploads/ existed using statName and it does!
I've made an output of the zip file using a loop and you can clearly see in the ziparchive created zip folder there is no verbatim images/uploads/ directory, yet there are files in those directories if you open the zip and as seen in output. Whereas the winzip one has the directory listed verbatim.
What is preventing the statName from showing up with the ZipArchive created zip file? I can only assume its because the dir is not listed verbatim, but that doesn't make any sense as those directories do exist! Is there something I don't know in the way ZipArchive handles adding files?
Related
I tried downloading a directory folder using wget, and it seems to have worked properly. However all the PHP files are empty. I know that they should not be empty as they show a file size on the web directory.
The folder I am trying to download is here:
https://www.isuperman.tw/wp-content/plugins/automatewoo-referrals/
I used these directions to download recursively with wget.
How to download HTTP directory with all files and sub-directories as they appear on the online files/folders list?
Any ideas on why they are downloading blank/empty?
view-source:https://www.isuperman.tw/wp-content/plugins/automatewoo-referrals/automatewoo-referrals.php
nope the files are empty, doesnt matter whats the content of the files.. if you use wget to download a file, wget simulates A browser and get the parsed php content from the server...
and these seems to be empty
if you want to download the files with php, use ftp or the server must not parse these files and deliver its raw content
I cannot believe this hasn't been asked before, but I cannot find the answer on stackoverflow, please redirect me if this is a duplicate and I'm too retarded to find it.
I am creating a script that will download a kmz file from my custom google map, then will unzip it, read the xml and create a web page that lists all my markers. But I don't have access to the servers to create a writable folder so where can I download the kmz file to then unzip it?
Tried the temp folder (/var/tmp) and it seems to download the file, but I cannot work on the file afterwards or extract it, probably because the file gets deleted right after I call fclose, after downloading, as it doesn't exist anymore if I scan the directory.
UPDATE:
To answer question, the file is immediately deleted because it is a shared server and there are tons of session files in that folder that are created by all the visitors of each websites
I have 16631 files hosted in a webserver, 2719 of them are text files that contains a list of specific files located on the server.
Using PHP, is possible to create a ZIP for each text file?
could be the text file name as ZIP file name?
I want to keep the same directory structure in the ZIP file.
Thanks in advance.
Taking the suggestion from Jeff Hines, you could use the ZipArchive class to create the zip.
In Pseudo code
Get List of Text Files in your folder
Read Text file and get list of files to add to zip
Using ZipArchive add each listed file to your new zip file.
Write flag to specify you've done this file.
You might need to run it on 10 files at a time since it may time out.
Is there a way to add files to a zip file from another server with php's zip extension? ie.
addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)
I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made
use file_get_contents() and ZipArchive::addFromString()
$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));
This writes the contents fetched remotely straight into your php object (no need to write/read temp file)
It's not hard to read remote files in PHP.
file_get_contents("http://example.com/remote.txt");
Or to copy them locally:
copy("http://example.com/remote.txt", "/tmp/local.txt");
Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.
Fetch them with cURL, add them from TEMP directory.
I have designed an online editor with which a user can create files and folders on the server. Suppose a user creates the below files and folders.
/docs/abc.txt
/docs/def.txt
/docs/work/assignment.txt
I want to write a PHP script to combine the files into a single ZIP file so that it makes it easier for the user to download them with one click.
The only purpose is that I should get a single file with the directory hierarchy maintained which the user can download and uncompress on his system to get the original files and directory structure back. I don't care even if the ZIP file is not compressed.
Note that I am using a shared server and do not have access to execute external commands.
ZipArchive class is what you need. Using this you can add directories and file to an archive.
This is a common requirement and has been solved. Try and check out this class: http://www.phpclasses.org/browse/package/2322.html
More tutorials at:
http://davidwalsh.name/create-zip-php <-- this has a similar example if you scroll down
http://www.w3schools.com/php/php_ref_zip.asp
Assuming you have permission to execute an external command, you could use exec to run an external command-line capable ZIP program such as 7-Zip.