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:

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

Bài Viết Liên Quan

Chủ Đề