압축파일(zip) 생성 후 다운로드 > 학습

본문 바로가기

사이트 내 전체검색

443,782명이 방문해 주셨습니다.

[  PHP  ] 압축파일(zip) 생성 후 다운로드

작성일 20-07-24 10:26

페이지 정보

작성자IPFT 조회 284회 댓글 0건

본문

다운로드 버튼에 링크로 아래의 소스가 있는 페이지로 링크를 걸어주세요.

자동으로 압축파일(zip)을 생성하여 다운로드됩니다.

다만, 아래의 코드중 파일명, 압축파일명, 서버상 파일 경로 등은 수정해주시기 바랍니다.(<-주석되어 있음)




<?php
$file_names = array('file001.png','file002.png'); //각각의 파일명
$archive_file_name=$name.'File.Zip'; //압축 파일명
$file_path=$_SERVER['DOCUMENT_ROOT'].'/data/file'; //파일이 있는 경로

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

function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
    $Zip = new ZipArchive();

    if ($Zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>\n");
    }

    foreach($file_names as $files)
    {
        $Zip->addFile($file_path.$files,$files);
    }

    $Zip->close();
    header("Content-type: application/Zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header("Content-length: " . filesize($archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}
?>

댓글목록

등록된 댓글이 없습니다.

Copyright © 소유하신 도메인. All rights reserved.