Hướng dẫn php download zip file not working - php tải xuống tệp zip không hoạt động

Sau đây là mã của tôi để tải xuống tệp zip bằng PHP.

function create_zip($files, $file_name, $overwrite = false) {

    foreach ($files as $imglink) {
        $img = file_get_contents($imglink);
        $destination_path = $_SERVER['DOCUMENT_ROOT'] . 'demoproject/downloads/' . time() . '.jpg';
        file_put_contents($destination_path, $img);
        $imgFiles[] = $destination_path;
    }

    if (file_exists($file_name) && !$overwrite) {
        return false;
    }
    $valid_files = array();
    if (is_array($imgFiles)) {
        foreach ($imgFiles as $file) {
            $valid_files[] = $file;
        }
    }

    if (count($valid_files)) {
        $zip = new ZipArchive();
        if ($zip->open($file_name, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            echo "Sorry ZIP creation failed at this time";
        }
        foreach ($valid_files as $file) {
            $zip->addFile($file, pathinfo($file, PATHINFO_BASENAME));
        }

        $count = $zip->numFiles;
        $resultArr = array();
        $resultArr['count'] = $count;
        $resultArr['destination'] = $file_name;

        $filename = $file_name;
        $filepath = $_SERVER['DOCUMENT_ROOT'] . 'demoproject/';


        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . filesize($filepath . $filename));
        ob_end_flush();
        @readfile($filepath . $filename);
    } else {
        return false;
    }
}

Ở đây $filename$filepath chứa tên và đường dẫn của tệp zip tương ứng.

Bán tại:

echo $filepath.$filename;

Output : D:/wamp/www/demoproject/1357198557.zip

Vấn đề là nó hiển thị cửa sổ tải xuống nhưng hiển thị kích thước thư mục 0 byte. Tôi đang sử dụng Windows7. Xem hình ảnh bên dưới:

Hướng dẫn php download zip file not working - php tải xuống tệp zip không hoạt động

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

/**
* @author: Sohel Rana
* @Author uri: http://sohelrana.me
* @descrip: Tạo tệp zip và tải xuống trong PHP
*/
FunctionCreateZipAndDownLoad ($ Files, $ Filespath, $ zipfilename) createZipAndDownload($files, $filesPath, $zipFileName)
{
// Tạo phiên bản của ziparchive. và mở thư mục zip.
$ zip = new \ ziparchive ();zip = new \ZipArchive();
if ($ zip-> mở ($ zipfilename, \ ziparchive :: created)! == true) { ($zip->open($zipFileName, \ZipArchive::CREATE) !== TRUE) {
thoát ("không thể mở \ n");cannot open <$zipFileName>\n");
}
// Thêm mọi tệp tệp đính kèm vào zip.
foreach ($ filesas $ file) { ($files as $file) {
$ zip-> addfile ($ filespath. $ file, $ file);zip->addFile($filesPath . $file, $file);
}
// Thêm mọi tệp tệp đính kèm vào zip.zip->close();
foreach ($ filesas $ file) {
$ zip-> addfile ($ filespath. $ file, $ file);Content-type: application/zip");
$ zip-> đóng ();Content-Disposition: attachment; filename = $zipFileName");
// Tải xuống tệp zip đã tạoPragma: no-cache");
Tiêu đề ("Loại nội dung: Ứng dụng/Zip");Expires: 0");
Tiêu đề ("Xác định nội dung: tệp đính kèm; fileName = $ zipfilename");$zipFileName");
tiêu đề ("Pragma: không có cache");
Tiêu đề ("Hết hạn: 0");
readfile ("$ zipfilename");
lối ra;files = array('sample.php', 'sample.jpg', 'sample.pdf', 'sample.doc');
}
// Các tệp cần được thêm vào zipfilesPath = '/ROOT/FILE_PATH';
$ files = mảng ('sample.php', 'sample.jpg', 'sample.pdf', 'sample.doc');
// Thư mục của các tệpzipName = 'document.zip';
$ filespath = '/root/file_path'; createZipAndDownload($files, $filesPath, $zipName);

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.

Làm thế nào tải xuống tất cả các tệp từ thư mục trong PHP?

PHP $ name = $ _get ['nama'];Tải xuống ($ name);Tải xuống chức năng ($ name) {$ file = $ nama_fail;if (file_exists ($ file)) {header ('nội dung mô tả: chuyển tệp');Tiêu đề ('loại nội dung: Ứng dụng/-dòng octet');Tiêu đề ('Xác định nội dung: tệp đính kèm; fileName ='.

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

Ziparchive :: ghi đè: ziparchive :: created)} $ zip-> đóng ();return file_exists ($ Destination);} other {return false;}} khác {return false;}} $ filename = 'myzipfile.zip ';$ files = mảng ('Tải lên/hồ sơ1. JPEG', 'Tải lên/hồ sơ2.

Php zip là gì?

Giới thiệu về các tệp ZIP PHP.Các hàm tệp ZIP PHP được sử dụng để lưu trữ các tệp hoặc thư mục cùng nhau trong nén.ZIP là định dạng tệp lưu trữ được sử dụng để lưu trữ các tệp ở dạng nén.used to stores the bunch of files or directory together in the compressed. The Zip is the archive file format that is used to stores the files in compressed form.