| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 4 | * |
|---|
| 5 | * http://www.lockon.co.jp/ |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * ファイル管理 のヘルパークラス. |
|---|
| 10 | * |
|---|
| 11 | * @package Page |
|---|
| 12 | * @author LOCKON CO.,LTD. |
|---|
| 13 | * @version $Id$ |
|---|
| 14 | */ |
|---|
| 15 | class SC_Helper_FileManager { |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * 指定パス配下のディレクトリ取得する. |
|---|
| 19 | * |
|---|
| 20 | * @param string $dir 取得するディレクトリパス |
|---|
| 21 | * @return void |
|---|
| 22 | */ |
|---|
| 23 | function sfGetFileList($dir) { |
|---|
| 24 | $arrFileList = array(); |
|---|
| 25 | $arrDirList = array(); |
|---|
| 26 | |
|---|
| 27 | if (is_dir($dir)) { |
|---|
| 28 | if ($dh = opendir($dir)) { |
|---|
| 29 | $cnt = 0; |
|---|
| 30 | // 行末の/を取り除く |
|---|
| 31 | while (($file = readdir($dh)) !== false) $arrDir[] = $file; |
|---|
| 32 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 33 | // アルファベットと数字でソート |
|---|
| 34 | natcasesort($arrDir); |
|---|
| 35 | foreach($arrDir as $file) { |
|---|
| 36 | // ./ と ../を除くファイルのみを取得 |
|---|
| 37 | if($file != "." && $file != "..") { |
|---|
| 38 | |
|---|
| 39 | $path = $dir."/".$file; |
|---|
| 40 | // SELECT内の見た目を整えるため指定文字数で切る |
|---|
| 41 | $file_name = SC_Utils_Ex::sfCutString($file, FILE_NAME_LEN); |
|---|
| 42 | $file_size = SC_Utils_Ex::sfCutString($this->sfGetDirSize($path), FILE_NAME_LEN); |
|---|
| 43 | $file_time = date("Y/m/d", filemtime($path)); |
|---|
| 44 | |
|---|
| 45 | // ディレクトリとファイルで格納配列を変える |
|---|
| 46 | if(is_dir($path)) { |
|---|
| 47 | $arrDirList[$cnt]['file_name'] = $file; |
|---|
| 48 | $arrDirList[$cnt]['file_path'] = $path; |
|---|
| 49 | $arrDirList[$cnt]['file_size'] = $file_size; |
|---|
| 50 | $arrDirList[$cnt]['file_time'] = $file_time; |
|---|
| 51 | $arrDirList[$cnt]['is_dir'] = true; |
|---|
| 52 | } else { |
|---|
| 53 | $arrFileList[$cnt]['file_name'] = $file; |
|---|
| 54 | $arrFileList[$cnt]['file_path'] = $path; |
|---|
| 55 | $arrFileList[$cnt]['file_size'] = $file_size; |
|---|
| 56 | $arrFileList[$cnt]['file_time'] = $file_time; |
|---|
| 57 | $arrFileList[$cnt]['is_dir'] = false; |
|---|
| 58 | } |
|---|
| 59 | $cnt++; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | closedir($dh); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | // フォルダを先頭にしてマージ |
|---|
| 67 | return array_merge($arrDirList, $arrFileList); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * 指定したディレクトリのバイト数を取得する. |
|---|
| 72 | * |
|---|
| 73 | * @param string $dir ディレクトリ |
|---|
| 74 | * @return void |
|---|
| 75 | */ |
|---|
| 76 | function sfGetDirSize($dir) { |
|---|
| 77 | $bytes = 0; |
|---|
| 78 | if(file_exists($dir)) { |
|---|
| 79 | // ディレクトリの場合下層ファイルの総量を取得 |
|---|
| 80 | if (is_dir($dir)) { |
|---|
| 81 | $handle = opendir($dir); |
|---|
| 82 | while ($file = readdir($handle)) { |
|---|
| 83 | // 行末の/を取り除く |
|---|
| 84 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 85 | $path = $dir."/".$file; |
|---|
| 86 | if ($file != '..' && $file != '.' && !is_dir($path)) { |
|---|
| 87 | $bytes += filesize($path); |
|---|
| 88 | } else if (is_dir($path) && $file != '..' && $file != '.') { |
|---|
| 89 | // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 |
|---|
| 90 | $bytes += $this->sfGetDirSize($path); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } else { |
|---|
| 94 | // ファイルの場合 |
|---|
| 95 | $bytes = filesize($dir); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | // ディレクトリ(ファイル)が存在しない場合は0byteを返す |
|---|
| 99 | if($bytes == "") $bytes = 0; |
|---|
| 100 | |
|---|
| 101 | return $bytes; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * 指定したディレクトリ又はファイルを削除する. |
|---|
| 106 | * |
|---|
| 107 | * @param string $dir 削除するディレクトリ又はファイル |
|---|
| 108 | * @return void |
|---|
| 109 | */ |
|---|
| 110 | function sfDeleteDir($dir) { |
|---|
| 111 | $arrResult = array(); |
|---|
| 112 | if(file_exists($dir)) { |
|---|
| 113 | // ディレクトリかチェック |
|---|
| 114 | if (is_dir($dir)) { |
|---|
| 115 | if ($handle = opendir("$dir")) { |
|---|
| 116 | $cnt = 0; |
|---|
| 117 | while (false !== ($item = readdir($handle))) { |
|---|
| 118 | if ($item != "." && $item != "..") { |
|---|
| 119 | if (is_dir("$dir/$item")) { |
|---|
| 120 | $this->sfDeleteDir("$dir/$item"); |
|---|
| 121 | } else { |
|---|
| 122 | $arrResult[$cnt]['result'] = @unlink("$dir/$item"); |
|---|
| 123 | $arrResult[$cnt]['file_name'] = "$dir/$item"; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | $cnt++; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | closedir($handle); |
|---|
| 130 | $arrResult[$cnt]['result'] = @rmdir($dir); |
|---|
| 131 | $arrResult[$cnt]['file_name'] = "$dir/$item"; |
|---|
| 132 | } else { |
|---|
| 133 | // ファイル削除 |
|---|
| 134 | $arrResult[0]['result'] = @unlink("$dir"); |
|---|
| 135 | $arrResult[0]['file_name'] = "$dir"; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | return $arrResult; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 144 | * |
|---|
| 145 | * @param string $dir ディレクトリ |
|---|
| 146 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスを |
|---|
| 147 | * | 区切りで格納 |
|---|
| 148 | * @return array ツリー生成用の配列 |
|---|
| 149 | */ |
|---|
| 150 | function sfGetFileTree($dir, $tree_status) { |
|---|
| 151 | |
|---|
| 152 | $cnt = 0; |
|---|
| 153 | $arrTree = array(); |
|---|
| 154 | $default_rank = count(split('/', $dir)); |
|---|
| 155 | |
|---|
| 156 | // 文末の/を取り除く |
|---|
| 157 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 158 | // 最上位層を格納(user_data/) |
|---|
| 159 | if($this->sfDirChildExists($dir)) { |
|---|
| 160 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 161 | } else { |
|---|
| 162 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 163 | } |
|---|
| 164 | $arrTree[$cnt]['path'] = $dir; |
|---|
| 165 | $arrTree[$cnt]['rank'] = 0; |
|---|
| 166 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 167 | // 初期表示はオープン |
|---|
| 168 | if($_POST['mode'] != '') { |
|---|
| 169 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($dir, $tree_status); |
|---|
| 170 | } else { |
|---|
| 171 | $arrTree[$cnt]['open'] = true; |
|---|
| 172 | } |
|---|
| 173 | $cnt++; |
|---|
| 174 | |
|---|
| 175 | $this->sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 176 | |
|---|
| 177 | return $arrTree; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * ツリー生成用配列取得(javascriptに渡す用). |
|---|
| 182 | * |
|---|
| 183 | * @param string $dir ディレクトリ |
|---|
| 184 | * @param string $default_rank デフォルトの階層 |
|---|
| 185 | * (/区切りで 0,1,2・・・とカウント) |
|---|
| 186 | * @param integer $cnt 連番 |
|---|
| 187 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 188 | * | 区切りで格納 |
|---|
| 189 | * @return array ツリー生成用の配列 |
|---|
| 190 | */ |
|---|
| 191 | function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { |
|---|
| 192 | |
|---|
| 193 | if(file_exists($dir)) { |
|---|
| 194 | if ($handle = opendir("$dir")) { |
|---|
| 195 | while (false !== ($item = readdir($handle))) $arrDir[] = $item; |
|---|
| 196 | // アルファベットと数字でソート |
|---|
| 197 | natcasesort($arrDir); |
|---|
| 198 | foreach($arrDir as $item) { |
|---|
| 199 | if ($item != "." && $item != "..") { |
|---|
| 200 | // 文末の/を取り除く |
|---|
| 201 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 202 | $path = $dir."/".$item; |
|---|
| 203 | // ディレクトリのみ取得 |
|---|
| 204 | if (is_dir($path)) { |
|---|
| 205 | $arrTree[$cnt]['path'] = $path; |
|---|
| 206 | if($this->sfDirChildExists($path)) { |
|---|
| 207 | $arrTree[$cnt]['type'] = "_parent"; |
|---|
| 208 | } else { |
|---|
| 209 | $arrTree[$cnt]['type'] = "_child"; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | // 階層を割り出す |
|---|
| 213 | $arrCnt = split('/', $path); |
|---|
| 214 | $rank = count($arrCnt); |
|---|
| 215 | $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; |
|---|
| 216 | $arrTree[$cnt]['count'] = $cnt; |
|---|
| 217 | // フォルダが開いているか |
|---|
| 218 | $arrTree[$cnt]['open'] = $this->lfIsFileOpen($path, $tree_status); |
|---|
| 219 | $cnt++; |
|---|
| 220 | // 下層ディレクトリ取得の為、再帰的に呼び出す |
|---|
| 221 | $this->sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | closedir($handle); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /** |
|---|
| 231 | * 指定したディレクトリ配下にファイルがあるかチェックする. |
|---|
| 232 | * |
|---|
| 233 | * @param string ディレクトリ |
|---|
| 234 | * @return bool ファイルが存在する場合 true |
|---|
| 235 | */ |
|---|
| 236 | function sfDirChildExists($dir) { |
|---|
| 237 | if(file_exists($dir)) { |
|---|
| 238 | if (is_dir($dir)) { |
|---|
| 239 | $handle = opendir($dir); |
|---|
| 240 | while ($file = readdir($handle)) { |
|---|
| 241 | // 行末の/を取り除く |
|---|
| 242 | $dir = ereg_replace("/$", "", $dir); |
|---|
| 243 | $path = $dir."/".$file; |
|---|
| 244 | if ($file != '..' && $file != '.' && is_dir($path)) { |
|---|
| 245 | return true; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | return false; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * 指定したファイルが前回開かれた状態にあったかチェックする. |
|---|
| 256 | * |
|---|
| 257 | * @param string $dir ディレクトリ |
|---|
| 258 | * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが |
|---|
| 259 | * | 区切りで格納 |
|---|
| 260 | * @return bool 前回開かれた状態の場合 true |
|---|
| 261 | */ |
|---|
| 262 | function lfIsFileOpen($dir, $tree_status) { |
|---|
| 263 | $arrTreeStatus = split('\|', $tree_status); |
|---|
| 264 | if(in_array($dir, $arrTreeStatus)) { |
|---|
| 265 | return true; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | return false; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /** |
|---|
| 272 | * ファイルのダウンロードを行う. |
|---|
| 273 | * |
|---|
| 274 | * @param string $file ファイルパス |
|---|
| 275 | * @return void |
|---|
| 276 | */ |
|---|
| 277 | function sfDownloadFile($file) { |
|---|
| 278 | // ファイルの場合はダウンロードさせる |
|---|
| 279 | Header("Content-disposition: attachment; filename=".basename($file)); |
|---|
| 280 | Header("Content-type: application/octet-stream; name=".basename($file)); |
|---|
| 281 | Header("Cache-Control: "); |
|---|
| 282 | Header("Pragma: "); |
|---|
| 283 | echo ($this->sfReadFile($file)); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | /** |
|---|
| 287 | * ファイル作成を行う. |
|---|
| 288 | * |
|---|
| 289 | * @param string $file ファイルパス |
|---|
| 290 | * @param integer $mode パーミッション |
|---|
| 291 | * @return bool ファイル作成に成功した場合 true |
|---|
| 292 | */ |
|---|
| 293 | function sfCreateFile($file, $mode = "") { |
|---|
| 294 | // 行末の/を取り除く |
|---|
| 295 | if($mode != "") { |
|---|
| 296 | $ret = @mkdir($file, $mode); |
|---|
| 297 | } else { |
|---|
| 298 | $ret = @mkdir($file); |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | return $ret; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | /** |
|---|
| 305 | * ファイル読込を行う. |
|---|
| 306 | * |
|---|
| 307 | * @param string ファイルパス |
|---|
| 308 | * @return string ファイルの内容 |
|---|
| 309 | */ |
|---|
| 310 | function sfReadFile($filename) { |
|---|
| 311 | $str = ""; |
|---|
| 312 | // バイナリモードでオープン |
|---|
| 313 | $fp = @fopen($filename, "rb" ); |
|---|
| 314 | //ファイル内容を全て変数に読み込む |
|---|
| 315 | if($fp) { |
|---|
| 316 | $str = @fread($fp, filesize($filename)+1); |
|---|
| 317 | } |
|---|
| 318 | @fclose($fp); |
|---|
| 319 | |
|---|
| 320 | return $str; |
|---|
| 321 | } |
|---|
| 322 | } |
|---|
| 323 | ?> |
|---|