Jump to content

rar file in php


rahuul

Recommended Posts

This should work

 

<?php
function archive($files = array(), $destination = '', $overwrite = FALSE) {
  if (file_exists($destination) && !$overwrite) {
    return false;
  }
  
  $valid_files = array();
  
  if (is_array($files)) {
    foreach ($files as $file) {
      if (file_exists($file)) {
        $valid_files[] = $file;
      }
    }
  }
  
  if (count($valid_files)) {
    $zip = new ZipArchive();
    
    if ($overwrite) {
      $zipcreate = $zip->open($destination, ZIPARCHIVE::OVERWRITE);
    }
    else {
      $zipcreate = $zip->open($destination, ZIPARCHIVE::CREATE);
    }
    
    if ($zipcreate !== TRUE) {
      return FALSE;
    }
    
    $zip->close();
    
    return file_exists($destination);
  }
  else {
    return FALSE;
  }
}

// Example
$files_to_zip = array('images/1.png', 'images/2.png', 'read.jpg');
$result = archive($files_to_zip, 'my_archive.zip');
?>

Link to comment
https://forums.phpfreaks.com/topic/195564-rar-file-in-php/#findComment-1027639
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.