I have created a PHP script that uploads file and extract it and get folder name inside the zip and insert its name and path into db. However it doesn't extract the folder name from zip archive and doesn't gets path name like I want. It extracts all direcotories and file names I just want directory names. Lets say I have zip file called abc.zip and it contains folders like this
a ->b->c
ab ->bc
What I want is to be able to get only parnet directory names like a and b currently it gets all directory names and file names.
$zip = new ZipArchive;
$zip->open($filen);
$zip->extractTo($path);
$zip->close();
$zip = new ZipArchive;
if ($zip->open($filen) === true) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$fileinfo = pathinfo($filename);
$db->con->query("Insert into files (rand) values ('$filename')");
}
$zip->close();
}
You could do:
$fileinfo = pathinfo($filename,PATHINFO_DIRNAME);
$db->con->query("Insert into files (rand) values ('$fileinfo')");
Related
i need to create a zipFile with a childs zipFiles already exist :
> folder
- file1.zip
- file2.zip
I want to get a zip file containing all the zip files in the folder.
$dir = "path/to/my/dir";
$finder = new Finder();
$zip = new \ZipArchive();
/***** I create a empty zip file ****/
$filesystem->dumpFile("$dir/delivrables.zip",'');
if($zip->open('delivrables.zip', ZipArchive::CREATE) === TRUE) {
foreach ($finder->in($dir) as $file) {
$zip->addFile($file->getPath(), $file->getFilename());
}
}
I don't get error but i can't extract zip file.
You can create a zip file with zip files in the same way as you create a zip file with images, documents etc
I need to download all files form url and download as a zip. Every thing is working but i am not able to rename the file names under zip. I am using below code
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
$error = "* Sorry ZIP creation failed at this time";
}else{
foreach ($_POST as $file) {
foreach($file as $res){
$download_file = file_get_contents($res);
$zip->addFromString(basename($res), $download_file);
}
}
$zip->close();
}
can anyone please help me how can i rename the files?
In your code, you use the line
$zip->addFromString(basename($res), $download_file);
...that means: add the downloaded file under it's name to the archive. If you want to change the file name that should occur in the archive, you should start looking here
I need to create a Zip file in a specific file path without any sub folders created inside it,
here is my code :
$files = array('article/includes/pdfFiles/file1.txt','article/includes/pdfFiles/file2.txt');
$zipname = 'article/includes/pdfFiles/file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
I need the file.zip to be created in the path 'assets/uploads/pdfFiles' in this folder, its been created but the problem is inside the zip file these folders exits: assets/uploads/pdfFiles
How can I create the zip file without these folders?
Your code above doesn't work because you don't show the contents of $fileNames but I am assuming it is something like:
array('assets/uploads/pdfFiles/file1.txt','assets/uploads/pdfFiles/file2.txt',...)
When you add to the zip do this:
foreach ($fileNames as $fullpath) {
// pull just the file name from the path
$filename = substr($fullpath, strrpos($fullpath, '/')+1);
$zip->addFile($fullpath,$filename);
}
I have this file that gets downloaded at:
DownloadFile($reportDownloadUrl, $DownloadPath);
But it's a zip file. Inside of it, a CSV file gets created with a random name i.e random_name.csv
How do I extract this folder abc.zip in php and rename this file with random name to new_name.csv
Problem is that I can't use
$zip->renameName('currentname.csv','newname.csv');
since I don't have currentname.
This code inspects the file in zipLocation then iterates over them to check if there are csv files. If it finds something it copies inside the directory with its original name, then copies another copy with a new name.
$zipLocation = "path/to/file.zip";
$zip = new ZipArchive;
if ($zip->open($zipLocation) === true) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
if (pathinfo($filename, PATHINFO_EXTENSION)=="csv"){
$fileinfo = pathinfo($filename);
copy("zip://".$zipLocation."#".$filename, "./newname.csv");
copy("zip://".$zipLocation."#".$filename, "./".$fileinfo['basename']);
}
}
$zip->close();
}
I have the following code, which as you can see i use to create a new directory then unzip a file.
<?php
function unzip_to_s3() {
// Set temp path
$temp_path = 'wp-content/uploads/gravity_forms/1-9e5dc27086c8b2fd2e48678e1f54f98c/2013/02/tmp/';
// Get filename from Zip file
$zip_file = 'archive.zip';
// Create full Zip file path
$zip_file_path = $temp_path.$zip_file;
// Generate unique name for temp sub_folder for unzipped files
$temp_unzip_folder = uniqid('temp_TMS_', true);
// Create full temp sub_folder path
$temp_unzip_path = $temp_path.$temp_unzip_folder;
// Make the new temp sub_folder for unzipped files
if (!mkdir($temp_unzip_path, '0755', true)) {
die('Error: Could not create path: '.$temp_unzip_path);
}
// Unzip files to temp unzip folder, ignoring anything that is not a .mp3 extension
$zip = new ZipArchive();
$filename = $zip_file_path;
if ($zip->open($filename)!==TRUE) {
exit("cannot open <$filename>\n");
}
for ($i=0; $i<$zip->numFiles;$i++) {
$info = $zip->statIndex($i);
$file = pathinfo($info['name']);
if(strtolower($file['extension']) == "mp3") {
file_put_contents(basename($info['name']), $zip->getFromIndex($i));
} else {
$zip->deleteIndex($i);
}
}
$zip->close();
}
unzip_to_s3();
?>
The unzip code was courtesy of #TotalWipeOut from one of my other posts. It currently unzips just mp3 files to my base directory, but i want to put them in my newly created folder.
I'm very new to PHP so have been trying my best with this, but i can't figure out how to change the file_put_contents(basename($info['name']), $zip->getFromIndex($i)); line to get it to put the files in my new folder?
As Marc B. mentioned you need to include the path to the directory you are putting the file in.
using your code:
file_put_contents($temp_unzip_path."/".basename($info['name']), $zip->getFromIndex($i));
I would also suggest reading a little more about the basics of PHP.