| [ Index ] | osCommerce
Docs :: PHP Cross Reference For osCommerce 2.2 MS2 Provided By OSCdox.com |
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 // self destruct 31 $this = null; 32 33 return false; 34 } 35 } 36 } 37 38 function parse() { 39 global $messageStack; 40 41 if (isset($_FILES[$this->file])) { 42 $file = array('name' => $_FILES[$this->file]['name'], 43 'type' => $_FILES[$this->file]['type'], 44 'size' => $_FILES[$this->file]['size'], 45 'tmp_name' => $_FILES[$this->file]['tmp_name']); 46 } elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) { 47 global $HTTP_POST_FILES; 48 49 $file = array('name' => $HTTP_POST_FILES[$this->file]['name'], 50 'type' => $HTTP_POST_FILES[$this->file]['type'], 51 'size' => $HTTP_POST_FILES[$this->file]['size'], 52 'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']); 53 } else { 54 $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''), 55 'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''), 56 'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''), 57 'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : '')); 58 } 59 60 if ( tep_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) { 61 if (sizeof($this->extensions) > 0) { 62 if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) { 63 if ($this->message_location == 'direct') { 64 $messageStack->add(ERROR_FILETYPE_NOT_ALLOWED, 'error'); 65 } else { 66 $messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, 'error'); 67 } 68 69 return false; 70 } 71 } 72 73 $this->set_file($file); 74 $this->set_filename($file['name']); 75 $this->set_tmp_filename($file['tmp_name']); 76 77 return $this->check_destination(); 78 } else { 79 if ($this->message_location == 'direct') { 80 $messageStack->add(WARNING_NO_FILE_UPLOADED, 'warning'); 81 } else { 82 $messageStack->add_session(WARNING_NO_FILE_UPLOADED, 'warning'); 83 } 84 85 return false; 86 } 87 } 88 89 function save() { 90 global $messageStack; 91 92 if (substr($this->destination, -1) != '/') $this->destination .= '/'; 93 94 if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) { 95 chmod($this->destination . $this->filename, $this->permissions); 96 97 if ($this->message_location == 'direct') { 98 $messageStack->add(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success'); 99 } else { 100 $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success'); 101 } 102 103 return true; 104 } else { 105 if ($this->message_location == 'direct') { 106 $messageStack->add(ERROR_FILE_NOT_SAVED, 'error'); 107 } else { 108 $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error'); 109 } 110 111 return false; 112 } 113 } 114 115 function set_file($file) { 116 $this->file = $file; 117 } 118 119 function set_destination($destination) { 120 $this->destination = $destination; 121 } 122 123 function set_permissions($permissions) { 124 $this->permissions = octdec($permissions); 125 } 126 127 function set_filename($filename) { 128 $this->filename = $filename; 129 } 130 131 function set_tmp_filename($filename) { 132 $this->tmp_filename = $filename; 133 } 134 135 function set_extensions($extensions) { 136 if (tep_not_null($extensions)) { 137 if (is_array($extensions)) { 138 $this->extensions = $extensions; 139 } else { 140 $this->extensions = array($extensions); 141 } 142 } else { 143 $this->extensions = array(); 144 } 145 } 146 147 function check_destination() { 148 global $messageStack; 149 150 if (!is_writeable($this->destination)) { 151 if (is_dir($this->destination)) { 152 if ($this->message_location == 'direct') { 153 $messageStack->add(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); 154 } else { 155 $messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); 156 } 157 } else { 158 if ($this->message_location == 'direct') { 159 $messageStack->add(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error'); 160 } else { 161 $messageStack->add_session(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error'); 162 } 163 } 164 165 return false; 166 } else { 167 return true; 168 } 169 } 170 171 function set_output_messages($location) { 172 switch ($location) { 173 case 'session': 174 $this->message_location = 'session'; 175 break; 176 case 'direct': 177 default: 178 $this->message_location = 'direct'; 179 break; 180 } 181 } 182 } 183 ?>
title
Description
Body
title
Description
Body
| Generated: Tue Nov 4 23:53:39 2003 | Hosted By :: AABox.com |
Cross-referenced by PHPXref 0.4 |