【问题标题】:how to delete yii assets through a button at the site admin side如何通过站点管理员侧的按钮删除 yii 资产
【发布时间】:2015-04-10 13:04:07
【问题描述】:

我在我的项目中使用 yii1。 运行项目时,yii 会自动在项目的 assets 文件夹中创建资产文件。有时它会产生一些问题。 我想知道如何在站点管理员端设置一个按钮,以便在单击该按钮时删除所有资产文件。

谢谢

【问题讨论】:

  • 只需向php 文件发出请求,该文件将删除您在jquery 中需要的文件
  • 我可以得到任何示例代码吗?
  • 好的,给你写答案
  • 为什么要删除资产文件夹中的任何内容?这都是由 Yii 管理的,那里应该真的不需要改变什么吧?
  • 我只是想知道方法。这些资产有时会与其他 javascript 插件产生一些冲突。

标签: php yii


【解决方案1】:

这里是清除 yii 1.1 通用结构应用程序的 assets 文件夹的简单功能:

public static function removeAssets()
    {
        $dir = realpath(Yii::app()->basePath.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."assets");
        $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
        $files = new RecursiveIteratorIterator($it,
            RecursiveIteratorIterator::CHILD_FIRST);
        foreach($files as $file) {
            if ($file->getFilename() === '.' || $file->getFilename() === '..') {
                continue;
            }
            if ($file->isDir()){
                rmdir($file->getRealPath());
            } else {
                unlink($file->getRealPath());
            }
        }

        return true;
    }

@stu:Why do you want to delete anything in the assets folder? This is all administered by Yii, there shouldn't really be a need to change anything in there?

例如,如果资产被缓存,并且您对已发布的脚本进行了一些更改,这有时会很有用。

【讨论】:

    【解决方案2】:

    这是你想做的:

    在jquery中你需要调用一个请求,该请求将在按钮点击事件中调用一个php文件

    HTML + Jquery

    <div id='result'></div>
    <button>Delete</delete>
    
    <script src="http://code.jquery.com/jquery-1.7.2.js"></script>
    <script>
    $("button").click(function() {
      $.ajax({
            type: "POST",
            url : "request.php",
            data: { 
            'fire': 'true'
        },
            success : function(data)
            {
            console.log(data);
            $("#result").html(data);
            }
        },"json");
    })
    </script>
    

    PHP

    无论何时调用 php,都会删除 asset 文件夹中的所有文件。

    <?php
    $files = glob('D:\Development\Websites\test\del\asset\*'); //Give real paths here
    foreach($files as $file){ // iterate files
      if(is_file($file))
        unlink($file); // delete file
    }
    echo 'Deleted all files';
    ?>
    

    注意:

    我不知道。由于 OP 想知道在对 php 的简单 jquery 调用中实现它的示例,我编写了这个简单的调用。

    Points to be noted 
    
    1. 我已经给出了本地驱动器的路径,您需要在其中给出目录的绝对路径
    2. 只要 ajax 调用到达 request.php 文件,我就会删除这些文件。您可以根据需要进行更改。

    【讨论】:

    【解决方案3】:

    如果你有 Linux 服务器并且 PHP 有权限运行 shell 命令,你可以尝试使用一行命令递归地删除资产中的所有文件和目录。

    要删除文件,您必须对文件和存储文件的文件夹具有写入权限。文件的 OWNER 不需要 rw 权限即可对其进行 rm。

    shell_exec("rm -rf /var/www/public_html/assets/*");
    

    但要小心使用rm -rf 命令!。

    对于 Windows,您需要 2 个命令:

    shell_exec("RD /S /Q C:\pathto\assets");
    shell_exec("MD C:\pathto\assets");
    

    【讨论】:

    • 更新了 windows 的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多