...

View Full Version : Changing the filenames in a Zip File



treeleaf20
07-10-2012, 10:16 PM
All,
I'm using the following code to create a zip file:


<?php
session_start();
include "config.php";
/* 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
echo "The file is: ".$file."<br>";
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
echo "The valid files are: ".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;
}
}


$other_files_to_zip = array();
//$user_id = mysql_real_escape_string($_POST['user_id']);
$user_id = '1';
$qry = "Select filename from uploaded_files where user_id='$user_id' and upload_type='video_montage' order by sort ASC";
$resultqry = mysql_query($qry);
while($resultset = mysql_fetch_array($resultqry)){
array_push($other_files_to_zip, 'upload/'.$resultset['filename']);
}
//$files_to_zip = array('upload/1_3266_671641323389_14800358_42187034_1524052_n.jpg', 'upload/1_3266_671641328379_14800358_42187035_3071342_n.jpg');
//if true, good; if false, zip creation failed
$zip_name = 'download.zip';
$result = create_zip($other_files_to_zip ,$zip_name,true);


My issue is that the code above is creating the filenames in the zip file with the same filename in my database. What I would like to do is add a value on the front of the filename for each one. So for each one I'd like the filename to look like something like the following:

1_filename.jpg
2_filename.jpg
...
10_filename.jpg
11_filename.jpg

With the number on the front being the number of the file that is being added to my zip file. Is there anyway to do this with my above code?

treeleaf20
07-10-2012, 10:33 PM
I figured it out. I updated my code to read like this:



$x=1;
foreach($valid_files as $file) {
$new_file = substr($file, strpos($file, "/") + 1);
$new_file = $x."_".$new_file;
$zip->addFile($file,$new_file);
$x++;
}

Fou-Lu
07-10-2012, 10:52 PM
Yep, simply change this:


foreach($valid_files as $file) {
$zip->addFile($file,$file);
}


To this:


$i = 0;
foreach($valid_files as $file) {
$sLocalname = ++$i . '_' . basename($file);
$zip->addFile($file, $sLocalname);
}

And that should do it.


Jeez I left that open for a long time. Too bad work gets in the way at times :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum