Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Changing the filenames in a Zip File

    All,
    I'm using the following code to create a zip file:
    PHP Code:
    <?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?

  • #2
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    I figured it out. I updated my code to read like this:

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

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, simply change this:
    PHP Code:
    foreach($valid_files as $file) {
          
    $zip->addFile($file,$file);
        } 
    To this:
    PHP Code:
    $i 0;
    foreach(
    $valid_files as $file) {
        
    $sLocalname = ++$i '_' basename($file);
          
    $zip->addFile($file$sLocalname);
        } 
    And that should do it.

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •