简体   繁体   English

PHP-使用* .csv文件登录提取zip文件

[英]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. 我需要一个PHP脚本来提取文件(包括文件夹和子文件夹)并创建一个日志,在其中插入一些有关提取文件的信息。

Now the script below extracts a zip file (with folders and subfolders) but inserts only last file extracted info in the csv files. 现在,以下脚本提取了一个zip文件(包括文件夹和子文件夹),但仅将最后一个文件提取的信息插入到csv文件中。 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 对于模式的fopen应该是一个W¯¯

'w' Open for writing only; w只开放写作; 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; 'a'仅用于写作; place the file pointer at the end of the file. 将文件指针放在文件末尾。 If the file does not exist, attempt to create it. 如果该文件不存在,请尝试创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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