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

Revision 14410, 12.7 KB checked in by uehara, 19 years ago (diff)
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 . "/../lib/gdthumb.php");
10require_once($SC_UPLOADFILE_DIR . "/../include/ftp.php");
11
12/* ¥¢¥Ã¥×¥í¡¼¥É¥Õ¥¡¥¤¥ë´ÉÍý¥¯¥é¥¹ */
13class SC_UploadFile {
14    var $temp_dir;                  // °ì»þ¥Õ¥¡¥¤¥ëÊݸ¥Ç¥£¥ì¥¯¥È¥ê
15    var $save_dir;                  // ¥Õ¥¡¥¤¥ëÊݸ¥Ç¥£¥ì¥¯¥È
16    var $ftp_dir;                   // FTPÀè¥Ç¥£¥ì¥¯¥È¥ê(Éé²Ùʬ»¶»þ»ÈÍÑ)
17    var $multi_web_server_mode;     // Éé²Ùʬ»¶¥Õ¥é¥°(Éé²Ùʬ»¶»þ»ÈÍÑ)
18    var $keyname;                   // ¥Õ¥¡¥¤¥ëinput¥¿¥°¤Îname
19    var $width;                     // ²£¥µ¥¤¥º
20    var $height;                    // ½Ä¥µ¥¤¥º
21    var $arrExt;                    // »ØÄꤹ¤ë³ÈÄ¥»Ò
22    var $temp_file;                 // Êݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾
23    var $save_file;                 // DB¤«¤éÆÉ¤ß½Ð¤·¤¿¥Õ¥¡¥¤¥ë̾
24    var $disp_name;                 // ¹àÌÜ̾
25    var $size;                      // À©¸Â¥µ¥¤¥º
26    var $necessary;                 // ɬ¿Ü¤Î¾ì¹ç:true
27    var $image;                     // ²èÁü¤Î¾ì¹ç:true
28   
29    // ¥Õ¥¡¥¤¥ë´ÉÍý¥¯¥é¥¹
30    function SC_UploadFile($temp_dir, $save_dir, $ftp_dir = "", $multi_web_server_mode = false) {
31        $this->temp_dir = $temp_dir;
32        $this->save_dir = $save_dir;
33        $this->ftp_dir = $ftp_dir;
34        $this->multi_web_server_mode = $multi_web_server_mode;
35        $this->file_max = 0;
36    }
37
38    // ¥Õ¥¡¥¤¥ë¾ðÊóÄɲÃ
39    function addFile($disp_name, $keyname, $arrExt, $size, $necessary=false, $width=0, $height=0, $image=true) {
40        $this->disp_name[] = $disp_name;
41        $this->keyname[] = $keyname;
42        $this->width[] = $width;
43        $this->height[] = $height;
44        $this->arrExt[] = $arrExt;
45        $this->size[] = $size;
46        $this->necessary[] = $necessary;
47        $this->image[] = $image;
48    }
49    // ¥µ¥à¥Í¥¤¥ë²èÁü¤ÎºîÀ®
50    function makeThumb($src_file, $width, $height) {
51        // °ì°Õ¤ÊID¤ò¼èÆÀ¤¹¤ë¡£
52        $uniqname = date("mdHi") . "_" . uniqid("");
53       
54        $objThumb = new gdthumb();
55       
56        // WEB¥µ¡¼¥ÐÉé²Ùʬ»¶´Ä¶­¤Î¾ì¹ç
57        if($this->multi_web_server_mode === true) {
58            // FTPÍÑ¥Õ¥¡¥¤¥ë°ì»þ³ÊǼÍѥǥ£¥ì¥¯¥È¥ê
59            $ftp_temp_dir = $this->temp_dir . "/ftp";
60            // ¥Ç¥£¥ì¥¯¥È¥ê¤¬Â¸ºß¤·¤Ê¤«¤Ã¤¿¤éºîÀ®
61            if(!file_exists($ftp_temp_dir)) {
62                mkdir($ftp_temp_dir);
63            }
64           
65            $dst_file = $ftp_temp_dir . $uniqname;
66            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);
67            ftpMoveFile($this->ftp_dir, $ret[1]);
68        } else {
69            $dst_file = $this->temp_dir . $uniqname;
70            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);         
71        }
72       
73        if($ret[0] != 1) {
74            // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤Îɽ¼¨
75            print($ret[1]);
76            exit;
77        }
78       
79        return basename($ret[1]);
80    }
81       
82    // ¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤òÊݸ¤¹¤ë¡£
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                            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->save_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($this->temp_file[$cnt] != "") {
162                                                   
163                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
164                // ¤¹¤Ç¤ËÊݸ¥Õ¥¡¥¤¥ë¤¬¤¢¤Ã¤¿¾ì¹ç¤Ïºï½ü¤¹¤ë¡£
165                if($this->save_file[$cnt] != "" && !ereg("^sub/", $this->save_file[$cnt])) {
166                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
167                }
168            }
169            $cnt++;
170        }
171    }
172   
173    // HIDDENÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
174    function getHiddenFileList() {
175        $cnt = 0;
176        foreach($this->keyname as $val) {
177            if($this->temp_file[$cnt] != "") {
178                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
179            }
180            if($this->save_file[$cnt] != "") {
181                $arrRet["save_" . $val] = $this->save_file[$cnt];
182            }
183            $cnt++;
184        }
185        return $arrRet;
186    }
187   
188    // HIDDEN¤ÇÁ÷¤é¤ì¤Æ¤­¤¿¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ¤¹¤ë
189    function setHiddenFileList($arrPOST) {
190        $cnt = 0;
191        foreach($this->keyname as $val) {
192            $key = "temp_" . $val;
193            if($arrPOST[$key] != "") {
194                $this->temp_file[$cnt] = $arrPOST[$key];
195            }
196            $key = "save_" . $val;
197            if($arrPOST[$key] != "") {
198                $this->save_file[$cnt] = $arrPOST[$key];
199            }
200            $cnt++;
201        }
202    }
203   
204    // ¥Õ¥©¡¼¥à¤ËÅϤ¹ÍѤΥե¡¥¤¥ë¾ðÊóÇÛÎó¤òÊÖ¤¹
205    function getFormFileList($temp_url, $save_url, $real_size = false) {
206
207        $cnt = 0;
208        foreach($this->keyname as $val) {
209            if($this->temp_file[$cnt] != "") {
210                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
211                if(ereg("/$", $temp_url)) {
212                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
213                } else {
214                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
215                }
216                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
217            } elseif ($this->save_file[$cnt] != "") {
218                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
219                if(ereg("/$", $save_url)) {
220                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
221                } else {
222                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
223                }
224                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
225            }
226            if($arrRet[$val]['filepath'] != "") {
227                if($real_size){
228                    if(is_file($arrRet[$val]['real_filepath'])) {
229                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
230                    }
231                    // ¥Õ¥¡¥¤¥ë²£Éý
232                    $arrRet[$val]['width'] = $width;
233                    // ¥Õ¥¡¥¤¥ë½ÄÉý
234                    $arrRet[$val]['height'] = $height;
235                }else{
236                    // ¥Õ¥¡¥¤¥ë²£Éý
237                    $arrRet[$val]['width'] = $this->width[$cnt];
238                    // ¥Õ¥¡¥¤¥ë½ÄÉý
239                    $arrRet[$val]['height'] = $this->height[$cnt];
240                }
241                // ɽ¼¨Ì¾
242                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
243            }
244            $cnt++;
245        }
246        return $arrRet;
247    }
248   
249    // DBÊݸÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
250    function getDBFileList() {
251        $cnt = 0;
252        foreach($this->keyname as $val) {
253            if($this->temp_file[$cnt] != "") {
254                $arrRet[$val] = $this->temp_file[$cnt];
255            } else  {
256                $arrRet[$val] = $this->save_file[$cnt];
257            }
258            $cnt++;
259        }
260        return $arrRet;
261    }
262   
263    // DB¤ÇÊݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾ÇÛÎó¤ò¥»¥Ã¥È¤¹¤ë
264    function setDBFileList($arrVal) {
265        $cnt = 0;
266        foreach($this->keyname as $val) {
267            if($arrVal[$val] != "") {
268                $this->save_file[$cnt] = $arrVal[$val];
269            }
270            $cnt++;
271        }
272    }
273   
274    // ²èÁü¤ò¥»¥Ã¥È¤¹¤ë
275    function setDBImageList($arrVal) {
276        $cnt = 0;
277        foreach($this->keyname as $val) {
278            if($arrVal[$val] != "" && $val == 'tv_products_image') {
279                $this->save_file[$cnt] = $arrVal[$val];
280            }
281            $cnt++;
282        }
283    }
284   
285    // DB¾å¤Î¥Õ¥¡¥¤¥ë¤ÎÆâºï½üÍ׵᤬¤¢¤Ã¤¿¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¡£
286    function deleteDBFile($arrVal) {
287        $objImage = new SC_Image($this->temp_dir);
288        $cnt = 0;
289        foreach($this->keyname as $val) {
290            if($arrVal[$val] != "") {
291                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
292                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
293                }
294            }
295            $cnt++;
296        }
297    }
298   
299    // ɬ¿ÜȽÄê
300    function checkEXISTS($keyname = "") {
301        $cnt = 0;
302        $arrRet = array();
303        foreach($this->keyname as $val) {
304            if($val == $keyname || $keyname == "") {
305                // ɬ¿Ü¤Ç¤¢¤ì¤Ð¥¨¥é¡¼¥Á¥§¥Ã¥¯
306                if ($this->necessary[$cnt] == true) {
307                    if($this->save_file[$cnt] == "" && $this->temp_file[$cnt] == "") {
308                        $arrRet[$val] = "¢¨ " . $this->disp_name[$cnt] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br>";
309                    }
310                }
311            }
312            $cnt++;
313        }
314        return $arrRet;
315    }
316       
317    // ³ÈÂçΨ¤ò»ØÄꤷ¤Æ²èÁüÊݸ
318    function saveResizeImage($keyname, $to_w, $to_h) {
319        $path = "";
320       
321        // keyname¤ÎźÉÕ¥Õ¥¡¥¤¥ë¤ò¼èÆÀ
322        $arrImageKey = array_flip($this->keyname);
323        $file = $this->temp_file[$arrImageKey[$keyname]];
324        $filepath = $this->temp_dir . $file;
325       
326        $path = $this->makeThumb($filepath, $to_w, $to_h);
327       
328        // ¥Õ¥¡¥¤¥ë̾¤À¤±ÊÖ¤¹
329        return basename($path);
330    }
331
332    /**         
333     * ¥Õ¥¡¥¤¥ë¤òÁ´¤Æ¤ÎWEB¥µ¡¼¥Ð¤Ø¥³¥Ô¡¼¤·¤Þ¤¹¡£
334     *
335     * @param string $dst_path ¥³¥Ô¡¼Àè¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÁêÂХѥ¹)
336     * @param string $src_path ¥³¥Ô¡¼¸µ¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÀäÂХѥ¹)
337     * @return void
338     */
339    function ftpMoveFile($dst_path, $src_path) {
340        global $arrWEB_SERVERS;
341
342        // Á´¤Æ¤Î¥µ¡¼¥Ð¤Ë¥Õ¥¡¥¤¥ë¤ò¥³¥Ô¡¼¤¹¤ë
343        foreach($arrWEB_SERVERS as $array) {
344            sfFtpCopy($array['host'], $array['user'], $array['pass'], $dst_path, $src_path);           
345        }
346        // °Üư¸å¤Ï¥Õ¥¡¥¤¥ë¤òºï½ü
347        unlink($src_path);
348    }
349}
350?>
Note: See TracBrowser for help on using the repository browser.