Php unzip all files

micronax / unzip.php

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

echo » ;
echo ‘
‘ ;
if (!class_exists( ‘ZipArchive’ ))
die(»

ZipArchive is not installed. Pleas echeck php.ini

«);

>
if ( $ _GET [ ‘delete_me’ ] == ‘true’ )
unlink(__FILE__);
echo »

Bye Bye!

«; exit;

>
if ( $ _GET [ ‘unzip_all’ ] == ‘true’ )
$ files = glob( ‘*.zip’ );
foreach ( $ files as $ file )
$ zip = new ZipArchive ();
if ( $ res = $ zip -> open ( $ file ))
$ zip -> extractTo ( ‘./’ );
$ zip -> close ();
if ( $ _GET [ ‘delete’ ] == ‘true’ )
unlink( $ file );
>
> else
echo »

Unzip failed!

«;

>
echo »

Unzip successful!

«;

>
>
if ( $ unzip_file = $ _GET [ ‘unzip_file’ ])
$ unzip_file = str_replace(» / «, «», $ unzip_file );
if (!is_readable( $ unzip_file ))
echo »

File cannot be opened!

«;

>
if (stripos( ‘..’ , $ unzip_file ))
echo »

Illegal pattern!

«;

>
$ zip = new ZipArchive ();
if ( $ res = $ zip -> open ( $ unzip_file ))
$ zip -> extractTo ( ‘./’ );
$ zip -> close ();
echo »

Unzip successful!

«;

if ( $ _GET [ ‘delete’ ] == ‘true’ )
unlink( $ unzip_file );
>
> else
echo »

Unzip failed!

«;

>
>
echo »

Zip-files in current dir

«;

$ files = glob( ‘*.zip’ );
echo »

«;

«, $ file , $ file , $ file );

foreach ( $ files as $ file )
echo sprintf(»
%s Unzip Unzip & delete
>
echo «

«;

echo »

Unzip all files • Unzip and delete all files • Delete this file

«;

echo »


Copyright © 2015 Fabian Golle IT. All rights reserved. «;

Источник

How to Zip and Unzip Files in PHP

Monty Shokeen

Monty Shokeen Last updated Jun 13, 2022

Compressing files when transferring them over the internet has a lot of advantages. In most cases, the combined total size of all the files in the compressed format comes down by a nice margin. This means that you will save some of your bandwidth, and users will also get faster download speeds. Once the users have downloaded a file, they can decompress it whenever they want. In short, compression can make serving files over the internet a lot easier for you as well as your visitors.

One factor that can discourage you from compressing files or make the process very tiresome is the fact that you might be doing it manually. Luckily, PHP comes with a lot of extensions that deal specifically with file compression and extraction. You can use the functions available in these extensions to automatically compress files in PHP.

This tutorial will teach you how to zip and unzip (compress and extract) files to and from a zip archive in PHP. You will also learn how to delete or rename files in an archive without extracting them first.

Compressing Individual Files in PHP

The PHP ZipArchive class has a lot of properties and methods which can help you compress and decompress all your files.

You can add files to your zip archive one at a time or add the whole directory at once. In either case, the first step is to create a new ZipArchive instance and then call the open($filename, [$flags]) method. This method will open a new zip archive for reading, writing, or other modifications. There are five valid values for the optional $flag parameter which determine how to handle different situations.

  • ZipArchive::OVERWRITE —This flag will overwrite the contents in the specified archive if it already exists.
  • ZipArchive::CREATE —This flag will create a new archive if it does not already exist.
  • ZipArchive::EXCL —This flag will result in an error if the archive already exists.
  • ZipArchive::CHECKCONS —This flag will tell PHP to perform additional consistency checks on the archive and give an error if they fail.
  • ZipArchive::RDONLY —This flag became available in PHP 7.4.3 and allows you to open an archive in read-only mode.

You can check the documentation of this method to learn about different error codes returned in case of failures to open the file. If the zip file was opened or created successfully, the method will return true .

You will have to use different flag combinations in different situations to avoid any unexpected results. For example, use the ZipArchive::OVERWRITE flag if you don’t care about the content of an existing archive. It will not create a new archive if none exists with that particular name.

You can use a combination of ZipArchive::OVERWRITE and ZipArchive::CREATE to overwrite an existing archive and create a new archive if none exists with that particular name. Here’s an example:

$zip->open('my_compressed_files.zip', ZipArchive::OVERWRITE|ZipArchive::CREATE); 

When using the ZipArchive::CREATE flag, you will notice that it starts modifying an existing archive if one already exists. This might be what you actually want to do, but it can also have some unexpected consequences. You can use a combination of ZipArchive::CREATE and ZipArchive::EXCL to make sure that you are only working with archives that did not previously exist.

$zip->open('new_compressed_files.zip', ZipArchive::EXCL|ZipArchive::CREATE); 

Once you have opened the archive successfully, you can use the addFile($filename, $localname, $start, $length) method to add any file from a given path to your archive. The $filename parameter is the path of a file that you want to add to the archive. The $localname parameter is used to assign a name to the file to store it inside the archive. You can call addFile() every time you want to add a new file to your archive.

After adding all the necessary files to the archive, you can simply call the close() method to close it and save the changes.

Let’s say you have a website which allows users to download font files for different fonts along with the licensing information to use them. Files like these will be perfect examples of automated archiving using PHP. The following code shows you how to do exactly that.

Источник

Читайте также:  Python socket accept invalid argument
Оцените статью