[ Index ] |
|
Code source de osCommerce 2.2ms2-060817 |
1 <?php 2 /* 3 $Id: upload.php,v 1.2 2003/06/20 00:18:30 hpdl Exp $ 4 5 osCommerce, Open Source E-Commerce Solutions 6 http://www.oscommerce.com 7 8 Copyright (c) 2003 osCommerce 9 10 Released under the GNU General Public License 11 */ 12 13 class upload { 14 var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location; 15 16 function upload($file = '', $destination = '', $permissions = '777', $extensions = '') { 17 $this->set_file($file); 18 $this->set_destination($destination); 19 $this->set_permissions($permissions); 20 $this->set_extensions($extensions); 21 22 $this->set_output_messages('direct'); 23 24 if (tep_not_null($this->file) && tep_not_null($this->destination)) { 25 $this->set_output_messages('session'); 26 27 if ( ($this->parse() == true) && ($this->save() == true) ) { 28 return true; 29 } else { 30 return false; 31 } 32 } 33 } 34 35 function parse() { 36 global $messageStack; 37 38 if (isset($_FILES[$this->file])) { 39 $file = array('name' => $_FILES[$this->file]['name'], 40 'type' => $_FILES[$this->file]['type'], 41 'size' => $_FILES[$this->file]['size'], 42 'tmp_name' => $_FILES[$this->file]['tmp_name']); 43 } elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) { 44 global $HTTP_POST_FILES; 45 46 $file = array('name' => $HTTP_POST_FILES[$this->file]['name'], 47 'type' => $HTTP_POST_FILES[$this->file]['type'], 48 'size' => $HTTP_POST_FILES[$this->file]['size'], 49 'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']); 50 } else { 51 $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''), 52 'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''), 53 'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''), 54 'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : '')); 55 } 56 57 if ( tep_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) { 58 if (sizeof($this->extensions) > 0) { 59 if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) { 60 if ($this->message_location == 'direct') { 61 $messageStack->add(ERROR_FILETYPE_NOT_ALLOWED, 'error'); 62 } else { 63 $messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, 'error'); 64 } 65 66 return false; 67 } 68 } 69 70 $this->set_file($file); 71 $this->set_filename($file['name']); 72 $this->set_tmp_filename($file['tmp_name']); 73 74 return $this->check_destination(); 75 } else { 76 if ($this->message_location == 'direct') { 77 $messageStack->add(WARNING_NO_FILE_UPLOADED, 'warning'); 78 } else { 79 $messageStack->add_session(WARNING_NO_FILE_UPLOADED, 'warning'); 80 } 81 82 return false; 83 } 84 } 85 86 function save() { 87 global $messageStack; 88 89 if (substr($this->destination, -1) != '/') $this->destination .= '/'; 90 91 if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) { 92 chmod($this->destination . $this->filename, $this->permissions); 93 94 if ($this->message_location == 'direct') { 95 $messageStack->add(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success'); 96 } else { 97 $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success'); 98 } 99 100 return true; 101 } else { 102 if ($this->message_location == 'direct') { 103 $messageStack->add(ERROR_FILE_NOT_SAVED, 'error'); 104 } else { 105 $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error'); 106 } 107 108 return false; 109 } 110 } 111 112 function set_file($file) { 113 $this->file = $file; 114 } 115 116 function set_destination($destination) { 117 $this->destination = $destination; 118 } 119 120 function set_permissions($permissions) { 121 $this->permissions = octdec($permissions); 122 } 123 124 function set_filename($filename) { 125 $this->filename = $filename; 126 } 127 128 function set_tmp_filename($filename) { 129 $this->tmp_filename = $filename; 130 } 131 132 function set_extensions($extensions) { 133 if (tep_not_null($extensions)) { 134 if (is_array($extensions)) { 135 $this->extensions = $extensions; 136 } else { 137 $this->extensions = array($extensions); 138 } 139 } else { 140 $this->extensions = array(); 141 } 142 } 143 144 function check_destination() { 145 global $messageStack; 146 147 if (!is_writeable($this->destination)) { 148 if (is_dir($this->destination)) { 149 if ($this->message_location == 'direct') { 150 $messageStack->add(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); 151 } else { 152 $messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); 153 } 154 } else { 155 if ($this->message_location == 'direct') { 156 $messageStack->add(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error'); 157 } else { 158 $messageStack->add_session(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error'); 159 } 160 } 161 162 return false; 163 } else { 164 return true; 165 } 166 } 167 168 function set_output_messages($location) { 169 switch ($location) { 170 case 'session': 171 $this->message_location = 'session'; 172 break; 173 case 'direct': 174 default: 175 $this->message_location = 'direct'; 176 break; 177 } 178 } 179 } 180 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 19:48:25 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |