Hướng dẫn download file unzipper php

Script này thực tế ít sử dụng hiện nay do các server đều hỗ trợ và cung cấp công cụ controler cho các bạn quản lý (Cpanel11, Directadmin, Vesta, Koloxo…) tuy nhiên nếu bạn nào sử dụng Froxlor hay các hosting miễn phí như Hostinger thì mình thấy nó không hỗ trợ tính năng này.

Nội dung chính

  • Unzipper – nzip/Ziper file, folder nếu Hosting không hỗ trợ.
  • Cách sử dụng Unziper
  • Unzipping Files on Server with Unzipper.php
  • Wrapping It Up
  • Post navigation
  • PHP Unzipper script code
  • How do I unzip a file with Unzipper?
  • How do I unzip a PHP file?
  • How do I unzip a file on a server?

Hôm nay Tú cần cài đặt 1 Website trên Hostinger thì thấy rằng Hosting không hỗ trợ như trước, nó đẩy phần quản lý file sang net2ftp.com . Mình cũng dùng nó nhưng bị lỗi gì đó, không hiểu họ làm gì mà không thể chọn được file và unzip.

Thực tế cũng chẳng dám dùng Web FTP nhiều vì sợ vớ vẩn bị lưu user và pass hosting thì đen. Tú có check các dịch vụ Web FTP khác thì giờ không còn site nào hỗ trợ nữa, vì thế giải pháp cuối cùng là sử dụng Php Script để unzip code.

Unzipper – nzip/Ziper file, folder nếu Hosting không hỗ trợ.

Đúng như tên gọi của nó, đoạn mã Php này sẽ giúp bạn có thể unzip thư mực, zip thư mục để tải về một cách đơn giản. Để chạy được script này thì phiên bản php của server yêu cầu từ 5.3 đến < 5.6. Một số server giờ bọn mình còn dùng tới 5.7 rồi :D.

Hướng dẫn download file unzipper php

Giao diện của Unziper

Script này được tác giả Ndeet đưa lên trên github và bạn có thể tải về nó tại đây

Cách sử dụng Unziper

Bước 1: Bạn sử dụng FTP Upload (như Filezilla hay CuteFtp, LeapFtp…) để tải file ZIP của các bạn lên hosting vào cùng thư mục chứa file unzipper.php

Bước 2: Bạn truy cập file unzipper.php trên hosting.

Bước 3: Script này sẽ tự động lấy danh sách các file .zip nằm cùng cấp với nó.

Bước 4: Bạn điền path (tên thư mục mà bạn muốn nội dung file zip được nén ra đó)

Bước 5: Nhấn Unzip Archive

Khi cần Ziper file và folder để tải về thì làm tương tự.

Chúc các bạn thành công!

Web designers and developers know how it’s necessary to compress files into one zip archive when it comes to uploading a huge number of files to the server. This also means that you need to unzip the file after downloading it.
As for web developers, they attempt to access all the kinds of PHP libraries. The sizes of some are comparable to the size of the zip archive itself.
So, from this tutorial, you’ll learn how to unzip files on your server using unzipper.php script. Still, before we start, let’s mention basic facts about PHP Unzipper:
• It’s really an indispensable tool at every time you need to upload a lot of files and folders to the site.
• In order to save your time and traffic, it’s enough to upload only one zip archive to the server, and quickly unzip it using unzipper.php script.
• In addition to the unpacking function, it’s possible to view the directory structure on the site. As well, there’s also the option to view the contents of zip archives on the server without unpacking it.
• The script goes with administrator authorization by login and password. It’s fully easy to install.

Nội dung chính

  • Unzipping Files on Server with Unzipper.php
  • Wrapping It Up
  • Post navigation
  • PHP Unzipper script code
  • How do I unzip a file with Unzipper?
  • How do I unzip a PHP file?
  • How do I unzip a file on a server?

Unzipping Files on Server with Unzipper.php

  1. First, you need to download unzipper.php script to your hard drive.
    Open your hosting server cPanel and navigate to the File Manager tool. Select Upload option in the top panel.
  2. You can also upload it using FTP client (e.g., FileZilla, Total Commander).
  3. Browse unzipper.php file and upload it to your server. You need to place it in the same directory where you have a ZIP archive.
  4. Open your browser window and access your website. Add unzipper.php at the end of the link (e.g., http://your-site.com/unzipper.php).
  5. Select the file to unzip in your browser window and use additional options to define, whether you want to delete the ZIP archive after extraction, or you want to keep it.
  6. Click Unzip button in order to extract the archived file.

Wrapping It Up

If you have to copy scripts containing a large number of files (for example, content management systems, blogs) to the FTP server quite often, then you can use a more convenient way to upload them. First of all, pack them in a ZIP archive and later unpack them directly on the server. Today, we’ve shown how to do this using unzipper.php script.
What do you think of this option to unzip files on the server? Agree that the download process is really faster if you upload only one file than all the files individually. When a cloud of files is downloaded, the FTP client accesses the server multiple times after each file, which takes a certain amount of time. Wait for more useful tips!

Post navigation

The Unzipper is a PHP script that extracts .zip, .rar or .gz archives on webservers.
It detects .zip/.rar/.gz archives and let you choose which one to extract (if there are multiple archives available). It also supports creating Zip archives.
- It is useful if you not have access to a File-Manager, and you want to upload a lot of files (php framework or image collection) as archive.

• To download the script, click on this link: Download PHP Unzipper (79 KB).

Requirements

- PHP 5.3 and newer.
- PHP Rar extension (needed only if you want to extract Rar archive).

Usage

1. Copy the script code from this page, or Download it from the link above, and save it into a .php file on your server (unzipper.php).
2. Access the "unzipper.php" in your browser.
- To extract a Zip/Rar archive, it must be in the same folder where you have the "unzipper.php" file.
3. Choose .zip, .rar archive or .gz file you want to extract, and click on the "Unzip Archive" button.

PHP Unzipper script code

Click on the code to select it.

prepareExtraction($archive, $destination);
}

//Create zip archive
if(isset($_POST['dozip'])){
 $zippath = !empty($_POST['zippath']) ? strip_tags($_POST['zippath']) :'.';
 // Resulting zipfile e.g. zipper--2016-07-23--11-55.zip
 $zipfile = 'zipper-'. date('Y-m-d--H-i') .'.zip';
 Zipper::zipDir($zippath, $zipfile);
}

$timeend = microtime(TRUE);
$time = $timeend - $timestart;

/**
 * Class Unzipper
 */
class Unzipper {
 public $localdir ='.';
 public $zipfiles = array();
 public function __construct(){
 //read directory and pick .zip and .gz files
 if($dh = opendir($this->localdir)){
 while (($file = readdir($dh)) !== FALSE){
 if(pathinfo($file, PATHINFO_EXTENSION)==='zip' || pathinfo($file, PATHINFO_EXTENSION)==='gz' || pathinfo($file, PATHINFO_EXTENSION)==='rar') $this->zipfiles[] = $file;
 }
 closedir($dh);
 if(!empty($this->zipfiles)) $GLOBALS['status'] = array('info'=>'.zip or .gz or .rar files found, ready for extraction');
 else $GLOBALS['status'] = array('info'=>'No .zip or .gz or rar files found. So only zipping functionality available.');
 }
 }

 // Prepare and check zipfile for extraction
 public function prepareExtraction($archive, $destination){
 // Determine paths.
 if(empty($destination)) $extpath = $this->localdir;
 else {
 $extpath = $this->localdir .'/'. $destination;
 // todo move this to extraction function
 if(!is_dir($extpath)) mkdir($extpath);
 }
 //allow only local existing archives to extract
 if(in_array($archive, $this->zipfiles)) self::extract($archive, $extpath);
 }

 //Checks file extension and calls suitable extractor functions
 public static function extract($archive, $destination){
 $ext = pathinfo($archive, PATHINFO_EXTENSION);
 switch($ext){
 case 'zip':
 self::extractZipArchive($archive, $destination);
 break;
 case 'gz':
 self::extractGzipFile($archive, $destination);
 break;
 case 'rar':
 self::extractRarArchive($archive, $destination);
 break;
 }
 }

 //Decompress/extract a zip archive using ZipArchive
 public static function extractZipArchive($archive, $destination){
 // Check if webserver supports unzipping.
 if(!class_exists('ZipArchive')){
 $GLOBALS['status'] = array('error'=>'Error: Your PHP version does not support unzip functionality.');
 return;
 }
 $zip = new ZipArchive;
 // Check if archive is readable.
 if($zip->open($archive)===TRUE){
 // Check if destination is writable
 if(is_writeable($destination .'/')){
 $zip->extractTo($destination);
 $zip->close();
 $GLOBALS['status'] = array('success'=>'Files unzipped successfully');
 }
 else $GLOBALS['status'] = array('error'=>'Error: Directory not writeable by webserver.');
 }
 else $GLOBALS['status'] = array('error'=>'Error: Cannot read .zip archive.');
 }

 // Decompress a .gz File
 public static function extractGzipFile($archive, $destination){
 // Check if zlib is enabled
 if(!function_exists('gzopen')){
 $GLOBALS['status'] = array('error'=>'Error: Your PHP has no zlib support enabled.');
 return;
 }
 $filename = pathinfo($archive, PATHINFO_FILENAME);
 $gzipped = gzopen($archive, 'rb');
 $file = fopen($filename, 'w');
 while($string = gzread($gzipped, 4096)) fwrite($file, $string, strlen($string));
 gzclose($gzipped);
 fclose($file);
 // Check if file was extracted.
 if(file_exists($destination .'/'. $filename)) $GLOBALS['status'] = array('success'=>'File unzipped successfully.');
 else $GLOBALS['status'] = array('error'=>'Error unzipping file.');
 }

 //Decompress/extract a Rar archive using RarArchive
 public static function extractRarArchive($archive, $destination){
 // Check if webserver supports unzipping.
 if(!class_exists('RarArchive')){
 $GLOBALS['status'] = array('error'=>'Error: Your PHP version does not support .rar archive functionality. How to install RarArchive');
 return;
 }
 // Check if archive is readable.
 if($rar = RarArchive::open($archive)){
 // Check if destination is writable
 if(is_writeable($destination .'/')){
 $entries = $rar->getEntries();
 foreach ($entries as $entry){
 $entry->extract($destination);
 }
 $rar->close();
 $GLOBALS['status'] = array('success'=>'Files extracted successfully.');
 }
 else $GLOBALS['status'] = array('error'=>'Error: Directory not writeable by webserver.');
 }
 else $GLOBALS['status'] = array('error'=>'Error: Cannot read .rar archive.');
 }
}

/**
 * Class Zipper
 *
 * Copied and slightly modified from http://at2.php.net/manual/en/class.ziparchive.php#110719
 * @author umbalaconmeogia
 */
class Zipper {
 /**
 * Add files and sub-directories in a folder to zip file.
 *
 * @param string $folder
 * Path to folder that should be zipped.
 *
 * @param ZipArchive $zipFile
 * Zipfile where files end up.
 *
 * @param int $exclusiveLength
 * Number of text to be exclusived from the file path.
 */
 private static function folderToZip($folder, &$zipFile, $exclusiveLength){
 $handle = opendir($folder);
 while(FALSE !== $f = readdir($handle)){
 // Check for local/parent path or zipping file itself and skip.
 if($f !='.' && $f != '..' && $f != basename(__FILE__)){
 $filePath = $folder .'/'. $f;
 // Remove prefix from file path before add to zip.
 $localPath = substr($filePath, $exclusiveLength);
 if(is_file($filePath)) $zipFile->addFile($filePath, $localPath);
 else if(is_dir($filePath)){
 // Add sub-directory.
 $zipFile->addEmptyDir($localPath);
 self::folderToZip($filePath, $zipFile, $exclusiveLength);
 }
 }
 }
 closedir($handle);
 }
 /**
 * Zip a folder (including itself).
 * Usage:
 * Zipper::zipDir('path/to/sourceDir', 'path/to/out.zip');
 *
 * @param string $sourcePath
 * Relative path of directory to be zipped.
 *
 * @param string $outZipPath
 * Relative path of the resulting output zip file.
 */
 public static function zipDir($sourcePath, $outZipPath){
 $pathInfo = pathinfo($sourcePath);
 $parentPath = $pathInfo['dirname'];
 $dirName = $pathInfo['basename'];
 $z = new ZipArchive();
 $z->open($outZipPath, ZipArchive::CREATE);
 $z->addEmptyDir($dirName);
 if($sourcePath == $dirName) self::folderToZip($sourcePath, $z, 0);
 else self::folderToZip($sourcePath, $z, strlen($parentPath.'/'));

 $z->close();
 $GLOBALS['status'] = array('success'=>'Successfully created archive '. $outZipPath);
 }
}
?>




File Unzipper, Unrar + Zipper



Status:
Processing Time: seconds

Archive Unzipper

Enter extraction path without leading or trailing slashes (e.g. "mypath"). If left empty current directory will be used.

Archive Zipper

Enter path to be zipped without leading or trailing slashes (e.g. "zippath"). If left empty current directory will be used.

Unzipper version:

Screenshot

• Scipt Page: github.com/ndeet/unzipper

How do I unzip a file with Unzipper?

Right-click the file you want to zip, and then select Send to > Compressed (zipped) folder. Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it.

How do I unzip a PHP file?

Description: Create an object of the ZipArchive class and open a given zip file using $zip->open() method. If it returns TRUE then extract the file to the specified path with extractTo() method by passing path address as an argument in it. Example 2: This example unzip the specific file from the folder.

How do I unzip a file on a server?

Step 1 – Log In to Your Server with SSH.

SSH terminal command in MyKinsta. ... .

SSH terminal window. ... .

Navigate to the directory containing your ZIP file. ... .

List files in Terminal. ... .

Unzip files in Terminal. ... .

Verify unzipped files..