PHP脚本实现Magento权限设置与缓存清理

PHP脚本实现Magento权限设置与缓存清理的实例代码有需要的朋友可参考一下.PHP实例代码如下:

  1. <?php
  2. ## 设置文件644,目录755
  3. function AllDirChmod( $dir = "./", $dirModes = 0755, $fileModes = 0644 ){
  4. $d = new RecursiveDirectoryIterator( $dir );
  5. foreach( new RecursiveIteratorIterator( $d, 1 ) as $path ){
  6. if( $path->isDir() ) chmod( $path, $dirModes );
  7. else if( is_file( $path ) ) chmod( $path, $fileModes );
  8. }
  9. }
  10. ## 清除指定目录
  11. function cleandir($dir) {
  12. if ($handle = opendir($dir)) {
  13. while (false !== ($file = readdir($handle))) {
  14. if ($file != '.' && $file != '..' && is_file($dir.'/'.$file)) {
  15. if (unlink($dir.'/'.$file)) { }
  16. else { echo $dir . '/' . $file . ' (file) NOT deleted!<br />'; }
  17. }
  18. else if ($file != '.' && $file != '..' && is_dir($dir.'/'.$file)) {
  19. cleandir($dir.'/'.$file);
  20. if (rmdir($dir.'/'.$file)) { }
  21. else { echo $dir . '/' . $file . ' (directory) NOT deleted!<br />'; }
  22. }
  23. }
  24. closedir($handle);
  25. }
  26. }
  27. ## 判断目录是否为空
  28. function isDirEmpty($dir){
  29. return (($files = @scandir($dir)) && count($files) <= 2);
  30. }
  31. echo "----------------------- CLEANUP START -------------------------<br/>";
  32. $start = (float) array_sum(explode(' ',microtime()));
  33. echo "<br/>*************** SETTING PERMISSIONS ***************<br/>";
  34. echo "Setting all folder permissions to 755<br/>";
  35. echo "Setting all file permissions to 644<br/>";
  36. AllDirChmod( "." );
  37. echo "Setting pear permissions to 550<br/>";
  38. chmod("pear", 550);
  39. echo "<br/>****************** CLEARING CACHE ******************<br/>";
  40. if (file_exists("var/cache")) {
  41. echo "Clearing var/cache<br/>";
  42. cleandir("var/cache");
  43. }
  44. if (file_exists("var/session")) {
  45. echo "Clearing var/session<br/>";
  46. cleandir("var/session");
  47. }
  48. if (file_exists("var/minifycache")) {
  49. echo "Clearing var/minifycache<br/>";
  50. cleandir("var/minifycache");
  51. }
  52. if (file_exists("downloader/pearlib/cache")) {
  53. echo "Clearing downloader/pearlib/cache<br/>";
  54. cleandir("downloader/pearlib/cache");
  55. }
  56. if (file_exists("downloader/pearlib/download")) {
  57. echo "Clearing downloader/pearlib/download<br/>";
  58. cleandir("downloader/pearlib/download");
  59. }
  60. if (file_exists("downloader/pearlib/pear.ini")) {
  61. echo "Removing downloader/pearlib/pear.ini<br/>";
  62. unlink ("downloader/pearlib/pear.ini");
  63. }
  64. echo "<br/>************** CHECKING FOR EXTENSIONS ***********<br/>";
  65. If (!isDirEmpty("app/code/local/")) {
  66. echo "-= WARNING =- Overrides or extensions exist in the app/code/local folder<br/>";
  67. }
  68. If (!isDirEmpty("app/code/community/")) {
  69. echo "-= WARNING =- Overrides or extensions exist in the app/code/community folder<br/>";
  70. }
  71. $end = (float) array_sum(explode(' ',microtime()));
  72. echo "<br/>------------------- CLEANUP COMPLETED in:". sprintf("%.4f", ($end-$start))." seconds ------------------<br/>";
  73. ?>