Tag: php

how can extract a file zip into a directory using php code?

If you want to extract a file into zip in a directory using php code then you can use this demo code which will work perfectly.Please check
this is demo code which help you to upload zip file and extract this zip in same file with zip folder also.

<?php
function rmdir_recursive($dir) {
    foreach(scandir($dir) as $file) {
        if ('.' === $file || '..' === $file) continue;
        if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
        else unlink("$dir/$file");
    }
 
    rmdir($dir);
}
 
if(!empty($_FILES["file_name"]["name"])) {
    $filename = $_FILES["file_name"]["name"];
    $source = $_FILES["file_name"]["tmp_name"];
    $type = $_FILES["file_name"]["type"];
 
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        }
    }
 
    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }
 
    $path = dirname(__FILE__).'/';  if you can check this path also  
    $filenoext = basename ($filename, '.zip');
    $filenoext = basename ($filenoext, '.ZIP');
 
    $targetdir = $path . $filenoext; 
    $targetzip = $path . $filename; 
    if (is_dir($targetdir))  rmdir_recursive ( $targetdir);
 
    mkdir($targetdir, 0777);
 
    if(move_uploaded_file($source, $targetzip)) {
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);
        if ($x === true) {
            $zip->extractTo($targetdir.'/');
            $zip->close();
 
            unlink($targetzip);
        }
        $message = "Your file is uploaded and unpacked.";
    } else {
        $message = "There is a problem to upload.";
    }
}
 
?>
 
<?php if(isset($message)) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
    <label>Upload a zip file to upload: <input type="file" name="file_name" /></label>
    <br />
    <input type="submit" name="submit" value="Upload" />
</form>

this is code first upload zip file then extract to folder. if you face any problem then please inform me. i will try to you.

Comment

php remove duplicates and sum of them from array

If you want to remove duplicates then sum of them from array then this code will help you. this is code which remove duplicate and sum of them.

$result = array();
foreach ($data as $val) {
    if (!isset($result[$val->reservations_detail_article]))
        $result[$val->reservations_detail_article] = $val;
    else
        $result[$val->reservations_detail_article]['reservations_detail_qte'] += $val['reservations_detail_qte'];
}

$result = array_values($result); 
print_r($result);

If you face any problem then inform me, i will try to help you.

Comment