save images as zip on a custom folder in php

Bruno Alex

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>\n");
    }
    //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);
?>
Tomasz

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python save images into the folder

From Dev

Adding images to folder and save their name in database using php

From Dev

PHP - Yii 2.0 Multiple images are save in folder but not saved in database

From Dev

Creation of zip folder in php

From Dev

save images in specific sdcard folder

From Dev

Android - Save images in an specific folder

From Dev

save images in specific sdcard folder

From Dev

How to zip folder content and save zip with Foldername in same Folder

From Dev

Save images with phimagemanager to custom album?

From Dev

Maven: zip resources and save into specific folder

From Dev

php zip a folder without looping

From Dev

How to save images into specific folder in iOS 9.0

From Dev

Open process and save specific images in related folder

From Dev

How to save images to a new folder matlab

From Dev

PHP script that shows the images in a folder

From Dev

PHP: uploaded images are not found in folder

From Dev

PHP - save a converted image into a folder

From Dev

PHP - save a converted image into a folder

From Dev

Save pictures to custom folder in media library

From Dev

Save image to custom folder android on onPictureTaken

From Dev

Is it possible to save a file in a custom folder inside NSDocumentDirectory

From Dev

force download .zip folder using php?

From Dev

how to add multiple images as attachment in CodeIgniter instead of Zip folder

From Dev

Custom php file in root folder

From Dev

How to view images from protected folder with php?

From Dev

check if a folder contains images of any types in php

From Dev

Insert Images from folder and subfolder php

From Dev

display all images in folder without php

From Dev

cannot display images from a folder using php

Related Related

HotTag

Archive