read()) { if ($file != '.' && $file != '..') { if (is_dir("$dirName$file")) { $size += Files::dirSize($dirName . '/' . $file); } else { $size += filesize($dirName . '/' . $file); } } } $dir->close(); return $size; } /** * Escape the filenames, any non-word characters will be * replaced by an underscore. * @param string $filename the orginal filename * @return string the escaped safe filename */ function escape($filename) { Return preg_replace('/[^\w\._]/', '_', $filename); } /** * Delete a file. * @param string $file file to be deleted * @return boolean true if deleted, false otherwise. */ function delFile($file) { if(is_file($file)) Return unlink($file); else Return false; } /** * Delete folder(s), can delete recursively. * @param string $folder the folder to be deleted. * @param boolean $recursive if true, all files and sub-directories * are delete. If false, tries to delete the folder, can throw * error if the directory is not empty. * @return boolean true if deleted. */ function delFolder($folder, $recursive=false) { $deleted = true; if($recursive) { $d = dir($folder); while (false !== ($entry = $d->read())) { if ($entry != '.' && $entry != '..') { $obj = Files::fixPath($folder).$entry; //var_dump($obj); if (is_file($obj)) { $deleted &= Files::delFile($obj); } else if(is_dir($obj)) { $deleted &= Files::delFolder($obj, $recursive); } } } $d->close(); } //$folder= $folder.'/thumbs'; //var_dump($folder); if(is_dir($folder)) $deleted &= rmdir($folder); else $deleted &= false; Return $deleted; } /** * Append a / to the path if required. * @param string $path the path * @return string path with trailing / */ function fixPath($path) { //append a slash to the path if it doesn't exists. if(!(substr($path,-1) == '/')) $path .= '/'; Return $path; } /** * Concat two paths together. Basically $pathA+$pathB * @param string $pathA path one * @param string $pathB path two * @return string a trailing slash combinded path. */ function makePath($pathA, $pathB) { $pathA = Files::fixPath($pathA); if(substr($pathB,0,1)=='/') $pathB = substr($pathB,1); Return Files::fixPath($pathA.$pathB); } /** * Similar to makePath, but the second parameter * is not only a path, it may contain say a file ending. * @param string $pathA the leading path * @param string $pathB the ending path with file * @return string combined file path. */ function makeFile($pathA, $pathB) { $pathA = Files::fixPath($pathA); if(substr($pathB,0,1)=='/') $pathB = substr($pathB,1); Return $pathA.$pathB; } /** * Format the file size, limits to Mb. * @param int $size the raw filesize * @return string formated file size. */ function formatSize($size) { if($size < 1024) return $size.' bytes'; else if($size >= 1024 && $size < 1024*1024) return sprintf('%01.2f',$size/1024.0).' Kb'; else return sprintf('%01.2f',$size/(1024.0*1024)).' Mb'; } } ?>