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

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

リファクタリングに伴う修正

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