문제

I have this piece of code..,everything works fine ,but an issue with renaming

     $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

Okay i figured it out

Zip creates a lock on the file..you cant rename on the fly ,close it and rename again

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

and open zip again and rename

다른 팁

Since you do not want to effect the original file I would think that you are going to need to include a copy statement. Something like;

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

if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top