i am using this piece of code to unzip a .zip file
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
Lets say: there is a .php file in the .zip and i do not want a .php file to be extracted. How can i prevent that?
You can try something like this for PHP >= 5.5:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
if( pathinfo($zip->getNameIndex($i)['extension'] != "php")){
$zip->extractTo('/my/destination/dir/', $zip->getNameIndex($i));
}
}
$zip->close();
}
Or this for PHP < 5.5:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
if( $ext != "php")){
$zip->extractTo('/my/destination/dir/', $zip->getNameIndex($i));
}
}
$zip->close();
}
The only difference between the two is the pathinfo function. Both will loop all files inside the zip file and, if the file extension isn't php, extracts it to /my/destination/dir/.
$zip= new ZipArchive;
if($zip->open('test.zip') === TRUE){
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = pathinfo($zip->getNameIndex($i));
$fileinfo = $filename['extension'];
if($fileinfo!="php"){
$zip->extractTo('extract/',$zip->getNameIndex($i));
}
$zip->close();
}
Related
i am trying to make an array of files which are part of a .zip file.
In the .zip file are 2 files: image1.jpg and image2.jpg
$zip = new ZipArchive;
if ($zip->open($_POST['extractfile']) === TRUE) {
$unzipped = 0;
$fails = 0;
$total = 0;
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
$total ++;
echo $zip->getNameIndex($i);
The echo outputs only the first file: image1.jpg
How can i make an array of the files which are in the .zip file so that i can use a foreach loop like below:
foreach($extractfiles as $extractfile) {
echo $extractfile;
}
To the second part
<?php
$zip = new ZipArchive;
$extractfiles = [];
if ($zip->open($_POST['extractfile']) === TRUE) {
$unzipped = 0;
$fails = 0;
$total = 0;
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
$total ++;
echo $zip->getNameIndex($i);
$extractfiles[] = $zip->getNameIndex($i);
}
}
foreach($extractfiles as $extractfile) {
echo $extractfile . "<br>" . PHP_EOL;
}
I am trying to import 6 files which are in zip files. First I extract those files after that I want to get all the data in these files. But currently I am getting only the first file data. The script had not read the second file. I don't understand how to get rid from this problem.
Here is my code.
<?php
if ($_FILES) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array(
'application/zip',
'application/x-zip-compressed',
'multipart/x-zip',
'application/x-compressed'
);
foreach ($accepted_types as $mime_type) {
if ($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if (!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = "zip/" . $filename;
if (move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
$col = array();
if ($x === true) {
for ($x = 0; $x < $zip->numFiles; $x++) {
$csv = $zip->getNameIndex($x);
$zip->extractTo("zip/");
$csv_path = "zip/" . $csv;
if (($handle = fopen($csv_path, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
echo "<pre>";
print_r($col);
}
fclose($handle);
}
}
$zip->close();
unlink($target_path);
exit;
}
$message = "Your .zip file was uploaded and unpacked.";
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
?>
Any help would be Highly appreciated.
Look at this part of your code...
<?php
// ...code...
$zip->extractTo("zip/");
$csv_path = "zip/" . $csv;
if (($handle = fopen($csv_path, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// ...code...
?>
extractTo() extracts the files. There are six files, you said. Then you do fopen(), and you do it once. You want to do that fopen() on each of the files.
What you'll want is...
<?php
// ...code... (files are extracted at this point)
$files = files('zip/');
for($i = 0; i < count($files); $i++) {
$file = $files[$i];
// ...do csv stuff here for $file
}
// ...code...
?>
I want to read a file inside two zip folders...
My tree:
main_zip_folder
-second_zip_folder
--file_to_read.xml
It's simple to enter in the first_zip_folder and list all the files persent in this folder. I'm using this example:
//create a ZipArchive instance
$zip = new ZipArchive;
//open the archive
if ($zip->open('data/main_zip_folder.zip') === TRUE) {
//iterate the archive files array and display the filename or each one
for ($i = 0; $i < $zip->numFiles; $i++) {
echo 'Filename: ' . $zip->getNameIndex($i) . '<br />';
}
} else {
echo 'Failed to open the archive!';
}
My problem is how to enter in the second_zip_folder to list the files present on it, and open the file_to_read.xml?
I hope it can be help you, This code below help you for reading to zip files or more
<?php
$za = new ZipArchive();
$files = array('zip1.zip', 'zip2.zip');
//$za->open('zip1.zip', 'zip2.zip');
foreach ($files as $fil){
$za->open($fil);
for( $i = 0; $i < $za->numFiles; $i++ ){
$stat = $za->statIndex( $i );
print_r( basename( $stat['name'] ) . PHP_EOL . '<br>');
}
}
?>
How can I validate files in a zip file using finfo_open(FILEINFO_MIME_TYPE) before extracting them using extractTo?
I am using the ziparchive class: http://php.net/manual/en/class.ziparchive.php
$zip = new ZipArchive();
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$name=$zip->getNameIndex($i);
$pieces=explode(DS,$name);
if(substr($name, -1)==DS) {
//validate directory structure if desired
}
else {
//validate file
$mime=$finfo->buffer($zip->getFromIndex($i));
}
}
}
I have a folder containing many PDF files. I have built a script that zip these pdf files into 100mb batches each.
#!/usr/bin/php
<?php
$pathToFiles = "../pdffakturor_test/";
$maxFileSize = 100 * 1024 * 1024;
$counter = 1;
$currentsize = 0;
$created_at_datum = date("Ymd");
$created_at_clock = date("Hi");
$zip = new ZipArchive;
if($counter <= 10)
{
$counter = sprintf("%02s", $counter);
}
$zip->open('PROD_SE_C_S_E_'.$created_at_datum.'_'.$created_at_clock.$counter.'.zip', ZipArchive::CREATE);
if ($handle = opendir($pathToFiles))
{
while (false !== ($entry = readdir($handle)))
{
if (substr($entry, -4) == ".pdf")
{
$filesize = filesize($pathToFiles.$entry);
if($currentsize >= $maxFileSize)
{
$zip->close();
$zip = null;
$zip = new ZipArchive;
$currentsize = 0;
if($counter <= 10)
{
$counter = sprintf("%02s", $counter);
}
$zip->open('PROD_SE_C_S_E_'.$created_at_datum.'_'.$created_at_clock.$counter.'.zip', ZipArchive::CREATE);
$counter++;
}
$zip->addFile($pathToFiles.$entry, $entry);
$currentsize += $filesize;
}
}
closedir($handle);
}
?>
The problem I have is that the first zip batch becomes 183mb and the others 91,6mb. I can't figure out why the first becomes 183mb?