php zipArchive unzip only certain extensions

前端 未结 2 1399
小蘑菇
小蘑菇 2020-12-18 16:01

I\'m in need of unziping uploaded content. But for security purposes must verify the files are only image files so that somebody can\'t add a php into the zip and then run i

2条回答
  •  悲&欢浪女
    2020-12-18 16:46

    from php.net, handling .txt files

    open($filename) === true) {
          echo "Generating TEXT file.";
              for($i = 0; $i < $zip->numFiles; $i++) { 
                 $entry = $zip->getNameIndex($i);
                   if(preg_match('#\.(txt)$#i', $entry))
                    {
                    ////This copy function will move the entry to the root of "txt_files" without creating any sub-folders unlike "ZIP->EXTRACTO" function.
                     copy('zip://'.dirname(__FILE__).'/zip_files/'.$value.'#'.$entry, 'txt_files/'.$value.'.txt'); 
                    } 
                  }  
                 $zip->close();
                }
        else{
             echo "ZIP archive failed";
            }
    ?>
    

提交回复
热议问题