source: branches/dev/data/class/SC_UploadFile.php @ 8

Revision 8, 8.4 KB checked in by root, 17 years ago (diff)

new import

Line 
1<?php
2/*
3 * Copyright(c) 2000-2006 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 . "/../lib/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                            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($this->temp_file[$cnt] != "") {
143                                                   
144                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
145                // ¤¹¤Ç¤ËÊݸ¥Õ¥¡¥¤¥ë¤¬¤¢¤Ã¤¿¾ì¹ç¤Ïºï½ü¤¹¤ë¡£
146                if($this->save_file[$cnt] != "" && !ereg("^sub/", $this->save_file[$cnt])) {
147                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
148                }
149            }
150            $cnt++;
151        }
152    }
153   
154    // HIDDENÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
155    function getHiddenFileList() {
156        $cnt = 0;
157        foreach($this->keyname as $val) {
158            if($this->temp_file[$cnt] != "") {
159                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
160            }
161            if($this->save_file[$cnt] != "") {
162                $arrRet["save_" . $val] = $this->save_file[$cnt];
163            }
164            $cnt++;
165        }
166        return $arrRet;
167    }
168   
169    // HIDDEN¤ÇÁ÷¤é¤ì¤Æ¤­¤¿¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ¤¹¤ë
170    function setHiddenFileList($arrPOST) {
171        $cnt = 0;
172        foreach($this->keyname as $val) {
173            $key = "temp_" . $val;
174            if($arrPOST[$key] != "") {
175                $this->temp_file[$cnt] = $arrPOST[$key];
176            }
177            $key = "save_" . $val;
178            if($arrPOST[$key] != "") {
179                $this->save_file[$cnt] = $arrPOST[$key];
180            }
181            $cnt++;
182        }
183    }
184   
185    // ¥Õ¥©¡¼¥à¤ËÅϤ¹ÍѤΥե¡¥¤¥ë¾ðÊóÇÛÎó¤òÊÖ¤¹
186    function getFormFileList($temp_url, $save_url, $real_size = false) {
187
188        $cnt = 0;
189        foreach($this->keyname as $val) {
190            if($this->temp_file[$cnt] != "") {
191                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
192                if(ereg("/$", $temp_url)) {
193                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
194                } else {
195                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
196                }
197                $arrRet[$val]['filepath_dir'] = $this->temp_dir . $this->temp_file[$cnt];
198            } elseif ($this->save_file[$cnt] != "") {
199                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
200                if(ereg("/$", $save_url)) {
201                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
202                } else {
203                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
204                }
205                $arrRet[$val]['filepath_dir'] = $this->save_dir . $this->save_file[$cnt];
206            }
207            if($arrRet[$val]['filepath'] != "") {
208               
209                if($real_size){
210                    list($width, $height) = getimagesize($arrRet[$val]['filepath_dir']);
211                    // ¥Õ¥¡¥¤¥ë²£Éý
212                    $arrRet[$val]['width'] = $width;
213                    // ¥Õ¥¡¥¤¥ë½ÄÉý
214                    $arrRet[$val]['height'] = $height;
215                }else{
216                    // ¥Õ¥¡¥¤¥ë²£Éý
217                    $arrRet[$val]['width'] = $this->width[$cnt];
218                    // ¥Õ¥¡¥¤¥ë½ÄÉý
219                    $arrRet[$val]['height'] = $this->height[$cnt];
220                }
221                // ɽ¼¨Ì¾
222                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
223            }
224            $cnt++;
225        }
226        return $arrRet;
227    }
228   
229    // DBÊݸÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
230    function getDBFileList() {
231        $cnt = 0;
232        foreach($this->keyname as $val) {
233            if($this->temp_file[$cnt] != "") {
234                $arrRet[$val] = $this->temp_file[$cnt];
235            } else  {
236                $arrRet[$val] = $this->save_file[$cnt];
237            }
238            $cnt++;
239        }
240        return $arrRet;
241    }
242   
243    // DB¤ÇÊݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾ÇÛÎó¤ò¥»¥Ã¥È¤¹¤ë
244    function setDBFileList($arrVal) {
245        $cnt = 0;
246        foreach($this->keyname as $val) {
247            if($arrVal[$val] != "") {
248                $this->save_file[$cnt] = $arrVal[$val];
249            }
250            $cnt++;
251        }
252    }
253   
254    // ²èÁü¤ò¥»¥Ã¥È¤¹¤ë
255    function setDBImageList($arrVal) {
256        $cnt = 0;
257        foreach($this->keyname as $val) {
258            if($arrVal[$val] != "" && $val == 'tv_products_image') {
259                $this->save_file[$cnt] = $arrVal[$val];
260            }
261            $cnt++;
262        }
263    }
264   
265    // DB¾å¤Î¥Õ¥¡¥¤¥ë¤ÎÆâºï½üÍ׵᤬¤¢¤Ã¤¿¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¡£
266    function deleteDBFile($arrVal) {
267        $objImage = new SC_Image($this->temp_dir);
268        $cnt = 0;
269        foreach($this->keyname as $val) {
270            if($arrVal[$val] != "") {
271                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
272                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
273                }
274            }
275            $cnt++;
276        }
277    }
278   
279    // ɬ¿ÜȽÄê
280    function checkEXISTS($keyname = "") {
281        $cnt = 0;
282        $arrRet = array();
283        foreach($this->keyname as $val) {
284            if($val == $keyname || $keyname == "") {
285                // ɬ¿Ü¤Ç¤¢¤ì¤Ð¥¨¥é¡¼¥Á¥§¥Ã¥¯
286                if ($this->necessary[$cnt] == true) {
287                    if($this->save_file[$cnt] == "" && $this->temp_file[$cnt] == "") {
288                        $arrRet[$val] = "¢¨ " . $this->disp_name[$cnt] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br>";
289                    }
290                }
291            }
292            $cnt++;
293        }
294        return $arrRet;
295    }
296       
297    // ³ÈÂçΨ¤ò»ØÄꤷ¤Æ²èÁüÊݸ
298    function saveResizeImage($keyname, $to_w, $to_h) {
299        $path = "";
300       
301        // keyname¤ÎźÉÕ¥Õ¥¡¥¤¥ë¤ò¼èÆÀ
302        $arrImageKey = array_flip($this->keyname);
303        $file = $this->temp_file[$arrImageKey[$keyname]];
304        $filepath = $this->temp_dir . $file;
305       
306        $path = $this->makeThumb($filepath, $to_w, $to_h);
307       
308        // ¥Õ¥¡¥¤¥ë̾¤À¤±ÊÖ¤¹
309        return basename($path);
310    }
311}
312?>
Note: See TracBrowser for help on using the repository browser.