Create .zip file from list of file names

I have a mysql table which holds data on cars. There is a column in the table which holds names of image files of each car. I need to find a way to take get a selection of these images to copy into a .zip file which I will then ftp to another server.

I can get a script to produce a list of all the image names I need to be included in this .zip file but I don’t know how I would then go about creating this .zip file.

any help with this would be much appreciated. Thanks in advance.

Try this: Create a Zip File Using PHP

i.e.

<?php

// ...

$result = mysql_query("SELECT filepath FROM table");
$files_to_zip = array();
while ($row = mysql_fetch_assoc($result))
{
	$files_to_zip[] = $row['filepath'];
	
}
mysql_free_result($result);

$result = create_zip($files_to_zip,'my-archive.zip');

?>

Thanks for the reply on this, I’ve tried using the code you suggested but I’m obviously doing something wrong because I’m getting errors.

The code I’m trying to use is below, can anyone help me with this please?



$server='localhost';
$login='XXXX';
$password='XXXX';
$db='XXXX';
$table='XXXX';

mysql_connect($server, $login, $password);
mysql_select_db($db);


$result = mysql_query("SELECT Pic_Refs FROM $table WHERE Feed_Id IN ('1832208', '2678574', '1127', '53224') AND Picture_Refs!=''");

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
	//if the zip file already exists and overwrite is false, return false
	if(file_exists($destination) && !$overwrite) { return false; }
	//vars
	$valid_files = array();
	//if files were passed in...
	if(is_array($files)) {
		//cycle through each file
		foreach($files as $file) {
			//make sure the file exists
			if(file_exists($file)) {
				$valid_files[] = $file;
			}
		}
	}
	//if we have good files...
	if(count($valid_files)) {
		//create the archive
		$zip = new ZipArchive();
		if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
			return false;
		}
		//add the files
		foreach($valid_files as $file) {
			$zip->addFile($file,$file);
		}
		//debug
		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
		
		//close the zip -- done!
		$zip->close();
		
		//check to make sure the file exists
		return file_exists($destination);
	}
	else
	{
		return false;
	}
}


// ...


$files_to_zip = array();

while ($row = mysql_fetch_assoc($result))

{

    $files_to_zip[] = $row['filepath'];

    

}

mysql_free_result($result);



$result = create_zip($files_to_zip,'my-archive.zip');