我有一个文件夹,其中包含所有上传的文件,我根据用户ID将每个文件的路径存储在数据库中。现在我想读取该特定用户的所有文件,并希望将所有文件写入压缩文件夹。
我该如何使用PHP?
我的代码:
// Getting file path here
$data = $model->findAll('tree_parent_id=:id', array(':id' => (int) $model->id));
foreach ($data as $type) {
print_r($type->path);
}
答案 0 :(得分:1)
我已经编写了这样的功能,我在我的项目中使用它:
$file: files arary
$zipnam: what to name your zip file
$dir: where to keep your zipped file
$del: should i delete files from source folder after zipping it?
功能:
function zipit($file, $zipnam, $dir, $del=TRUE) {
$filestozip = $file; // FILES ARRAY TO ZIP
$dir = trim($dir); // DIR NAME TO MOVE THE ZIPPED FILES
$zipnam = trim($zipnam);
$zip = new ZipArchive();
$files = $filestozip;
$zip_name = $zipnam.".zip";
$fizip = $dir.$zip_name;
if($zip->open($fizip, ZipArchive::CREATE) === TRUE) {
foreach ($files as $fl) {
if(file_exists($fl)){
$zip->addFromString(basename($fl), file_get_contents($fl));
if($del === TRUE) {
unlink($fl);
}
}
}
$zip->close();
return TRUE;
} else { $zip->close(); return FALSE;}
}
答案 1 :(得分:0)
Yii有一个拉链extension.
$zip = Yii::app()->zip;
$zip->makeZip('./','./toto.zip'); // make an ZIP archive
$zip->extractZip('./toto.zip', './1/'); // extract Zip arc.