* $unit = Files::convertUnitFileSize(146432); * * Result: * * 143 KB * * * @param int $size * @return string */ public static function convertUnitFileSize($size) { $units = [" B", " KB", " MB", " GB"]; $calculatedSize = $size; $unit = Arrays::first($units); if ($size) { $calculatedSize = round($size / pow(1024, ($i = (int)floor(log($size, 1024)))), 2); $unit = $units[$i]; } return $calculatedSize . $unit; } /** * Returns a size of the given file. * * @param string $path * @return int */ public static function size($path) { return (int)self::exists($path) ? filesize($path) : 0; } /** * Checks if the given file exists. * * @param string $path * @return bool */ public static function exists($path) { return file_exists($path); } /** * Returns all files from the given directory that have the given extension. * * @param string $dir * @param string $extension * @return array */ public static function getFilesRecursivelyWithSpecifiedExtension($dir, $extension) { $directory = new RecursiveDirectoryIterator($dir); $iterator = new RecursiveIteratorIterator($directory); $filter = new RegexIterator($iterator, "/\.$extension$/i", RecursiveRegexIterator::GET_MATCH); return array_keys(iterator_to_array($filter)); } /** * Copies content from $inputFile to $outputFile. * * @param string $inputFile * @param string $outputFile * @param int $bufferSize */ public static function copyContent($inputFile, $outputFile, $bufferSize = 1024) { $input = fopen($inputFile, 'r'); $output = fopen($outputFile, "w"); while ($data = fread($input, $bufferSize)) { fwrite($output, $data); } fclose($input); fclose($output); } /** * Returns mime type for the given file path. * * @param string $path * @return string */ public static function mimeType($path) { $fileInfo = new finfo(FILEINFO_MIME_TYPE); return $fileInfo->file($path); } /** * Returns class name if file contains class or false if not, throws FileNotFoundException when file does not exist * * @param string $filePath - path to file * @return bool|string - false if file doesn't contain class or class name if file contains class * @throws FileNotFoundException - error thrown when file does not exist */ public static function checkWhetherFileContainsClass($filePath) { if (!self::exists($filePath)) { throw new FileNotFoundException('Cannot find source file: ' . $filePath); } $fp = fopen($filePath, 'r'); $class = false; $buffer = ''; $i = 0; while (!$class && !feof($fp)) { $buffer .= fread($fp, 512); $tokens = token_get_all($buffer); if (strpos($buffer, '{') === false) continue; for (; $i < count($tokens); $i++) { if ($tokens[$i][0] === T_CLASS) { for ($j = $i + 1; $j < count($tokens); $j++) { if ($tokens[$j] === '{') { $class = $tokens[$i + 2][1]; } } } } } return $class; } }