在php中的自定义文件夹上将图像保存为zip

im trying to save my images on a zip file, my code works fine but saves all images inside the folder /wp-content/uploads/2016/03/... and i want to have my own "images" folder on the zip with all the content on it... i tried the other solutions posted on stackoverflow but i cant get it to work

my code :

<?php
//This script is developed by www.webinfopedia.com
//For more examples in php visit www.webinfopedia.com
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
    $zip = new ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>
");
    }
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files,$files);
        //echo $file_path.$files,$files."<br />";
    }
    $zip->close();
    //then send the headers to foce download the zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    exit;
}


//------------------------------------------------------------------------------------------------------
//If you are passing the file names to thae array directly use the following method
$file_names = array('../wp-content/uploads/2016/03/IMG_8121-900x1200.jpg','../wp-content/uploads/2016/03/IMG_8124-900x1200.jpg','../wp-content/uploads/2016/03/IMG_8106-900x1200.jpg');

//------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------



//Archive name
$archive_file_name=$name.'DEMOphpCreateZipTodownloadMultipleFiles.zip';
//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/images/';

//cal the function
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>

Simply change:

$zip->addFile($file_path.$files,$files);

into:

$zip->addFile($file_path.$files, 'pictures' . DIRECTORY_SEPARATOR . pathinfo($files)['basename']);

Second parameter for addFile method is how file will look like inside archive so you can define new path and filename.

If i got it right, you want folder 'images' inside zip archive.

try something like this:

public function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
    $zip = new \ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZipArchive::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>
");
    }
    $zip->addEmptyDir("images");        
    //add each files of $file_name array to archive
    foreach($file_names as $files)
    {
        $zip->addFile($file_path.$files, "images".DIRECTORY_SEPARATOR.$files);
    }
    $zip->close();
    ...
}

it ads empty folder into your zip a then specifies file path within zip when adding in foreach loop