使用php添加和重命名Zip文件

时间:2013-11-04 06:13:01

标签: php zip rename file-rename

我有这段代码..,一切正常,但重命名问题

     $zip = new ZipArchive();
        $zipPath = 'images/userfiles/'.$company_details->company_name.'_products.zip';

            $emptydir = $company_details->company_name.'_product_logos';

            if ($zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
                $new_filename = substr($my_file, strrpos($my_file, '/') + 1);
                $zip->addFile($my_file, $new_filename);

                    $zip->addEmptyDir($emptydir);

                    foreach($parts_list as $pl) {   
                     if(!empty($pl['part_image'])){     
                        if (file_exists($_SERVER['DOCUMENT_ROOT'] .  $baseurl . '/images/group-logo/' . $pl['part_image'])) { 

                                $img_file = 'images/group-logo/' .  $pl['part_image'];
                                $new_filename2 = substr($img_file, strrpos($img_file, '/') + 1);                          
                                $zip->addFile($img_file,$emptydir . '/' . $new_filename2);                                 

            /*********the problem here******
                 Is there any way to rename the file that i added on the previous line to something else ,already tried zip rename and renameindex but not working  

    for example i want to rename the file $new_filename2 to $new_filename2.'something' with out affecting the original file's name

P.S the files to be renamed are inside another folder in the zip


           *******************************/



                           }
                        }
                    }


                $zip->close();
            }

2 个答案:

答案 0 :(得分:0)

由于您不想影响原始文件,我认为您需要包含一个复制语句。喜欢的东西;

$file = '$new_filename2';
$newfile = '$new_filename2.zip';

if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";

答案 1 :(得分:0)

好的,我想出来了

Zip会在文件上创建一个锁..你无法动态重命名,关闭它并重新命名

$zip->addFile('.....');
....
$zip->close();

再次打开zip并重命名