반응형
php zip 내부의 파일에 대한 경로 없이 zip 생성
저는 php를 사용하여 zip 파일을 생성하려고 합니다(이 페이지에서 가져온). http://davidwalsh.name/create-zip-php), 하지만 zip 파일 안에는 파일 자체에 대한 모든 폴더 이름이 있습니다.
모든 폴더를 제외하고 zip 안에 파일만 있으면 가능한가요?
제 암호는 이렇습니다.
function create_zip($files = array(), $destination = '', $overwrite = true) {
if(file_exists($destination) && !$overwrite) { return false; };
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$valid_files[] = $file;
};
};
};
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
};
foreach($valid_files as $file) {
$zip->addFile($file,$file);
};
$zip->close();
return file_exists($destination);
} else {
return false;
};
};
$files_to_zip = array('/media/138/file_01.jpg','/media/138/file_01.jpg','/media/138/file_01.jpg');
$result = create_zip($files_to_zip,'/...full_site_path.../downloads/138/138_files.zip');
여기서 문제가 되는 것은$zip->addFile두 개의 파라미터를 동일하게 전달하고 있습니다.
문서에 의하면 다음과 같습니다.
boole ZipArchive::addFile ( 문자열 $filename [, 문자열 $localname ] )
파일명
추가할 파일의 경로입니다.현지명
ZIP 아카이브 내의 로컬 이름.
즉, 첫 번째 매개 변수는 파일 시스템의 실제 파일에 대한 경로이고, 두 번째 매개 변수는 파일이 아카이브에서 가질 경로 및 파일 이름입니다.
두 번째 매개변수를 입력하면 zip 아카이브에 경로를 추가할 때 경로에서 경로를 제거할 수 있습니다.예를 들어, 유닉스 기반 시스템에서는 다음과 같이 나타납니다.
$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename);
더 나은 선택은 다음과 같습니다.
$zip->addFile($file,basename($file));
단순히 경로에서 파일 이름을 추출합니다.
이것은 제가 발견한 또 다른 방법일 뿐입니다.
$zipname = 'file.zip';
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
$download_file = file_get_contents($file);
$zip->addFromString(basename($file),$download_file);
$zip->close();
header('Content-disposition: attachment; filename='.$zipname);
header('Content-type: application/zip');
readfile($tmp_file);
zip에서 루트 폴더를 제거할 때 사용합니다.
D:\xampp\htdocs\myapp\assets\index.php
zip:
assets\index.php
우리 코드:
echo $main_path = str_replace("\\", "/", __DIR__ );// get current folder, which call scipt
$zip_file = 'myapp.zip';
if (file_exists($main_path) && is_dir($main_path))
{
$zip = new ZipArchive();
if (file_exists($zip_file)) {
unlink($zip_file); // truncate ZIP
}
if ($zip->open($zip_file, ZIPARCHIVE::CREATE)!==TRUE) {
die("cannot open <$zip_file>\n");
}
$files = 0;
$paths = array($main_path);
while (list(, $path) = each($paths))
{
foreach (glob($path.'/*') as $p)
{
if (is_dir($p)) {
$paths[] = $p;
} else {
// special here: we remove root folder ("D:\xampp\htdocs\myapp\") :D
$new_filename = str_replace($main_path."/" , "", $p);
$zip->addFile($p, $new_filename);
$files++;
echo $p."<br>\n";
}
}
}
echo 'Total files: '.$files;
$zip->close();
}
언급URL : https://stackoverflow.com/questions/3993105/php-creating-zips-without-path-to-files-inside-the-zip
반응형
'programing' 카테고리의 다른 글
| Android 버튼이 On으로 설정되었습니다.TouchListener가 호출되었지만 performClick을 재정의하지 않습니다. (0) | 2023.09.12 |
|---|---|
| 서피스 뷰와 뷰의 차이점은 무엇입니까? (0) | 2023.09.12 |
| 새로 추가된 입력 요소에 초점을 맞춥니다. (0) | 2023.09.12 |
| MariaDB 쿼리 JSON 결과는 텍스트 열에서 \\"라는 큰따옴표를 벗어납니다. (0) | 2023.09.12 |
| int.TryParse합성당 (0) | 2023.09.12 |