NK143
NK143

Reputation: 53

how to delete yii assets through a button at the site admin side

I am using yii1 in my project. When running the project then yii automatically creates assets files in the assets folder of project. some times it creates some issues. I want to know that how can I set a button at the site admin-side to delete all the assets files when click on that button.

thanks

Upvotes: 1

Views: 1122

Answers (3)

Alexander R.
Alexander R.

Reputation: 1756

In case you have Linux server and PHP has permissions to run shell commands, you can try one-line command to remove recursively all files and dirs in assets.

To remove a file you must have write permission on the file and the folder where it is stored. The OWNER of a file does not need rw permissions in order to rm it.

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

But be careful usin rm -rf command!.

For Windows you need 2 commands:

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

Upvotes: 1

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

Here is you wanted to do :

In the jquery you need to call a request that will call a php file in the button click event

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

The php whenever called it, will delete all the files that is inside the asset folder.

<?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';
?>

Note :

I don't know yii. As the OP wants to know the example to achieve it in simple jquery call to php i have written this simple call.

Points to be noted 
  1. I have given the path to my local drive where you need to give your absolute path of the directory
  2. I am just deleting the files whenever the ajax calls reaches request.php file. You can change it according to your need.

Upvotes: 1

ineersa
ineersa

Reputation: 3445

Here is simple function to clear assets folder for yii 1.1 application with common structure:

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?

It's usefull sometimes if assets are cached for example, and you made some changes to published scripts.

Upvotes: 1

Related Questions