Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
FileUtils.php
Go to the documentation of this file.
1<?php
3
4class FileUtils {
5 public static function deleteAllFilesFromFolder($folder){
6 $files = glob($folder.'/*');
7 foreach($files as $file){
8 if(is_file($file))
9 unlink($file);
10 }
11 }
12
13 public static function openFile($filename){
14 if(file_exists($filename)){
15 return file_get_contents($filename);
16 }
17 return false;
18 }
19
20 public static function writeFile($filename,$data){
21 return file_put_contents($filename,$data);
22 }
23
24 public static function xcopy($source, $dest, $permissions = 0755){
25 $path = pathinfo($dest);
26 if (!file_exists($path['dirname'])) {
27 mkdir($path['dirname'], 0777, true);
28 }
29 if (is_link($source)) {
30 return symlink(readlink($source), $dest);
31 }
32 if (is_file($source)) {
33 return copy($source, $dest);
34 }
35 $dir = dir($source);
36 while (false !== $entry = $dir->read()) {
37 if ($entry == '.' || $entry == '..') {
38 continue;
39 }
40 self::xcopy("$source/$entry", "$dest/$entry", $permissions);
41 }
42 $dir->close();
43 return true;
44 }
45
46 public static function delTree($dir) {
47 $files = array_diff(scandir($dir), array('.','..'));
48 foreach ($files as $file) {
49 (is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
50 }
51 return rmdir($dir);
52 }
53
54 public static function safeMkdir($dir){
55 if(!is_dir($dir))
56 return mkdir($dir,0777,true);
57 }
58
59 public static function cleanPathname($path){
60 if($path!==null & $path!==""){
61 if(DS==="/")
62 $path=\str_replace("\\", DS, $path);
63 else
64 $path=\str_replace("/", DS, $path);
65 $path=\str_replace(DS.DS, DS, $path);
66 if(!substr($path, -1)=== DS){
67 $path=$path.DS;
68 }
69 }
70 return \realpath($path);
71 }
72
73 public static function systemCommandExists($command) :bool {
74 $windows = \strpos(PHP_OS, 'WIN') === 0;
75 $test = $windows ? 'where' : 'command -v';
76 $commands=explode("\n",\trim(\shell_exec("$test $command")));
77 foreach ($commands as $cmd){
78 if($cmd!=null && \file_exists($cmd)){
79 return true;
80 }
81 }
82 return false;
83 }
84}
static deleteAllFilesFromFolder($folder)
Definition FileUtils.php:5
static systemCommandExists($command)
Definition FileUtils.php:73
static xcopy($source, $dest, $permissions=0755)
Definition FileUtils.php:24
static writeFile($filename, $data)
Definition FileUtils.php:20