I am using the ZipArchive to unzip a zip file:
This file is a zipped up folder and i want to:
Make sure that the content consists of just a folder (obviously with files in side)
Get the name of the unzipped folder so i can then rename it if needs be?
But having trouble getting the folders name after the unzip? I know i could get the name of the zip file as most of the time when zipped it the same name but some people also change the zipped file name.
If you know for sure there's only one dir in the zip you can just do this:
$dir = trim($zip->getNameIndex(0), '/');
Otherwise, you'll have to loop over all the files and somehow figure out which one of them is the one you want:
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
}
Related
I want to upload an mp3 file and add it into a directory on my server. I am executing the php code from the server directory I want to add the file to. This is what I have so far:
$dir=basename(dirname(__FILE__));
$folder = dirname(dirname($_SERVER['SCRIPT_NAME']));
$file_name = $_FILES['upload']['name'];
//remember to remove this line
$file_name = "test.mp3";
$add_file[0] = "<a
href='//mydomain.com$folder/$dir/$file_name'>$file_name</a>";
$path = "https://nerjabible.com$folder/$dir/";
so I have $add_file[0] and I have the $path to append to. I have tried put and write append, but they all seem to work by opening a file, I am already in the directory, I just want to append my linked file to the existing links in the directory.
The mp3 is in a directory on my PC as a hyperlink playable file and I am using a file upload script to obtain the file which ends up in $file_name but as a non linked file name, so i want to append this into the same directory the script is running from, but as a linked file.
printscreen of the local and remote directories
How can i get the zipped file folder name after unzipping in php.
$zip = new \ZipArchive();
$zip->open(storage_path('app/'.$request->vrfile));
$zip->extractTo(public_path('tour_videos/videos/'.str_slug($company)));
$zip->close();
unlink(storage_path('app/'.$request->vrfile));
Zipped file is called Kigali Home Web Tour
And After Unzipping the file the folder name is Web Tours
How can i get the folder name after extracting zipped files??
$name = $zip->open(storage_path('app/'.$request->vrfile));
$name will give you an absolute path as well as the name of your folder.
My file structure in zip is
lib
css
js
app1
app2
index.php
All the above files and folders are in test.zip
i want to extract this test.zip using php
$zip = new ZipArchive;
$res = $zip->open("test.zip");
if ($res === TRUE)
{
$zip->extractTo('path');
$zip->close();
}
if lib folder is already exist in the directory where we are unzipping then except lib(folder) all other files and folders should get override.
how to solve this.
http://php.net/manual/en/ziparchive.open.php , on open, pass in the over write flag.
I have a script which allows users to upload a ZIP folder. Some users select individual files, and ZIP them up so when uncompressed there are multiple files, however some ZIP entire folders, so when they're uncompressed you just get one folder.
What I want to do, is check if the only contents of an unzipped folder is another folder, and if it is, move all the files from that folder down into the previous one, and delete the original folder.
What would the best way of doing this be?
In other words, you want to flatten the directory structure of a zip archive. There are lots of ways to do this.
Unzip, then flatten folder structure
One would be to unzip the zip archive and then use a DirectoryIterator to walk files and folders.
If a folder is detected, move/copy it's file content to the extraction dir and delete the folder afterwards. That would flatten the dir structure with every iteration on a folder.
https://stackoverflow.com/a/17087268/1163786
ZipArchive - unzip without folder
Another approach would be to unzip the zip archive, without taking care about the paths stored in it, like so:
$path = 'zipfile.zip'
$zip = new ZipArchive;
if ($zip->open($path) === true) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$fileinfo = pathinfo($filename);
copy("zip://".$path."#".$filename, "/your/new/destination/".$fileinfo['basename']);
}
$zip->close();
}
unzip -j or 7z -e
Finally, it's tagged as PHP question, but:
If you use unzip on the CLI, then use the -j option.
Ìt stands for "junk paths", meaning that the archive's directory structure is not recreated, when unzipping and all files are stored in the extraction directory (by default, the current one).
It's e (to ignore paths), instead of x (with full paths) on 7z. http://sevenzip.sourceforge.jp/chm/cmdline/commands/extract.htm
I'm having some issues, I'm using PCLZip to create an archive. I don't get any errors, the zip file is created, but when I go to view it, the archive is empty and on my windows machine I get an error "The Compressed (zipped) folder "local directory zip file") is invalid. I have the following code:
$dir = '../downloads/liability/';
$archive = new PclZip($dir.'archive.zip');
$v_list = $archive->create($dir);
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
My directory structure is:
-admin
--liabilityDev.php (where the above code resides)
--index.php
--commission.php
-downloads
--liability
---one.pdf
---two.pdf
The end result is that in the liability folder, there is a file called archive.zip which contains the 2 pdf's but I get the invalid error.
If I don't have the directory variable, I archive index.php and commission.php and that works fine. It leads me to believe it may be a permission issue, but I'm running on fumes now. Please help!
You can try this:
if(extension_loaded('zip')){
$zip = new ZipArchive();
if($zip->open('../downloads/liability/archive.zip', ZIPARCHIVE::CREATE)===TRUE){
$zip->addFile('path of any normal file to be add into zip');
}
$zip->close();
}
I think, this will fullfill your need. Before implementing this code, please check first that, zip extension is already loaded or not.