I have the next folder with files:
2147485934: {image.ai, image.jpg, image.psd}
And I want to create a new zip with thoses files:
<?
$zip = new ZipArchive;
$zip->open('newPack.zip', ZipArchive::CREATE);
foreach (glob("2147485934/*") as $file) {
$zip->addFile($file);
}
$zip->close();
?>
It works, but the new zip file have the same structure:
newPack.zip --> 2147485934: {image.ai, image.jpg, image.psd}
and I want to get the next structure inside my zip:
newPack.zip --> image.ai, image.jpg, image.psd //without folder 2147485934
Any help with this problem would be much appreciated
Try this:
$zip->addFile($file, basename($file));
The second argument defines the name of the file inside the archive. The code I added strips the directory name from the filename inside the archive.
Move to the selected folder with chdir:
http://www.php.net/chdir
<?
$zip = new ZipArchive;
$zip->open('newPack.zip', ZipArchive::CREATE);
chdir('2147485934')
foreach (glob("*") as $file) {
$zip->addFile($file);
}
$zip->close();
?>
Related
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);
}
To put it simply, I would like my users to be able to download their website files, so I created a "Download Website" button which uses this script to add all files/folders in their directory which is in the variable $direc and archive those files/folders.
<?
///////// DOWNLOAD ENTIRE WEBSITE:
if(isset($_POST['download_site'])){
// define some basics
$rootPath = '../useraccounts/'.$direc.'';
$archiveName = ''.$direc.'.zip';
// initialize the ZIP archive
$zip = new ZipArchive;
$zip->open($archiveName, ZipArchive::CREATE);
// create recursive directory iterator
$files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
// let's iterate
foreach ($files as $name => $file) {
$filePath = $file->getRealPath();
$zip->addFile($filePath);
}
// close the zip file
if (!$zip->close()) {
echo '<p>There was a problem writing the ZIP archive.</p>';
} else {
echo '<p>Successfully created the ZIP Archive!</p>';
}
}
?>
To my surprise, this code works. Although, there are a few hiccups:
It doesn't automatically force a download of that archive.
It adds the archive in to my main directory rather than moving it to a separate directory of my choice such as site_downloads or deletes it up on completed download.
Are these problems at all fixable or if not, is there a better way to do it so my main directory does not get filled with constant downloads? I guess it will cause a problem once a Archive is created more than once, as it uses the Directory name.
Solved this by using a few different combinations:
<?
///////// DOWNLOAD ENTIRE WEBSITE:
if(isset($_POST['download_site'])){
// define some basics
$rootPath = '../useraccounts/'.$direc.'';
$archiveName = ''.$direc.'.zip';
// initialize the ZIP archive
$zip = new ZipArchive;
$zip->open($archiveName, ZipArchive::CREATE);
// create recursive directory iterator
$files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
// let's iterate
foreach ($files as $name => $file) {
$filePath = $file->getRealPath();
$zip->addFile($filePath);
}
// close the zip file
if (!$zip->close()) {
echo '<p>There was a problem writing the ZIP archive.</p>';
} else {
rename($archiveName, 'user_archives/'.$archiveName.'');
$yourfile = "user_archives/".$archiveName."";
$file_name = basename($yourfile);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));
readfile($yourfile);
ignore_user_abort(true);
if (connection_aborted()) {
unlink('user_archives/'.$archiveName.'');
} else {
unlink('user_archives/'.$archiveName.'');
}
echo '<p>Successfully created the ZIP Archive!</p>';
}
}
?>
This seems to fix all problems.
I want create a zip in a specific Directory. Actually i can create a folder with this code:
function createZip($array)
{
$files = $array;
$zipname = 'ftpack.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
print_r($array);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
But this code create a zip in the principal directory, i want create in the at /zipDownload directory.
Just pass the full path:
$zipname = '/path/to/ftpack.zip';
Also make sure that the folder /path/to is writable by PHP.
I have lots of files in a particular Directory. In a certain PHP page I lists the contents of the particular directory with links to download each item separately. Now I need to display a Link which will ZIP all the contents of that directory so any visitor can download all the contents as a Single ZIP file.
Use ZipArchive for zipping files and RecursiveDirectoryIterator for getting all files in a directory
something like
$zipfilename = <zip filename>;
$zip = new ZipArchive();
$zip->open($zipfilename, ZipArchive::CREATE);
// add all files in directory to zip
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('path/to/files/')) as $filename) {
$zip->addFile($filename);
}
$zip->close();
Then send the zip to the browser
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="'. $zipfilename .'"');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipfilename);
Obviously you could post the directory name and event the zip file name to the script but it gives you a starting point
Try this
$files = array('file1.txt', 'file2.txt', 'file3.txt');
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
What is the easiest way of creating dynamically zip files with php?
For example,
I have these files on the server,
Root -> Folder 1 -> file1.wav
Root -> Folder 2 -> file2.jpg
I want to create a zip file contains these 2 files and allow user to download it.
any help ?
Thx in advance
See the ZipArchive class; it has what you need.
http://www.php.net/manual/en/class.ziparchive.php
Example from PHP.Net:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
After zipping it, you can output a mime-type header and output the file:
header('Content-type: application/zip');
readfile('test.zip');
this is the working example of making zip in php
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();