>  기사  >  백엔드 개발  >  php_php 팁에서 zip 압축 파일을 만드는 간단한 방법

php_php 팁에서 zip 압축 파일을 만드는 간단한 방법

WBOY
WBOY원래의
2016-05-16 19:53:311266검색

이 기사의 예에서는 PHP에서 zip 압축 파일을 간단히 생성하는 방법을 설명합니다. 참고하실 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {
    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
      if(file_exists($file)) {
        $valid_files[] = $file;
      }
    }
  }
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
      return false;
    }
    //add the files
    foreach($valid_files as $file) {
      $zip->addFile($file,$file);
    }
    //debug
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
    //close the zip -- done!
    $zip->close();
    //check to make sure the file exists
    return file_exists($destination);
  }
  else
  {
    return false;
  }
}

사용방법:

$files_to_zip = array(
  'preload-images/1.jpg',
  'preload-images/2.jpg',
  'preload-images/5.jpg',
  'kwicks/ringo.gif',
  'rod.jpg',
  'reddit.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');

더 많은 PHP 관련 콘텐츠에 관심이 있는 독자는 이 사이트의 특별 주제를 확인할 수 있습니다. "PHP 작업 zip 파일 및 압축 기술 요약", "PHP 파일 작업 요약 ", " PHP 정규식 사용법 요약", "PHP Ajax 기술 및 응용 요약", "PHP 작업 및 연산자 사용법 요약 ", "PHP 네트워크 프로그래밍 기술 요약", "PHP 기본 구문 튜토리얼 소개", "PHP 운영 오피스 문서 기술 요약(워드, 엑셀, 액세스 포함) , ppt) ", "php 날짜 및 시간 사용법 요약", "php 객체지향 프로그래밍 입문 튜토리얼", "php 문자열(문자열) 사용법 요약 ", " PHP mysql 데이터베이스 작업 입문 튜토리얼 " 및 " PHP 공통 데이터베이스 작업 기술 요약 "

이 기사가 PHP 프로그래밍에 종사하는 모든 사람에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.