source: branches/version-2/data/class/SC_UploadFile.php @ 17069

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