40 function get_file_type($filename)
42 if (is_string($filename)) {
43 return strtolower( substr( strrchr($filename,
'.') , 1) );
59 function is_image($filename)
62 switch (get_file_type($filename)) {
83 function file_to_string($filename)
85 if (!is_readable($filename))
return FALSE;
86 return file_get_contents($filename);
100 function string_to_file($string, $filename)
102 if (!is_string($filename) || empty($filename)) {
109 if (!is_dir(dirname($filename)))
return FALSE;
111 $tmp_filename = tempnam(dirname($filename), __FUNCTION__);
112 if (!$f = fopen($tmp_filename,
'wb')) {
113 trigger_error(
'Unable to open temporary file ('.$tmp_filename.
') for writing, unable to write '.$filename, E_USER_WARNING);
116 $s = fputs($f, $string);
119 trigger_error(
'Unable to write '.$filename, E_USER_WARNING);
120 unlink($tmp_filename);
125 chmod($tmp_filename, 0664);
130 if ((strtolower(PHP_OS) ==
'winnt') || (strtolower(PHP_OS) ==
'win32')) {
134 if (file_exists($filename)) unlink($filename);
137 if (rename($tmp_filename, $filename)) {
140 unlink($tmp_filename);
158 function array_to_file($value, $var_name, $filename)
160 if (!is_array($value))
return FALSE;
161 $string =
'<?php $'.$var_name.
' = '.var_export($value, TRUE).
'; ?>';
162 return string_to_file($string, $filename);
175 function easy_filesize($size)
178 return $size.
' Bytes';
179 }
else if ($size < 1048576) {
180 return sprintf(
'%.1f KB', $size/1024.0);
181 }
else if ($size < 1073741824) {
182 return sprintf(
'%.1f MB',($size/1024.0)/1024.0);
184 return sprintf(
'%.1f GB',(($size/1024.0)/1024.0)/1024.0);
200 function increment_filename($name,$spacer=
'')
202 require_once dirname(__FILE__).
'/../general/general.inc';
203 if (strpos($name,
'.') !== FALSE) {
204 $ext = get_file_type($name);
205 return increment_name(substr($name,0,-strlen($ext)-1),$spacer).
'.'.$ext;
207 return increment_name($name, $spacer);
224 function list_files($dir, $fullpath=FALSE)
226 if (!$dir)
return FALSE;
230 $restrict = basename(str_replace(
'*',
'',$dir));
231 $dir = dirname($dir);
234 if (!is_dir($dir))
return Array();
237 if ($handle = opendir($dir)) {
238 while (($file = readdir($handle)) !== FALSE) {
239 if ($file ==
'.' || $file ==
'..') {
242 if (is_file($dir.
'/'.$file)) {
243 if ($restrict && (preg_match(
"%$restrict%", $file))) {
244 $files[] = ($fullpath) ? $dir.
'/'.$file : $file;
247 $files[] = ($fullpath) ? $dir.
'/'.$file : $file;
269 function list_dirs($dir, $fullpath=FALSE, $ignore_list=Array(), $recursive=FALSE)
271 if (!is_dir($dir))
return Array();
274 if ($recursive) $fullpath = TRUE;
276 array_push($ignore_list,
'.',
'..');
279 if ($handle = opendir($dir)) {
280 while (($subdir = readdir($handle)) !== FALSE) {
281 if (!is_dir($dir.
'/'.$subdir) || in_array($subdir, $ignore_list)) {
284 $dirs[] = ($fullpath) ? $dir.
'/'.$subdir : $subdir;
286 $dirs = array_merge($dirs, list_dirs($dir.
'/'.$subdir, $fullpath, $ignore_list, TRUE));
306 function create_directory($path)
308 if ((!is_string($path) &&(!is_int($path))) || empty($path)) {
311 if (is_dir($path))
return TRUE;
313 $old_umask = umask(0);
314 if (!mkdir($path, 0775, TRUE)) {
315 trigger_error(
'Unable to create directory: '.$path, E_USER_WARNING);
335 function delete_directory($path)
337 $path = rtrim($path,
'/');
338 if (!is_dir($path)) {
339 trigger_error($path.
' is not a directory (for deletion).', E_USER_WARNING);
343 if (clear_directory($path)) {
348 trigger_error(
'Unable to delete dir: '.$path, E_USER_WARNING);
368 function clear_directory($path, $file_excl = Array())
370 $path = rtrim($path,
'/');
371 if (!is_dir($path)) {
372 trigger_error($path.
' is not a directory (for clearing).');
376 $dir = opendir($path);
377 while (FALSE !== ($filename = readdir($dir))) {
378 if ($filename ==
'.' || $filename ==
'..' || in_array($filename, $file_excl)) {
381 $filename = $path.
'/'.$filename;
382 if (is_dir($filename)) {
383 delete_directory($filename);
385 if (!unlink($filename)) {
386 trigger_error(
'Unable to delete: '.$filename, E_USER_WARNING);
408 function copy_directory($path, $new_path)
410 $path = rtrim($path,
'/');
411 $new_path = rtrim($new_path,
'/');
412 if (!is_dir($path)) {
413 trigger_error($path.
' is not a directory (for copying.)');
417 if (file_exists($new_path)) {
418 if (!is_dir($new_path)) {
419 trigger_error($new_path.
' exists but is not a directory (for copying into).');
424 if (!create_directory($new_path)) {
430 $dir = opendir($path);
431 while (FALSE !== ($filename = readdir($dir))) {
432 if ($filename ==
'.' || $filename ==
'..') {
435 $full_filename = $path.
'/'.$filename;
436 if (is_dir($full_filename)) {
437 if (!copy_directory($full_filename, $new_path.
'/'.$filename)) {
442 if (!copy($full_filename, $new_path.
'/'.$filename)) {
443 trigger_error(
'Unable to copy: '.$full_filename.
' --> '.$new_path.
'/'.$filename);
465 function copy_file($from, $to)
467 if (!create_directory(dirname($to)))
return FALSE;
469 if (!copy($from, $to))
return FALSE;
484 function move_file($from, $to)
486 if (!copy_file($from, $to))
return FALSE;
487 if (!unlink($from))
return FALSE;
501 function truncate_file($file)
503 $fp = fopen($file,
'r+');
505 if (!ftruncate($fp, 0))
return FALSE;
527 function get_last_lines_from_file($filepath, $max_lines=10, $line_ending=
"\n")
530 if (!is_string($filepath) || !is_int($max_lines) || !is_string($line_ending)) {
535 $max_lines = is_int($max_lines) ? $max_lines : 10;
537 if (!file_exists($filepath) || !is_readable($filepath)) {
541 if (!$handle = fopen($filepath,
'r'))
return FALSE;
544 fseek($handle, -1, SEEK_END);
545 $pos = ftell($handle);
546 if ($pos === FALSE)
return FALSE;
549 $current_word = NULL;
555 $char = fgetc($handle);
556 if (0 != fseek($handle, -2, SEEK_CUR)) $ok = FALSE;
558 if ($char == $line_ending) {
559 if (is_null($current_word)) {
562 array_unshift($lines, $current_word);
564 $current_word = NULL;
567 }
else if ($char === FALSE) {
572 $current_word = $char.$current_word;
576 }
while ($max_lines > $line_count && $ok);
578 if (!is_null($current_word) && $max_lines > $line_count) {
579 array_unshift($lines, $current_word);