source: branches/feature-module-update/data/include/file_manager.inc @ 15078

Revision 15078, 8.5 KB checked in by nanasess, 17 years ago (diff)

r15064 から svn cp
とりあえず暫定コミット.

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