Hướng dẫn how to download file in zip php? - Làm thế nào để tải xuống tệp trong php zip?

Hãy chắc chắn rằng đường dẫn bên trong $archive_file_name là chính xác và không tồn tại. Trong mã của bạn, bạn đã cập nhật biến $archive_file_name bằng cách đặt một biến tiền tố chưa được khai báo - $name.

$archive_file_name = $name.'iMUST_Products.zip';

Nếu đường dẫn không chính xác và không thể tìm thấy, tệp được tạo ra không có nơi nào để truy cập và hai tệp không có nơi nào để lưu trữ để bắt đầu. Đường dẫn đến các tập tin sẽ được lưu trữ cũng nên tồn tại.

Giả sử rằng bạn chỉ muốn tùy chỉnh tên của thư mục được tải xuống để tải xuống đó là lý do tại sao có một biến $name để ngăn tên tệp dự phòng. Nhưng bạn phải xem xét URI nơi bạn chạy tập lệnh của bạn.

Ví dụ: bạn đang sử dụng định tuyến URI và bạn tải tập lệnh tại https://sample.co/file-man Quản lý/doad-zipped-111. Với tập lệnh hiện tại của bạn, thư mục nén sẽ được lưu trữ tại thư mục

$file_names = array('iMUST Operating Manual V1.3a.pdf', 'iMUST Product Information Sheet.pdf');

// $name should be declared before this line
$archive_file_name = $name.'iMUST_Products.zip';

//Download Files path
$file_path = $_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';

zipFilesAndDownload($file_names, $archive_file_name, $file_path);

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{

    $zip = new ZipArchive();
    // WE REUSED THE $file_path VARIABLE HERE THEN ADDED zipped FOLDER
    if ($zip->open($file_path.'zipped/'.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit('cannot open <'.$archive_file_name.'>');
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, $files);
    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header('Content-length: '.filesize($file_path.'zipped/'.$archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}
1. Nếu nó không tồn tại, bạn sẽ gặp phải lỗi mà bạn đang gặp phải ngay bây giờ.

Giải pháp là khai báo rõ ràng đường dẫn đến thư mục nơi tệp nén sẽ được lưu trữ.

Ví dụ: bạn muốn lưu trữ tệp nén tại

$file_names = array('iMUST Operating Manual V1.3a.pdf', 'iMUST Product Information Sheet.pdf');

// $name should be declared before this line
$archive_file_name = $name.'iMUST_Products.zip';

//Download Files path
$file_path = $_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';

zipFilesAndDownload($file_names, $archive_file_name, $file_path);

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{

    $zip = new ZipArchive();
    // WE REUSED THE $file_path VARIABLE HERE THEN ADDED zipped FOLDER
    if ($zip->open($file_path.'zipped/'.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit('cannot open <'.$archive_file_name.'>');
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, $files);
    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header('Content-length: '.filesize($file_path.'zipped/'.$archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}
2. Đảm bảo rằng thư mục này tồn tại bằng cách tạo một thư mục có tên
$file_names = array('iMUST Operating Manual V1.3a.pdf', 'iMUST Product Information Sheet.pdf');

// $name should be declared before this line
$archive_file_name = $name.'iMUST_Products.zip';

//Download Files path
$file_path = $_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';

zipFilesAndDownload($file_names, $archive_file_name, $file_path);

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{

    $zip = new ZipArchive();
    // WE REUSED THE $file_path VARIABLE HERE THEN ADDED zipped FOLDER
    if ($zip->open($file_path.'zipped/'.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit('cannot open <'.$archive_file_name.'>');
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, $files);
    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header('Content-length: '.filesize($file_path.'zipped/'.$archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}
3 bên trong thư mục
$file_names = array('iMUST Operating Manual V1.3a.pdf', 'iMUST Product Information Sheet.pdf');

// $name should be declared before this line
$archive_file_name = $name.'iMUST_Products.zip';

//Download Files path
$file_path = $_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';

zipFilesAndDownload($file_names, $archive_file_name, $file_path);

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{

    $zip = new ZipArchive();
    // WE REUSED THE $file_path VARIABLE HERE THEN ADDED zipped FOLDER
    if ($zip->open($file_path.'zipped/'.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit('cannot open <'.$archive_file_name.'>');
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, $files);
    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header('Content-length: '.filesize($file_path.'zipped/'.$archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}
4 và bạn có quyền truy cập để viết trên thư mục này.

$file_names = array('iMUST Operating Manual V1.3a.pdf', 'iMUST Product Information Sheet.pdf');

// $name should be declared before this line
$archive_file_name = $name.'iMUST_Products.zip';

//Download Files path
$file_path = $_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';

zipFilesAndDownload($file_names, $archive_file_name, $file_path);

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{

    $zip = new ZipArchive();
    // WE REUSED THE $file_path VARIABLE HERE THEN ADDED zipped FOLDER
    if ($zip->open($file_path.'zipped/'.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit('cannot open <'.$archive_file_name.'>');
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, $files);
    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header('Content-length: '.filesize($file_path.'zipped/'.$archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}

Mọi người có thể đề xuất - "Tại sao không trực tiếp đưa đường dẫn đầy đủ đến biến $archive_file_name?

$archive_file_name = $file_path.'zipped/'.$name.'iMUST_Products.zip';

Nếu chúng tôi đặt nó trực tiếp vào biến $archive_file_name, tệp sẽ được tải xuống hầu như sẽ được đặt tên là

$file_names = array('iMUST Operating Manual V1.3a.pdf', 'iMUST Product Information Sheet.pdf');

// $name should be declared before this line
$archive_file_name = $name.'iMUST_Products.zip';

//Download Files path
$file_path = $_SERVER['DOCUMENT_ROOT'].'/Harshal/files/';

zipFilesAndDownload($file_names, $archive_file_name, $file_path);

function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{

    $zip = new ZipArchive();
    // WE REUSED THE $file_path VARIABLE HERE THEN ADDED zipped FOLDER
    if ($zip->open($file_path.'zipped/'.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit('cannot open <'.$archive_file_name.'>');
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, $files);
    }
    $zip->close();
    //then send the headers to force download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header('Content-length: '.filesize($file_path.'zipped/'.$archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}
7.

  • Cập nhật lần cuối: 21/08/2021

Tạo tệp zip hoặc tải xuống tất cả các tệp vì các tệp zip bằng PHP không quá khó. Bạn chỉ cần tạo tệp zip bằng hàm PHP Touch () sau đó thêm tất cả các tệp vào tệp zip đã tạo và cuối cùng tự động tải xuống tệp bằng nội dung tiêu đề.

Hướng dẫn how to download file in zip php? - Làm thế nào để tải xuống tệp trong php zip?

Trong bài đăng này, tôi sẽ cung cấp cho bạn một hướng dẫn đầy đủ cho.

  • Làm thế nào để tạo một tệp zip trong PHP?
  • Làm thế nào để thêm tệp vào zip trong PHP?
  • Làm thế nào để tải zip trong PHP?

PHP tạo tệp zip trên máy chủ

Tạo một tệp hoặc tạo bất kỳ tệp nào giống hệt nhau trong PHP, bạn chỉ cần hàm PHP Touch () mặc định. Chức năng này sẽ lấy tên tệp với vị trí và trả lại cho bạn một tệp.

$zip_file_name_with_location = "/zip-file/all-image.zip"; 
touch($zip_file_name_with_location); // just create a zip file

PHP Thêm tệp vào zip

Bây giờ nếu bạn muốn thêm hoặc chèn một số tệp vào tệp zip của mình, bạn cần mở tệp zip của mình trong lớp Ziparchive mặc định bằng các phương thức Open (). Phương thức này sẽ lấy tên tệp zip của bạn với vị trí. Sau khi mở tệp zip của bạn, sau đó chèn các tệp của bạn vào tệp zip đã mở bằng phương thức addFile (), phương thức này sẽ lấy hai tham số là tên tệp với vị trí (mà bạn muốn chèn) và một tham số khác không có vị trí . Sau khi thêm tất cả các tệp, bạn cần đóng tệp zip mở.

$zip = new ZipArchive;
$opening_zip = $zip->open($zip_file_name_with_location);
// add a image to this opening zip file
$opening_zip->addFile($inserting_file_name_with_location,$inserting_file_name_without_location);
$opening_zip>close();

Lưu ý: Giả sử bạn muốn đổi tên tên tệp chèn của mình bên trong tệp zip sau đó bạn có thể thực hiện nó chỉ bằng cách sử dụng tên được chấp nhận của bạn như một $ chèn_file_name_without_location. suppose you want to rename your inserting file name inside the zip file then you can do it just using your accepted name as a $inserting_file_name_without_location.

Hãy để, tên tệp chèn của bạn với vị trí là trên mạng /images Bạn muốn thêm tệp này dưới dạng Image Image-1.jpg. Vì vậy, bạn có thể làm điều đó như thế này.
your inserting file name with location is “/images/ngo-2342341.jpg” And,
without location, the file name is “ngo-2342341.jpg”.
You want to add this file as “image-1.jpg”. So you can do it like this.

$opening_zip->addFile($inserting_file_name_with_location,”image-1.jpg”);

Trong ví dụ này, chúng tôi chỉ cần thêm hoặc chèn một tệp vào tệp zip của mình, nhưng để thêm tất cả các tệp của thư mục bạn cần tìm nạp tất cả các tệp từ một thư mục.

Php tải xuống tệp zip từ máy chủ

Để tải xuống tệp zip của bạn một cách tự động, bạn chỉ cần đặt một số mô tả kiểu nội dung và nội dung tiêu đề sau đó đọc tệp.

// file will download ad this name
$demo_name = "my-image.zip";
header('Content-type: application/zip');  
header('Content-Disposition: attachment; filename="'.$demo_name.'"');  
readfile($zip_file_name_with_location); // auto download
//if you wnat to delete this zip file after download
unlink($zip_file_name_with_location);

Làm cách nào để zip một tệp trong PHP?

Trong trình duyệt, nhập https: //localhost/zip.php dưới dạng URL và tệp sẽ được nén.enter https://localhost/zip.php as the url and the file will be zipped.

Làm cách nào để tải xuống tệp từ PHP?

PHP cho phép bạn tải xuống tệp dễ dàng bằng hàm readfile () tích hợp.Hàm readfile () đọc một tệp và ghi nó vào bộ đệm đầu ra ...
Tiêu đề ('Loại nội dung: Ứng dụng/Dòng octet') ;.
Tiêu đề ("Mã hóa chuyển đổi nội dung: UTF-8") ;.

Làm cách nào để tải xuống tệp zip từ url trong PHP?

Để tải xuống và trích xuất tệp zip, chỉ cần một bước:- $ url = "http://anysite.com/file.zip";$ zip_file = "Thư mục/Tải xuống.$url = "http://anysite.com/file.zip"; $zip_file = "folder/downloadfile.