2012-03-03 3 views
2

ZIP은 요청시 사용자 개인 디렉터리 인 스크립트를 실행하고 있습니다.PHP .ZIP 객체 - str_replace 폴더 경로?

디렉토리에 대한 경로는 다음과 같습니다

../temp/useremail/ 우편이 실제로 다운로드

,이 폴더 경로가 분명 끔찍한 보이는 (그 안에 표시되는 방법도). 내가 않는 str_replace를 사용하여 시도하지만 효과가 있었다

에서 useremail/

: 나는 간단하게 표시 할. 여기에 내 코드가있다 :

$zip = new ZipArchive(); 


$exists = file_exists('../temp/' . $email . '/theme-' . $email . '.zip'); 
$delete = '../temp/' . $email . '/theme-' . $email . '.zip'; 

if($exists) { 
    unlink($delete); 
} 

if ($zip->open('../temp/' . $email . '/theme-' . $email . '.zip', ZIPARCHIVE::CREATE) !== TRUE) { 
    die("Could not open archive"); 
} 

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../temp/$email/")); 

foreach ($iterator as $key => $value) { 
    $_key = str_replace("../temp/'.$email.'/", "", $key); 
    $zip->addFile(realpath($_key), $_key) or die("ERROR: Could not add file: $key"); 
} 

$zip->close(); 
+0

'ZipArchive :: renameName' 시도 - http://www.php.net/manual/en/ziparchive.renamename.php – scibuff

+0

빠른 모양이 있었지만 사용하지 않는 한 도움이되지 않는 것 같습니다. 잘못이야. 현재 스크립트에 어떻게 통합 시키시겠습니까? – tctc91

답변

1

실제로 str_replace를 사용하는 방법은 실제로 작동하지만 간단한 실수를 한 것이다. 이 줄의 내용 :

$ zip-> addFile (realpath ($ _ key), $ _key) 또는 die ("ERROR : 파일을 추가 할 수 없습니다 : $ key");

수정하려면 addFile의 첫 번째 매개 변수를 편집 할 때 $ key (시스템에서 더 긴 경로/실제 위치)와 두 번째 매개 변수 (zip 아카이브의 새 경로)로 유지하십시오.

$zip = new ZipArchive(); 


$exists = file_exists('../temp/' . $email . '/theme-' . $email . '.zip'); 
$delete = '../temp/' . $email . '/theme-' . $email . '.zip'; 

if($exists) { 
    unlink($delete); 
} 

if ($zip->open('../temp/' . $email . '/theme-' . $email . '.zip', ZIPARCHIVE::CREATE) !== TRUE) { 
    die("Could not open archive"); 
} 

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../temp/$email/")); 

foreach ($iterator as $key => $value) { 
    $_key = str_replace("../temp/'.$email.'/", "", $key); 
    $zip->addFile(realpath($key), $_key) or die("ERROR: Could not add file: $key"); 
} 

$zip->close(); 

그게 전부 야!

+0

아무런 차이가없는 것 같습니다. 지퍼 구조가 전혀 변경되지 않았습니다. – tctc91