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

Revision 14424, 12.9 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_temp_dir;              // FTPÀè°ì»þ¥Ç¥£¥ì¥¯¥È¥ê(Éé²Ùʬ»¶»þ»ÈÍÑ)
17    var $ftp_save_dir;              // FTPÀè¥Õ¥¡¥¤¥ëÊݸ¥Ç¥£¥ì¥¯¥È¥ê(Éé²Ùʬ»¶»þ»ÈÍÑ)
18    var $multi_web_server_mode;     // Éé²Ùʬ»¶¥Õ¥é¥°(Éé²Ùʬ»¶»þ»ÈÍÑ)
19    var $keyname;                   // ¥Õ¥¡¥¤¥ëinput¥¿¥°¤Îname
20    var $width;                     // ²£¥µ¥¤¥º
21    var $height;                    // ½Ä¥µ¥¤¥º
22    var $arrExt;                    // »ØÄꤹ¤ë³ÈÄ¥»Ò
23    var $temp_file;                 // Êݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾
24    var $save_file;                 // DB¤«¤éÆÉ¤ß½Ð¤·¤¿¥Õ¥¡¥¤¥ë̾
25    var $disp_name;                 // ¹àÌÜ̾
26    var $size;                      // À©¸Â¥µ¥¤¥º
27    var $necessary;                 // ɬ¿Ü¤Î¾ì¹ç:true
28    var $image;                     // ²èÁü¤Î¾ì¹ç:true
29   
30    // ¥Õ¥¡¥¤¥ë´ÉÍý¥¯¥é¥¹
31    function SC_UploadFile($temp_dir, $save_dir, $ftp_temp_dir = "", $ftp_save_dir = "", $multi_web_server_mode = false) {
32        $this->temp_dir = $temp_dir;
33        $this->save_dir = $save_dir;
34        $this->ftp_temp_dir = $ftp_temp_dir;
35        $this->ftp_save_dir = $ftp_save_dir;
36        $this->multi_web_server_mode = $multi_web_server_mode;
37        $this->file_max = 0;
38    }
39
40    // ¥Õ¥¡¥¤¥ë¾ðÊóÄɲÃ
41    function addFile($disp_name, $keyname, $arrExt, $size, $necessary=false, $width=0, $height=0, $image=true) {
42        $this->disp_name[] = $disp_name;
43        $this->keyname[] = $keyname;
44        $this->width[] = $width;
45        $this->height[] = $height;
46        $this->arrExt[] = $arrExt;
47        $this->size[] = $size;
48        $this->necessary[] = $necessary;
49        $this->image[] = $image;
50    }
51    // ¥µ¥à¥Í¥¤¥ë²èÁü¤ÎºîÀ®
52    function makeThumb($src_file, $width, $height) {
53        // °ì°Õ¤ÊID¤ò¼èÆÀ¤¹¤ë¡£
54        $uniqname = date("mdHi") . "_" . uniqid("");
55       
56        $objThumb = new gdthumb();
57       
58        // WEB¥µ¡¼¥ÐÉé²Ùʬ»¶´Ä¶­¤Î¾ì¹ç
59        if($this->multi_web_server_mode === true) {
60            // FTPÍÑ¥Õ¥¡¥¤¥ë°ì»þ³ÊǼÍѥǥ£¥ì¥¯¥È¥ê
61            $ftp_temp_dir = $this->temp_dir . "ftp_temp/";
62            // ¥Ç¥£¥ì¥¯¥È¥ê¤¬Â¸ºß¤·¤Ê¤«¤Ã¤¿¤éºîÀ®
63            if(!file_exists($ftp_temp_dir)) {
64                mkdir($ftp_temp_dir, 0777);
65            }
66           
67            $dst_file = $ftp_temp_dir . $uniqname;
68            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);
69            $this->ftpMoveFile($this->ftp_temp_dir . basename($ret[1]), $ret[1]);
70        } else {
71            $dst_file = $this->temp_dir . $uniqname;
72            $ret = $objThumb->Main($src_file, $width, $height, $dst_file);         
73        }
74       
75        if($ret[0] != 1) {
76            // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤Îɽ¼¨
77            print($ret[1]);
78            exit;
79        }
80       
81        return basename($ret[1]);
82    }
83       
84    // ¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤òÊݸ¤¹¤ë¡£
85    function makeTempFile($keyname, $rename = true) {
86        $objErr = new SC_CheckError();
87        $cnt = 0;
88        $arrKeyname = array_flip($this->keyname);
89       
90        if(!($_FILES[$keyname]['size'] > 0)) {
91            $objErr->arrErr[$keyname] = "¢¨ " . $this->disp_name[$arrKeyname[$keyname]] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br />";
92        } else {
93            foreach($this->keyname as $val) {
94                // °ìÃפ·¤¿¥­¡¼¤Î¥Õ¥¡¥¤¥ë¤Ë¾ðÊó¤òÊݸ¤¹¤ë¡£
95                if ($val == $keyname) {
96                    // ³ÈÄ¥»Ò¥Á¥§¥Ã¥¯
97                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->arrExt[$cnt]), array("FILE_EXT_CHECK"));
98                    // ¥Õ¥¡¥¤¥ë¥µ¥¤¥º¥Á¥§¥Ã¥¯
99                    $objErr->doFunc(array($this->disp_name[$cnt], $keyname, $this->size[$cnt]), array("FILE_SIZE_CHECK"));
100                    // ¥¨¥é¡¼¤¬¤Ê¤¤¾ì¹ç
101                    if(!isset($objErr->arrErr[$keyname])) {
102                        // ²èÁü¥Õ¥¡¥¤¥ë¤Î¾ì¹ç
103                        if($this->image[$cnt]) {
104                            $this->temp_file[$cnt] = $this->makeThumb($_FILES[$keyname]['tmp_name'], $this->width[$cnt], $this->height[$cnt]);
105                        // ²èÁü¥Õ¥¡¥¤¥ë°Ê³°¤Î¾ì¹ç
106                        } else {
107                            // °ì°Õ¤Ê¥Õ¥¡¥¤¥ë̾¤òºîÀ®¤¹¤ë¡£
108                            if($rename) {
109                                $uniqname = date("mdHi") . "_" . uniqid("").".";
110                                $this->temp_file[$cnt] = ereg_replace("^.*\.",$uniqname, $_FILES[$keyname]['name']);
111                            } else {
112                                $this->temp_file[$cnt] = $_FILES[$keyname]['name'];   
113                            }
114                            $result  = copy($_FILES[$keyname]['tmp_name'], $this->temp_dir. "/". $this->temp_file[$cnt]);
115                            gfPrintLog($_FILES[$keyname]['name']." -> ".$this->temp_dir. "/". $this->temp_file[$cnt]);
116                        }
117                    }
118                }
119                $cnt++;
120            }
121        }
122        return $objErr->arrErr[$keyname];
123    }
124
125    // ²èÁü¤òºï½ü¤¹¤ë¡£
126    function deleteFile($keyname) {
127        $objImage = new SC_Image($this->temp_dir);
128        $cnt = 0;
129        foreach($this->keyname as $val) {
130            if ($val == $keyname) {
131                // °ì»þ¥Õ¥¡¥¤¥ë¤Î¾ì¹çºï½ü¤¹¤ë¡£
132                if($this->temp_file[$cnt] != "") {
133                    $objImage->deleteImage($this->temp_file[$cnt], $this->save_dir);
134                }
135                $this->temp_file[$cnt] = "";
136                $this->save_file[$cnt] = "";
137            }
138            $cnt++;
139        }
140    }
141   
142    // °ì»þ¥Õ¥¡¥¤¥ë¥Ñ¥¹¤ò¼èÆÀ¤¹¤ë¡£
143    function getTempFilePath($keyname) {
144        $cnt = 0;
145        $filepath = "";
146        foreach($this->keyname as $val) {
147            if ($val == $keyname) {
148                if($this->temp_file[$cnt] != "") {
149                    $filepath = $this->temp_dir . "/" . $this->temp_file[$cnt];
150                }
151            }
152            $cnt++;
153        }
154        return $filepath;
155    }
156   
157    // °ì»þ¥Õ¥¡¥¤¥ë¤òÊݸ¥Ç¥£¥ì¥¯¥È¥ê¤Ë°Ü¤¹
158    function moveTempFile() {
159        $cnt = 0;
160        $objImage = new SC_Image($this->temp_dir);
161       
162        foreach($this->keyname as $val) {
163            if($this->temp_file[$cnt] != "") {
164                                                   
165                $objImage->moveTempImage($this->temp_file[$cnt], $this->save_dir);
166                // ¤¹¤Ç¤ËÊݸ¥Õ¥¡¥¤¥ë¤¬¤¢¤Ã¤¿¾ì¹ç¤Ïºï½ü¤¹¤ë¡£
167                if($this->save_file[$cnt] != "" && !ereg("^sub/", $this->save_file[$cnt])) {
168                    $objImage->deleteImage($this->save_file[$cnt], $this->save_dir);
169                }
170            }
171            $cnt++;
172        }
173    }
174   
175    // HIDDENÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
176    function getHiddenFileList() {
177        $cnt = 0;
178        foreach($this->keyname as $val) {
179            if($this->temp_file[$cnt] != "") {
180                $arrRet["temp_" . $val] = $this->temp_file[$cnt];
181            }
182            if($this->save_file[$cnt] != "") {
183                $arrRet["save_" . $val] = $this->save_file[$cnt];
184            }
185            $cnt++;
186        }
187        return $arrRet;
188    }
189   
190    // HIDDEN¤ÇÁ÷¤é¤ì¤Æ¤­¤¿¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ¤¹¤ë
191    function setHiddenFileList($arrPOST) {
192        $cnt = 0;
193        foreach($this->keyname as $val) {
194            $key = "temp_" . $val;
195            if($arrPOST[$key] != "") {
196                $this->temp_file[$cnt] = $arrPOST[$key];
197            }
198            $key = "save_" . $val;
199            if($arrPOST[$key] != "") {
200                $this->save_file[$cnt] = $arrPOST[$key];
201            }
202            $cnt++;
203        }
204    }
205   
206    // ¥Õ¥©¡¼¥à¤ËÅϤ¹ÍѤΥե¡¥¤¥ë¾ðÊóÇÛÎó¤òÊÖ¤¹
207    function getFormFileList($temp_url, $save_url, $real_size = false) {
208
209        $cnt = 0;
210        foreach($this->keyname as $val) {
211            if($this->temp_file[$cnt] != "") {
212                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
213                if(ereg("/$", $temp_url)) {
214                    $arrRet[$val]['filepath'] = $temp_url . $this->temp_file[$cnt];
215                } else {
216                    $arrRet[$val]['filepath'] = $temp_url . "/" . $this->temp_file[$cnt];
217                }
218                $arrRet[$val]['real_filepath'] = $this->temp_dir . $this->temp_file[$cnt];
219            } elseif ($this->save_file[$cnt] != "") {
220                // ¥Õ¥¡¥¤¥ë¥Ñ¥¹¥Á¥§¥Ã¥¯(¥Ñ¥¹¤Î¥¹¥é¥Ã¥·¥å/¤¬Ï¢Â³¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¡£)
221                if(ereg("/$", $save_url)) {
222                    $arrRet[$val]['filepath'] = $save_url . $this->save_file[$cnt];
223                } else {
224                    $arrRet[$val]['filepath'] = $save_url . "/" . $this->save_file[$cnt];
225                }
226                $arrRet[$val]['real_filepath'] = $this->save_dir . $this->save_file[$cnt];
227            }
228            if($arrRet[$val]['filepath'] != "") {
229                if($real_size){
230                    if(is_file($arrRet[$val]['real_filepath'])) {
231                        list($width, $height) = getimagesize($arrRet[$val]['real_filepath']);
232                    }
233                    // ¥Õ¥¡¥¤¥ë²£Éý
234                    $arrRet[$val]['width'] = $width;
235                    // ¥Õ¥¡¥¤¥ë½ÄÉý
236                    $arrRet[$val]['height'] = $height;
237                }else{
238                    // ¥Õ¥¡¥¤¥ë²£Éý
239                    $arrRet[$val]['width'] = $this->width[$cnt];
240                    // ¥Õ¥¡¥¤¥ë½ÄÉý
241                    $arrRet[$val]['height'] = $this->height[$cnt];
242                }
243                // ɽ¼¨Ì¾
244                $arrRet[$val]['disp_name'] = $this->disp_name[$cnt];
245            }
246            $cnt++;
247        }
248        return $arrRet;
249    }
250   
251    // DBÊݸÍѤΥե¡¥¤¥ë̾ÇÛÎó¤òÊÖ¤¹
252    function getDBFileList() {
253        $cnt = 0;
254        foreach($this->keyname as $val) {
255            if($this->temp_file[$cnt] != "") {
256                $arrRet[$val] = $this->temp_file[$cnt];
257            } else  {
258                $arrRet[$val] = $this->save_file[$cnt];
259            }
260            $cnt++;
261        }
262        return $arrRet;
263    }
264   
265    // DB¤ÇÊݸ¤µ¤ì¤¿¥Õ¥¡¥¤¥ë̾ÇÛÎó¤ò¥»¥Ã¥È¤¹¤ë
266    function setDBFileList($arrVal) {
267        $cnt = 0;
268        foreach($this->keyname as $val) {
269            if($arrVal[$val] != "") {
270                $this->save_file[$cnt] = $arrVal[$val];
271            }
272            $cnt++;
273        }
274    }
275   
276    // ²èÁü¤ò¥»¥Ã¥È¤¹¤ë
277    function setDBImageList($arrVal) {
278        $cnt = 0;
279        foreach($this->keyname as $val) {
280            if($arrVal[$val] != "" && $val == 'tv_products_image') {
281                $this->save_file[$cnt] = $arrVal[$val];
282            }
283            $cnt++;
284        }
285    }
286   
287    // DB¾å¤Î¥Õ¥¡¥¤¥ë¤ÎÆâºï½üÍ׵᤬¤¢¤Ã¤¿¥Õ¥¡¥¤¥ë¤òºï½ü¤¹¤ë¡£
288    function deleteDBFile($arrVal) {
289        $objImage = new SC_Image($this->temp_dir);
290        $cnt = 0;
291        foreach($this->keyname as $val) {
292            if($arrVal[$val] != "") {
293                if($this->save_file[$cnt] == "" && !ereg("^sub/", $arrVal[$val])) {
294                    $objImage->deleteImage($arrVal[$val], $this->save_dir);
295                }
296            }
297            $cnt++;
298        }
299    }
300   
301    // ɬ¿ÜȽÄê
302    function checkEXISTS($keyname = "") {
303        $cnt = 0;
304        $arrRet = array();
305        foreach($this->keyname as $val) {
306            if($val == $keyname || $keyname == "") {
307                // ɬ¿Ü¤Ç¤¢¤ì¤Ð¥¨¥é¡¼¥Á¥§¥Ã¥¯
308                if ($this->necessary[$cnt] == true) {
309                    if($this->save_file[$cnt] == "" && $this->temp_file[$cnt] == "") {
310                        $arrRet[$val] = "¢¨ " . $this->disp_name[$cnt] . "¤¬¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤Æ¤¤¤Þ¤»¤ó¡£<br>";
311                    }
312                }
313            }
314            $cnt++;
315        }
316        return $arrRet;
317    }
318       
319    // ³ÈÂçΨ¤ò»ØÄꤷ¤Æ²èÁüÊݸ
320    function saveResizeImage($keyname, $to_w, $to_h) {
321        $path = "";
322       
323        // keyname¤ÎźÉÕ¥Õ¥¡¥¤¥ë¤ò¼èÆÀ
324        $arrImageKey = array_flip($this->keyname);
325        $file = $this->temp_file[$arrImageKey[$keyname]];
326        $filepath = $this->temp_dir . $file;
327       
328        $path = $this->makeThumb($filepath, $to_w, $to_h);
329       
330        // ¥Õ¥¡¥¤¥ë̾¤À¤±ÊÖ¤¹
331        return basename($path);
332    }
333
334    /**         
335     * ¥Õ¥¡¥¤¥ë¤òÁ´¤Æ¤ÎWEB¥µ¡¼¥Ð¤Ø¥³¥Ô¡¼¤·¤Þ¤¹¡£
336     *
337     * @param string $dst_path ¥³¥Ô¡¼Àè¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÁêÂХѥ¹)
338     * @param string $src_path ¥³¥Ô¡¼¸µ¥Õ¥¡¥¤¥ë¥Ñ¥¹(ÀäÂХѥ¹)
339     * @return void
340     */
341    function ftpMoveFile($dst_path, $src_path) {
342        global $arrWEB_SERVERS;
343
344        // Á´¤Æ¤Î¥µ¡¼¥Ð¤Ë¥Õ¥¡¥¤¥ë¤ò¥³¥Ô¡¼¤¹¤ë
345        foreach($arrWEB_SERVERS as $array) {
346            sfFtpCopy($array['host'], $array['user'], $array['pass'], $dst_path, $src_path);           
347        }
348        // °Üư¸å¤Ï¥Õ¥¡¥¤¥ë¤òºï½ü
349        //unlink($src_path);
350    }
351}
352?>
Note: See TracBrowser for help on using the repository browser.