dongyi1524 2019-04-12 05:11
浏览 196

如何查看和删除目录中的zip文件和文件夹

I am trying to get the list of all the zip files in my particular directory for ex.

C:/xampp/htdocs

But most important thing is that I would like to get all the zip file from all the folders means it should check in all the sub folders.

After getting all the zip file, I would like to delete zip file on click of delete button.

I have tried following example by googling but not working. I have also tried other solution also.

foreach (glob("*.zip") as $filename) {
 if(unlink($filename))
        echo "success" ;
 else
        echo "Failure"
}

OR

  <?php 
function list_zipfiles($mydirectory) {

    // directory we want to scan
    $dircontents = scandir('c:/xampp/htdocs');

    // list the contents
    echo '<ul>';
    foreach ($dircontents as $file) {
        $extension = pathinfo($file, PATHINFO_EXTENSION);
        if ($extension == 'zip') {
            echo "<li>$file </li>";
        }
    }
    echo '</ul>';
}
  • 写回答

1条回答 默认 最新

  • dongyan6910 2019-04-12 05:24
    关注

    This will list all the zip files into the particular directory.

    For testing you can create test folder and add some inner folder into it and put zip files. So that it will not take long to scan and display all the files.

    <?php
    
    $mydirectory    = 'C:/xampp/htdocs/test';
    
    
    // create recursive directory iterator for zip folder
    $directory_iterator = new RecursiveDirectoryIterator($mydirectory); 
    // required to iterate through results using foreach
    $recursive_iterator = new RecursiveIteratorIterator($directory_iterator);
    // filter only .zip files
    $regex_iterator = new RegexIterator($recursive_iterator, '/^.*\.zip$/',
                              RegexIterator::GET_MATCH);
    
    // iterate through results
    foreach($regex_iterator as $file_info) {
        var_dump($file_info);
    }
    

    To remove file

    use unlink() function to remove the files. On click of particular link should take you to the php page where you can get the file name and remove it.

    <?php
    
        if(isset($_POST) && !empty($_POST) && $_SERVER['REQUEST_METHOD'] === 'POST'){
    
            if (!empty($_POST['zip_file'])) {
                if (!unlink($_POST['zip_file']))
                {
                   echo ("file cannot be deleted due to an error");
                }
                else
                {
                   echo ("file has been deleted");
                }
            }
            header('location:zip_delete_demo.php');
            die();
        }
    
    ?>
    
        <html>
        <body>
            <form method="post" name="zip_form" id="zip_form">
            <?php
            $mydirectory    = 'C:\xampp\htdocs\php_demo';
            $directory_iterator = new RecursiveDirectoryIterator($mydirectory); 
            $recursive_iterator = new RecursiveIteratorIterator($directory_iterator);
            $regex_iterator = new RegexIterator($recursive_iterator, '/^.*\.zip$/',RegexIterator::GET_MATCH);
    
            foreach($regex_iterator as $file_info) {
                echo '<a class="btn_delete_zip" href="javascript:void(0);" data-href="'.$file_info[0].'">'.$file_info[0].'</a><br/>';
    ?>
    
      <?php          
            }
    
            ?>
    
            </form>
        </body>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $(document).on('click','.btn_delete_zip',function(){
                    var zip_file = jQuery(this).data('href');
    
                    var input = $("<input>")
                   .attr("name", "zip_file")
                   .attr("type", "hidden")
                   .val(zip_file);
    
                    $('#zip_form').append(input);
                    $('#zip_form').submit();
    
    
                });
            });
        </script>
        </html>
    

    Notes

    Please increase the maximum execution time in php.ini file. Please don't test your code by putting any exist or die function. It will list out only single file.

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序