codingtcher.com
Theme Night
Home
Login

Php create directory

<?php
class FlxZipArchive extends ZipArchive{
 public function addDir($location, $name){
       $this->addEmptyDir($name);
       $this->addDirDo($location, $name);
 }
 private function addDirDo($location, $name){
    $name .= '/';
    $location .= '/';
    $dir = opendir ($location);
    while($file = readdir($dir)){
        if($file == '.' || $file == '..') continue;
        $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
        $this->$do($location . $file, $name . $file);
    }
  } 
}
?>

function createZip($arrayFiles,$nameofzip,$download=false){
if(class_exists('ZipArchive')){
foreach($arrayFiles as $dirfile){
$za = new FlxZipArchive;
$res = $za->open($nameofzip, ZipArchive::CREATE);
if($res === TRUE){
    if(is_dir($dirfile)){
    $za->addDir($dirfile, basename($dirfile));
    }
    else{
     if(is_file($dirfile)){
     $za->addFile($dirfile, basename($dirfile));
     }
    }
    $za->close();
}
else{return false; exit;}
}
if($download==true && is_file($nameofzip)){
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$nameofzip);
    header('Content-Length: '.filesize($nameofzip));
    readfile($nameofzip);
}
}else{
    return false;
}
}
$rootdir = $_SERVER["DOCUMENT_ROOT"];
$arraydirfile = [$rootdir.'/file1.txt',$rootdir.'/file2.php',$rootdir.'/dir1',$rootdir.'/dir2'];
$zip_file_name = $_SERVER["DOCUMENT_ROOT"].'/new.zip';

// create a zip file.
createZip($arraydirfile,$zip_file_name);

// create zip and download it.
createZip($arraydirfile,$zip_file_name,true);

function createfilesZip($arrayFiles,$nameofzip,$download=false){
if(class_exists('ZipArchive')){
$zip = new ZipArchive;
$res = $zip->open($nameofzip, ZipArchive::CREATE);
if($res === TRUE){
foreach ($arrayFiles as $file) {
  if(is_file($file))
  $zip->addFile($file);
}
$zip->close();
}
else{return false; exit;}
if($download==true && is_file($nameofzip)){
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$nameofzip);
header('Content-Length: '.filesize($nameofzip));
readfile($nameofzip);
}
} else{
    return false;
}
}
$rootdir = $_SERVER["DOCUMENT_ROOT"];
$arraydirfile = [$rootdir.'/file1.txt',$rootdir.'/file2.php'];
$zip_file_name = $_SERVER["DOCUMENT_ROOT"].'/newfile.zip';

// create a zip file.
createfilesZip($arraydirfile,$zip_file_name);

// create zip and download it.
createfilesZip($arraydirfile,$zip_file_name,true);
//php mkdir
// Create a directory with the permission level (optional)
<?php
mkdir("/path/to/my/dir", 0700);
?>

Related Code