简体   繁体   中英

Php - Extract zip file with log in a *.csv file

I need a php script that extracts a file (with folders and subfolders) and creates a log in which insert some info about files extracted.

Now the script below extracts a zip file (with folders and subfolders) but inserts only last file extracted info in the csv files. How can i fix it to create a list of all files extracted info.

Thanks for help.

file = "test.zip";
$dir = getcwd();
function Unzip($dir, $file, $destiny="")
{
$dir .= DIRECTORY_SEPARATOR;
$path_file = $dir . $file;
$zip = zip_open($path_file);
$_tmp = array();
$count=0;
if ($zip)
{
    while ($zip_entry = zip_read($zip))
    {
        $_tmp[$count]["filename"] = zip_entry_name($zip_entry);
        $_tmp[$count]["stored_filename"] = zip_entry_name($zip_entry);
        $_tmp[$count]["size"] = zip_entry_filesize($zip_entry);
        $_tmp[$count]["compressed_size"] = zip_entry_compressedsize($zip_entry);
        $_tmp[$count]["mtime"] = "";
        $_tmp[$count]["comment"] = "";
        $_tmp[$count]["folder"] = dirname(zip_entry_name($zip_entry));
        $_tmp[$count]["index"] = $count;
        $_tmp[$count]["status"] = "ok";
        $_tmp[$count]["method"] = zip_entry_compressionmethod($zip_entry);

        if (zip_entry_open($zip, $zip_entry, "r"))
        {
            $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
            if($destiny)
            {
                $path_file = str_replace("/",DIRECTORY_SEPARATOR, $destiny . zip_entry_name($zip_entry));
            }
            else
            {
                $path_file = str_replace("/",DIRECTORY_SEPARATOR, $dir . zip_entry_name($zip_entry));
            }
            $new_dir = dirname($path_file);

            mkdir($new_dir);

            $fp = fopen($dir . zip_entry_name($zip_entry), "w");
            fwrite($fp, $buf);
            fclose($fp);

            zip_entry_close($zip_entry);

        }
        echo "\n</pre>";
        $count++;
        $named = zip_entry_name($zip_entry);                            
        $outputfile = "log.csv";
        if (!$outfilefp = fopen($outputfile, "w"))
        die("<br>No write on $outputfile");
        else
        echo "<br>Add lines to csv file";
        $temp = array($path_file,$new_dir,$named);
        fputcsv($outfilefp, $temp, ";");
    }

    zip_close($zip);
}
}
  Unzip($dir,$file);

Actually it is inserting every one, but you are overwriting the last inserted file each time, so you only see the last one. The mode for fopen should be a not w

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length . If the file does not exist, attempt to create it.

'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM