source: branches/feature-module-update/data/class/SC_UploadFile.php @ 15681

Revision 15681, 11.9 KB checked in by nanasess, 17 years ago (diff)

FIXME コメント追加

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
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$SC_UPLOADFILE_DIR = realpath(dirname( __FILE__));
9require_once($SC_UPLOADFILE_DIR . "/../module/gdthumb.php");
10
11/* アップロードファイル管理クラス */
12class SC_UploadFile {
13    var $temp_dir;
14    var $save_dir;
15    var $keyname;   // ファイルinputタグのname
16    var $width;     // 横サイズ
17    var $height;    // 縦サイズ
18    var $arrExt;    // 指定する拡張子
19    var $temp_file; // 保存されたファイル名
20    var $save_file; // DBから読み出したファイル名
21    var $disp_name; // 項目名
22    var $size;      // 制限サイズ
23    var $necessary; // 必須の場合:true
24    var $image;     // 画像の場合:true
25
26    // ファイル管理クラス
27    function SC_UploadFile($temp_dir, $save_dir) {
28        $this->temp_dir = $temp_dir;
29        $this->save_dir = $save_dir;
30        $this->file_max = 0;
31    }
32
33    // ファイル情報追加
34    function addFile($disp_name, $keyname, $arrExt, $size, $necessary=false, $width=0, $height=0, $image=true) {
35        $this->disp_name[] = $disp_name;
36        $this->keyname[] = $keyname;
37        $this->width[] = $width;
38        $this->height[] = $height;
39        $this->arrExt[] = $arrExt;
40        $this->size[] = $size;
41        $this->necessary[] = $necessary;
42        $this->image[] = $image;
43    }
44    // サムネイル画像の作成
45    function makeThumb($src_file, $width, $height) {
46        // 一意なIDを取得する。
47        $uniqname = date("mdHi") . "_" . uniqid("");
48
49        $dst_file = $this->temp_dir . $uniqname;
50
51        $objThumb = new gdthumb();
52        $ret = $objThumb->Main($src_file, $width, $height, $dst_file);
53
54        if($ret[0] != 1) {
55            // エラーメッセージの表示
56            print($ret[1]);
57            exit;
58        }
59
60        return basename($ret[1]);
61    }
62
63    // アップロードされたファイルを保存する。
64    // FIXME see. http://www.php.net/manual/en/features.file-upload.php
65    function makeTempFile($keyname, $rename = true) {
66        $objErr = new SC_CheckError();
67        $cnt = 0;
68        $arrKeyname = array_flip($this->keyname);
69
70        if(!($_FILES[$keyname]['size'] > 0)) {
71            $objErr->arrErr[$keyname] = "※ " . $this->disp_name[$arrKeyname[$keyname]] . "がアップロードされていません。<br />";
72        } else {
73            foreach($this->keyname as $val) {
74                // 一致したキーのファイルに情報を保存する。
75                if ($val == $keyname) {
76                    // 拡張子チェック
77                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array("FILE_EXT_CHECK"));
78                    // ファイルサイズチェック
79                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array("FILE_SIZE_CHECK"));
80                    // エラーがない場合
81                    if(!isset($objErr->arrErr[$keyname])) {
82                        // 画像ファイルの場合
83                        if($this->image[$cnt]) {
84                            $this->temp_file[$cnt] = $this->makeThumb($_FILES[$keyname]['tmp_name'], $this->width[$cnt], $this->height[$cnt]);
85                        // 画像ファイル以外の場合
86                        } else {
87                            // 一意なファイル名を作成する。
88                            if($rename) {
89                                $uniqname = date("mdHi") . "_" . uniqid("").".";
90                                $this->temp_file[$cnt] = ereg_replace("^.*\.",$uniqname, $_FILES[$keyname]['name']);
91                            } else {
92                                $this->temp_file[$cnt] = $_FILES[$keyname]['name'];
93                            }
94                            $result  = copy($_FILES[$keyname]['tmp_name'], $this->temp_dir. "/". $this->temp_file[$cnt]);
95                            GC_Utils_Ex::gfPrintLog($_FILES[$keyname]['name']." -> ".$this->temp_dir. "/". $this->temp_file[$cnt]);
96                        }
97                    }
98                }
99                $cnt++;
100            }
101        }
102        return $objErr->arrErr[$keyname];
103    }
104
105    // 画像を削除する。
106    function deleteFile($keyname) {
107        $objImage = new SC_Image($this->temp_dir);
108        $cnt = 0;
109        foreach($this->keyname as $val) {
110            if ($val == $keyname) {
111                // 一時ファイルの場合削除する。
112                if($this->temp_file[$cnt] != "") {
113                    $objImage->deleteImage($this->temp_file[$cnt], $this->save_dir);
114                }
115                $this->temp_file[$cnt] = "";
116                $this->save_file[$cnt] = "";
117            }
118            $cnt++;
119        }
120    }
121
122    // 一時ファイルパスを取得する。
123    function getTempFilePath($keyname) {
124        $cnt = 0;
125        $filepath = "";
126        foreach($this->keyname as $val) {
127            if ($val == $keyname) {
128                if($this->temp_file[$cnt] != "") {
129                    $filepath = $this->temp_dir . "/" . $this->temp_file[$cnt];
130                }
131            }
132            $cnt++;
133        }
134        return $filepath;
135    }
136
137    // 一時ファイルを保存ディレクトリに移す
138    function moveTempFile() {
139        $cnt = 0;
140        $objImage = new SC_Image($this->temp_dir);
141
142        foreach($this->keyname as $val) {
143            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
144
145                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
146
147                // すでに保存ファイルがあった場合は削除する。
148                if(isset($this->save_file[$cnt])
149                   && $this->save_file[$cnt] != ""
150                   && !ereg("^sub/", $this->save_file[$cnt])) {
151
152                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
153                }
154            }
155            $cnt++;
156        }
157    }
158
159    // HIDDEN用のファイル名配列を返す
160    function getHiddenFileList() {
161        $cnt = 0;
162        $arrRet = array();
163        foreach($this->keyname as $val) {
164            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
165                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
166            }
167            if(isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
168                $arrRet["save_" . $val] = $this->save_file[$cnt];
169            }
170            $cnt++;
171        }
172        return $arrRet;
173    }
174
175    // HIDDENで送られてきたファイル名を取得する
176    function setHiddenFileList($arrPOST) {
177        $cnt = 0;
178        foreach($this->keyname as $val) {
179            $key = "temp_" . $val;
180            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
181                $this->temp_file[$cnt] = $arrPOST[$key];
182            }
183            $key = "save_" . $val;
184            if(isset($arrPOST[$key]) && !empty($arrPOST[$key])) {
185                $this->save_file[$cnt] = $arrPOST[$key];
186            }
187            $cnt++;
188        }
189    }
190
191    // フォームに渡す用のファイル情報配列を返す
192    function getFormFileList($temp_url, $save_url, $real_size = false) {
193        $arrRet = array();
194        $cnt = 0;
195        foreach($this->keyname as $val) {
196            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
197                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
198                if(ereg("/$", $temp_url)) {
199                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
200                } else {
201                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
202                }
203                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
204            } elseif (isset($this->save_file[$cnt]) && $this->save_file[$cnt] != "") {
205                // ファイルパスチェック(パスのスラッシュ/が連続しないようにする。)
206                if(ereg("/$", $save_url)) {
207                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
208                } else {
209                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
210                }
211                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
212            }
213            if(isset($arrRet[$val]['filepath']) && !empty($arrRet[$val]['filepath'])) {
214                if($real_size){
215                    if(is_file($arrRet[$val]['real_filepath'])) {
216                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
217                    }
218                    // ファイル横幅
219                    $arrRet[$val]['width'] = $width;
220                    // ファイル縦幅
221                    $arrRet[$val]['height'] = $height;
222                }else{
223                    // ファイル横幅
224                    $arrRet[$val]['width'] = $this->width[$cnt];
225                    // ファイル縦幅
226                    $arrRet[$val]['height'] = $this->height[$cnt];
227                }
228                // 表示名
229                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
230            }
231            $cnt++;
232        }
233        return $arrRet;
234    }
235
236    // DB保存用のファイル名配列を返す
237    function getDBFileList() {
238        $cnt = 0;
239        foreach($this->keyname as $val) {
240            if(isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] != "") {
241                $arrRet[$val] = $this->temp_file[$cnt];
242            } else  {
243                $arrRet[$val] = isset($this->save_file[$cnt]) ? $this->save_file[$cnt] : "";
244            }
245            $cnt++;
246        }
247        return $arrRet;
248    }
249
250    // DBで保存されたファイル名配列をセットする
251    function setDBFileList($arrVal) {
252        $cnt = 0;
253        foreach($this->keyname as $val) {
254            if(isset($arrVal[$val]) && $arrVal[$val] != "") {
255                $this->save_file[$cnt] = $arrVal[$val];
256            }
257            $cnt++;
258        }
259    }
260
261    // 画像をセットする
262    function setDBImageList($arrVal) {
263        $cnt = 0;
264        foreach($this->keyname as $val) {
265            if($arrVal[$val] != "" && $val == 'tv_products_image') {
266                $this->save_file[$cnt] = $arrVal[$val];
267            }
268            $cnt++;
269        }
270    }
271
272    // DB上のファイルの内削除要求があったファイルを削除する。
273    function deleteDBFile($arrVal) {
274        $objImage = new SC_Image($this->temp_dir);
275        $cnt = 0;
276        foreach($this->keyname as $val) {
277            if($arrVal[$val] != "") {
278                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
279                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
280                }
281            }
282            $cnt++;
283        }
284    }
285
286    // 必須判定
287    function checkEXISTS($keyname = "") {
288        $cnt = 0;
289        $arrRet = array();
290        foreach($this->keyname as $val) {
291            if($val == $keyname || $keyname == "") {
292                // 必須であればエラーチェック
293                if ($this->necessary[$cnt] == true) {
294                    if(isset($this->save_file[$cnt]) && $this->save_file[$cnt] == ""
295                            && isset($this->temp_file[$cnt]) && $this->temp_file[$cnt] == "") {
296                        $arrRet[$val] = "※ " . $this->disp_name[$cnt] . "がアップロードされていません。<br>";
297                    }
298                }
299            }
300            $cnt++;
301        }
302        return $arrRet;
303    }
304
305    // 拡大率を指定して画像保存
306    function saveResizeImage($keyname, $to_w, $to_h) {
307        $path = "";
308
309        // keynameの添付ファイルを取得
310        $arrImageKey = array_flip($this->keyname);
311        $file = $this->temp_file[$arrImageKey[$keyname]];
312        $filepath = $this->temp_dir . $file;
313
314        $path = $this->makeThumb($filepath, $to_w, $to_h);
315
316        // ファイル名だけ返す
317        return basename($path);
318    }
319}
320?>
Note: See TracBrowser for help on using the repository browser.