For a project, I would like to read a specific file in an uploaded archive (zip file). The file always has the same name/relative-path.
Is it possible to only extract this file, or should I extract the entire archive?
Thanks
$files = array('/a_path/titi.txt'); //create a list of files to be extracted here
$zip = new ZipArchive;
$res = $zip->open('test_im.zip');
if ($res === TRUE) {
$zip->extractTo('/my/destination/dir/', $files);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
Related
I got a loop with whom I write data in a table, included urls of files I've to zip.
During the loop I put the url in an array.
At the end of table I would have a button, giving me the zip of all files.
I'm in a wordpress site, PHP 5 and Linux.
During loop I save url file here with its path
if (file_exists($file_letttut)) {
$nameAndCode['zipp'] = "http://mypath/to/".$file_letttut;
$namesArray[] = $nameAndCode;}
I tried different codes to zip, I write here the easier
$zip = new ZipArchive;
$zip_name = "zippot.zip"; // Zip name
$res = $zip->open($zip_name, ZipArchive::CREATE);
if ($res === TRUE) {
foreach($namesArray as $key => $value) {
$nomefile = basename($namesArray[$key][zipp].$value);
$nomefile = str_replace('Array', '', $nomefile);
$nomecompleto = str_replace('Array', '', $namesArray[$key][zipp].$value);
$zip->addFile($nomecompleto, $nomefile);
}
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
if(file_exists($zip_name)) {
echo "yes";die;
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$res.'"');
readfile($res);
unlink($res);
} else {
echo "zip not created";die;
}
$zip_name doesn't exist...
Where do I fail?
Should I've to copy files to a folder before?
Thanks in advance
I am creating script zipping files from an array.
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
#Mysql connect
$files_to_zip = array();
while($row = mysql_fetch_assoc($shots)) {
$files_to_zip[] = '/home/username/domains/example.com/public_html/components/items/images/item/' . $row["name"] . '_thb.' . $row["ext"];
}
if(isset($_POST['create'])){
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive5.zip');
}
After form submitting zip is created, but there's full directory tree inside files.
How to only zip files without directories?
Second thing:
When I open zip in total commander - I can see directories and files. When I open it normally - zip is empty.
The second argument to the addFile() method is the name of the file inside the zip. You're doing this:
$zip->addFile($file, $file);
So you're getting something like:
$zip->addFile('/path/to/some/file', '/path/to/some/file');
Which just copies the path from the source into the zip. If you want all the files to be in one dir in the zip, remove the path from the file name:
$zip->addFile($file, basename($file));
This will give you:
$zip->addFile('/path/to/some/file', 'file');
Note this won't work too well if you have files with the same name in different directories.
Check the docs on addFile method of Zip extension. You can specify local name as second argument.
Example from the documentation
<?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';
}
?>
I am generating dynamic images from API.
And on button click i want to download a zip file with images .
I tried many solution, but still not able to download zip file with images
$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()
function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)
!== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of
',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
// using function as
include('functions.php');
$files_to_zip = array(
'database/350-001(3.1).examdb',
'setup/ExaminationEngine.exe'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
I am using this function to make zip.
Every thing is working perfect but the files that are created under zip file is created by full path i want only my two file in my-archive.php. but inside my-archive.php datbase/350-001(3.1).examdb and setup/ExaminationEngine.exe database and setup folder is created.
How can i fix this?
try
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,basename($file));
}
instead of
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
second argument of addFile is 'local path', basename of $file is what you want to pass to it (I assume that right now you are passing some kind of relative path)
Remember that you will have to manually assert that your filenames are unique.
see
http://php.net/manual/en/ziparchive.addfile.php
http://php.net/manual/en/function.basename.php
I recommend that you prepare a temporary folder, copy the files from different locations in there, zip it, and remove the temporary folder. Doing otherwise sounds pretty boring.
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();