00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 if (!function_exists("file_get_contents")) {
00018 function file_get_contents($filename) {
00019 if (file_exists($filename)) {
00020 $fp = fopen($filename, "r");
00021 $contents = fread($fp, filesize($filename));
00022 fclose($fp);
00023 return $contents;
00024 } else {
00025 return false;
00026 }
00027 }
00028 }
00029
00030 if (!function_exists("file_put_contents")) {
00031 function file_put_contents($filename, $content) {
00032 touch($filename);
00033 if (is_writable($filename)) {
00034 if (!$handle = fopen($filename, 'a')) {
00035 return false;
00036 }
00037
00038 if (fwrite($handle, $content) === FALSE) {
00039 return false;
00040 }
00041
00042 fclose($handle);
00043 return true;
00044
00045 } else {
00046 return false;
00047 }
00048 }
00049 }
00050
00058 function fileweight( $filename ) {
00059
00060 $a = array("B", "KB", "MB", "GB", "TB", "PB");
00061 $pos = 0;
00062 $size = filesize($filename);
00063 while ($size >= 1024) {
00064 $size /= 1024;
00065 $pos++;
00066 }
00067 return round($size,2) . " " . $a[$pos];
00068 }
00069
00078 function rmdirr($dirname)
00079 {
00080
00081 if (!file_exists($dirname)) {
00082 return false;
00083 }
00084
00085
00086 if (is_file($dirname)) {
00087 return unlink($dirname);
00088 }
00089
00090
00091 $dir = dir($dirname);
00092 while (false !== $entry = $dir->read()) {
00093
00094 if ($entry == '.' || $entry == '..') {
00095 continue;
00096 }
00097
00098
00099 if (is_dir("$dirname/$entry")) {
00100 rmdirr("$dirname/$entry");
00101 } else {
00102 unlink("$dirname/$entry");
00103 }
00104 }
00105
00106
00107 $dir->close();
00108 return rmdir($dirname);
00109 }
00110
00114 function unlink_multiple($str) {
00115 foreach(glob($str) as $fn) {
00116 if (!unlink($fn)) return false;
00117 }
00118 return true;
00119 }
00120
00125 function rename_multiple($from, $target, $needle) {
00126 $dir = dir($from);
00127 while (false !== $entry = $dir->read()) {
00128
00129 if ($entry == '.' || $entry == '..' || strpos($entry, $needle) === false) {
00130 continue;
00131 } else {
00132 if (!rename("$from/$entry", "$target/$entry")) return false;
00133 }
00134 }
00135 $dir->close();
00136 return true;
00137 }
00138
00139
00144 function RecursiveMkdir($path) {
00145
00146
00147
00148 if (!file_exists($path))
00149 {
00150
00151
00152 RecursiveMkdir(dirname($path));
00153
00154 mkdir($path, 0775);
00155 }
00156 return true;
00157 }
00158
00167 function suggestFilename( $filename, $destination )
00168 {
00169 $origin = $filename;
00170 $fulldest = $destination . "/" . $origin;
00171 $newfilename = $origin;
00172
00173 $fileext = (strpos($origin,'.')===false?'':'.'.substr(strrchr($origin, "."), 1));
00174 for ($i=1; file_exists($fulldest); $i++)
00175 {
00176 $newfilename = substr($origin, 0, strlen($origin)-strlen($fileext)).'['.$i.']'.$fileext;
00177 $fulldest = $destination . "/" . $newfilename;
00178 }
00179
00180 return $newfilename;
00181 }
00182
00183 if ( ! function_exists ( 'mime_content_type' ) )
00184 {
00185 function mime_content_type ( $f )
00186 {
00187 return exec ( trim( 'file -bi ' . escapeshellarg ( $f ) ) ) ;
00188 }
00189 }
00197 function checkImageMimetype( $filename ) {
00198 $mimetype = mime_content_type( $filename );
00199 switch( $mimetype ) {
00200 case "image/jpeg":
00201 return "jpg";
00202 case "image/gif":
00203 return "gif";
00204 case "image/png":
00205 return "png";
00206 default:
00207 return false;
00208 }
00209 }
00210
00211 ?>