0) ? @rmdir($path) : true; } } // ------------------------------------------------------------------------ if (! function_exists('get_filenames')) { /** * Get Filenames * * Reads the specified directory and builds an array containing the filenames. * Any sub-folders contained within the specified path are read as well. * * @param string path to source * @param bool whether to include the path as part of the filename * @param bool internal variable to determine recursion status - do not use in calls * @return array */ function get_filenames($source_dir, $include_path = false, $_recursion = false) { static $_filedata = array(); if ($fp = @opendir($source_dir)) { // reset the array and make sure $source_dir has a trailing slash on the initial call if ($_recursion === false) { $_filedata = array(); $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } while (false !== ($file = readdir($fp))) { if (is_dir($source_dir . $file) && $file[0] !== '.') { get_filenames($source_dir . $file . DIRECTORY_SEPARATOR, $include_path, true); } elseif ($file[0] !== '.') { $_filedata[] = ($include_path === true) ? $source_dir . $file : $file; } } closedir($fp); return $_filedata; } return false; } } // -------------------------------------------------------------------- if (! function_exists('get_dir_file_info')) { /** * Get Directory File Information * * Reads the specified directory and builds an array containing the filenames, * filesize, dates, and permissions * * Any sub-folders contained within the specified path are read as well. * * @param string path to source * @param bool Look only at the top level directory specified? * @param bool internal variable to determine recursion status - do not use in calls * @return array */ function get_dir_file_info($source_dir, $top_level_only = true, $_recursion = false) { static $_filedata = array(); $relative_path = $source_dir; if ($fp = @opendir($source_dir)) { // reset the array and make sure $source_dir has a trailing slash on the initial call if ($_recursion === false) { $_filedata = array(); $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast while (false !== ($file = readdir($fp))) { if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false) { get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true); } elseif ($file[0] !== '.') { $_filedata[$file] = get_file_info($source_dir . $file); $_filedata[$file]['relative_path'] = $relative_path; } } closedir($fp); return $_filedata; } return false; } } // -------------------------------------------------------------------- if (! function_exists('get_file_info')) { /** * Get File Info * * Given a file and path, returns the name, path, size, date modified * Second parameter allows you to explicitly declare what information you want returned * Options are: name, server_path, size, date, readable, writable, executable, fileperms * Returns FALSE if the file cannot be found. * * @param string path to file * @param mixed array or comma separated string of information returned * @return array */ function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date')) { if (! file_exists($file)) { return false; } if (is_string($returned_values)) { $returned_values = explode(',', $returned_values); } foreach ($returned_values as $key) { switch ($key) { case 'name': $fileinfo['name'] = basename($file); break; case 'server_path': $fileinfo['server_path'] = $file; break; case 'size': $fileinfo['size'] = filesize($file); break; case 'date': $fileinfo['date'] = filemtime($file); break; case 'readable': $fileinfo['readable'] = is_readable($file); break; case 'writable': $fileinfo['writable'] = is_really_writable($file); break; case 'executable': $fileinfo['executable'] = is_executable($file); break; case 'fileperms': $fileinfo['fileperms'] = fileperms($file); break; } } return $fileinfo; } } // -------------------------------------------------------------------- if (! function_exists('get_mime_by_extension')) { /** * Get Mime by Extension * * Translates a file extension into a mime type based on config/mimes.php. * Returns FALSE if it can't determine the type, or open the mime config file * * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience * It should NOT be trusted, and should certainly NOT be used for security * * @param string $filename File name * @return string */ function get_mime_by_extension($filename) { static $mimes; if (! is_array($mimes)) { $mimes = get_mimes(); if (empty($mimes)) { return false; } } $extension = strtolower(substr(strrchr($filename, '.'), 1)); if (isset($mimes[$extension])) { return is_array($mimes[$extension]) ? current($mimes[$extension]) // Multiple mime types, just give the first one : $mimes[$extension]; } return false; } } // -------------------------------------------------------------------- if (! function_exists('symbolic_permissions')) { /** * Symbolic Permissions * * Takes a numeric value representing a file's permissions and returns * standard symbolic notation representing that value * * @param int $perms Permissions * @return string */ function symbolic_permissions($perms) { if (($perms & 0xC000) === 0xC000) { $symbolic = 's'; // Socket } elseif (($perms & 0xA000) === 0xA000) { $symbolic = 'l'; // Symbolic Link } elseif (($perms & 0x8000) === 0x8000) { $symbolic = '-'; // Regular } elseif (($perms & 0x6000) === 0x6000) { $symbolic = 'b'; // Block special } elseif (($perms & 0x4000) === 0x4000) { $symbolic = 'd'; // Directory } elseif (($perms & 0x2000) === 0x2000) { $symbolic = 'c'; // Character special } elseif (($perms & 0x1000) === 0x1000) { $symbolic = 'p'; // FIFO pipe } else { $symbolic = 'u'; // Unknown } // Owner $symbolic .= (($perms & 0x0100) ? 'r' : '-') . (($perms & 0x0080) ? 'w' : '-') . (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-')); // Group $symbolic .= (($perms & 0x0020) ? 'r' : '-') . (($perms & 0x0010) ? 'w' : '-') . (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-')); // World $symbolic .= (($perms & 0x0004) ? 'r' : '-') . (($perms & 0x0002) ? 'w' : '-') . (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-')); return $symbolic; } } // -------------------------------------------------------------------- if (! function_exists('octal_permissions')) { /** * Octal Permissions * * Takes a numeric value representing a file's permissions and returns * a three character string representing the file's octal permissions * * @param int $perms Permissions * @return string */ function octal_permissions($perms) { return substr(sprintf('%o', $perms), -3); } }