Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00017 final class FileUtils extends StaticFactory
00018 {
00023 public static function convertLineEndings(
00024 $dir, $ignore, $from = "\r\n", $to = "\n"
00025 )
00026 {
00027 $converted = 0;
00028
00029 if (!is_dir($dir) || !is_readable($dir)) {
00030 throw new WrongArgumentException();
00031 }
00032
00033 $files = scandir($dir);
00034
00035 foreach ($files as $file) {
00036 if (
00037 '.' != $file
00038 && '..' != $file
00039 &&
00040 !in_array(
00041 substr($file, strrpos($file, '.')), $ignore, true
00042 )
00043 ) {
00044 if (is_dir($path = $dir . DIRECTORY_SEPARATOR . $file)) {
00045 $converted += self::convertLineEndings(
00046 $path, $ignore, $from, $to
00047 );
00048 } else {
00049 file_put_contents(
00050 $path,
00051 preg_replace(
00052 "/$from/",
00053 $to,
00054 file_get_contents($path)
00055 )
00056 );
00057
00058 ++$converted;
00059 }
00060 }
00061 }
00062
00063 return $converted;
00064 }
00065
00066 public static function makeTempFile(
00067 $where = 'file-utils/', $prefix = '', $mkdirMode = 0700
00068 )
00069 {
00070 $directory = ONPHP_TEMP_PATH.$where;
00071
00072 if (!is_writable($directory))
00073 if (!mkdir($directory, $mkdirMode, true))
00074 throw new WrongArgumentException(
00075 "can not write to '{$directory}'"
00076 );
00077
00078 $result = tempnam($directory, $prefix);
00079
00080 if ($result === false)
00081 throw new WrongArgumentException(
00082 'failed to create temp file in '.$directory
00083 );
00084
00085 return $result;
00086 }
00087
00088 public static function makeTempDirectory(
00089 $where = 'file-utils/', $prefix = '', $mode = 0700
00090 )
00091 {
00092 $directory = ONPHP_TEMP_PATH.$where;
00093
00094 if (substr($directory, -1) != DIRECTORY_SEPARATOR)
00095 $directory .= DIRECTORY_SEPARATOR;
00096
00097 $attempts = 42;
00098
00099 do {
00100 --$attempts;
00101 $path = $directory.$prefix.mt_rand();
00102 } while (
00103 !mkdir($path, $mode, true)
00104 && $attempts > 0
00105
00106 && !usleep(100)
00107 );
00108
00109 if ($attempts == 0)
00110 throw new WrongArgumentException(
00111 'failed to create subdirectory in '.$directory
00112 );
00113
00114 return $path;
00115 }
00116
00117 public static function makeUniqueName($fileName)
00118 {
00119 $extensionPosition = strrpos($fileName, '.');
00120
00121 return
00122 substr($fileName, 0, $extensionPosition)
00123 .'_'.uniqid()
00124 .substr($fileName, $extensionPosition);
00125 }
00126
00127 public static function removeDirectory($directory, $recursive = false)
00128 {
00129 if (!$recursive) {
00130 try {
00131 rmdir($directory);
00132 } catch (BaseException $e) {
00133 throw new WrongArgumentException($e->getMessage());
00134 }
00135 } else {
00136 $directoryIterator = new DirectoryIterator($directory);
00137
00138 foreach ($directoryIterator as $file) {
00139 if ($file->isDot())
00140 continue;
00141
00142 if ($file->isDir())
00143 self::removeDirectory($file->getPathname(), $recursive);
00144 elseif (!unlink($file->getPathname()))
00145 throw new WrongStateException(
00146 "cannot unlink {$file->getPathname()}"
00147 );
00148 }
00149
00150 try {
00151 rmdir($directory);
00152 } catch (BaseException $e) {
00153 throw new WrongStateException(
00154 "cannot unlink {$directory}, though it should be empty now"
00155 );
00156 }
00157 }
00158 }
00159
00160 public static function upload($source, $target)
00161 {
00162 if (
00163 is_readable($source)
00164 && is_writable(pathinfo($target, PATHINFO_DIRNAME))
00165 )
00166 return move_uploaded_file($source, $target);
00167
00168 throw new WrongArgumentException(
00169 "can not move {$source} to {$target}"
00170 );
00171 }
00172
00173 public static function move($source, $target)
00174 {
00175 if (
00176 is_readable($source)
00177 && is_writable(pathinfo($target, PATHINFO_DIRNAME))
00178 )
00179 return rename($source, $target);
00180
00181 throw new WrongArgumentException(
00182 "can not move {$source} to {$target}"
00183 );
00184 }
00185
00186 public static function unlink($filePath)
00187 {
00188 if (
00189 file_exists($filePath)
00190 && is_writable(pathinfo($filePath, PATHINFO_DIRNAME))
00191 )
00192 return unlink($filePath);
00193
00194 throw new WrongArgumentException(
00195 "can not unlink {$filePath}"
00196 );
00197 }
00198 }
00199 ?>