2013-10-15 4 views
1

"output"폴더의 내용을 압축 할 때 호출되는 스크립트가 있지만폴더의 내용을로드하려고하면 "Outpup"폴더에 있습니다. 루트 디렉토리 C에서 파일을 압축 : \ZIP 폴더 whitou 루트

로 myscripf

$rootpath="./Output"; 
$destinazione="./Output/lista.zip"; 
Zip($rootpath,$destinazione); 

기능 ZIP 내가 밖으로 "폴더의 내용을 압축 할 필요가

function Zip($source, $destination) 

{ 

    if (!extension_loaded('zip') || !file_exists($source)) { 
     return false; 
    } 

    $zip = new ZipArchive(); 
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { 
     return false; 
    } 

    $source = str_replace('\\', '/', realpath($source)); 

    if (is_dir($source) === true) 
    { 
     $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); 

     foreach ($files as $file) 
     { 
      $file = str_replace('\\', '/', $file); 

      // Ignore "." and ".." folders 
      if(in_array(substr($file, strrpos($file, '/')+1), array('.', '..'))) 
       continue; 

      $file = realpath($file); 

      if (is_dir($file) === true) 
      { 
       $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); 
      } 
      else if (is_file($file) === true) 
      { 
       $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); 
      } 
     } 
    } 
    else if (is_file($source) === true) 
    { 
     $zip->addFromString(basename($source), file_get_contents($source)); 
    } 

    return $zip->close(); 
} 

넣어 "하지만 지퍼 내가 얻을 다음과 같은 중첩 된 폴더

c:\ 
└─ Program Files (x86) 
└─ www 
    └─ Separalista 
    └─output 
    ├─ folder1 
    │ └─ file.csv 
    └─ folder2 
     └─ file.csv 
내가 zip 파일은 하위 폴더 안에 찾고자하는

"출력 "

output 
    ├─ folder1 
    │ └─ file.csv 
    └─ folder2 
    └─ file.csv 

덕분에 모든

+0

대상을 원본 이외의 폴더로 사용하십시오. 그들이 동일 할 경우, 문제가 발생할 수 있습니다. – Lizz

+0

대상을 변경하려고했지만 항상 동일한 문제가 발생했습니다. –

답변

0

문제에 - $rootpath에 점으로 표시되는 작업 디렉토리는 아무 것도 될 수 없으며 스크립트 자체의 디렉토리로 만들 수 없습니다. 스크립트의 하나, 사용 작업 디렉토리를 변경하려면 :

chdir(dirname (realpath (__FILE__))); 

또는, PHP 5.3.0에서 이상 단순히

chdir(__DIR__); 

스크립트의 시작 부분에.

그래도 문제가 해결되지 않으면 조금 더 변경해야합니다. 다음과 같이 디렉토리에있는 변수에 직접 경로를 첨부하십시오.

$rootpath = __DIR__ . '/Output'; 
$destinazione = $rootpath . '/lista.zip'; 

위의 내용은 파일이 프로젝트의 루트 디렉토리에 있다고 가정합니다. 그렇지 않은 경우 적절하게 $rootpath을 수정하십시오. PHP 5.3.0에서는 __DIR__ 대신 dirname (realpath (__FILE__))을 사용합니다.

+0

죄송 합니다만, 귀하의 제안을 어디에 적용 할 것인지 이해하지 못했습니다. –

+0

@MarcoAttanasio를 스크립트의 첫 번째 줄로 사용하십시오. –

+0

시도했지만 여전히 작동하지 않습니다 –