[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
Zen Cart 源代码 upload.php

Zen Cart 源代码 upload.php




下载文件

文件名: upload.php
文件类型: PHP文件
文件大小: 7.6 KiB
MD5: bea92a253fc4f3d48a30e0ab97c05edb

upload.php - 关闭高亮
  1. <?php
  2. /**
  3.  * upload Class.
  4.  *
  5.  * @package classes
  6.  * @copyright Copyright 2003-2010 Zen Cart Development Team
  7.  * @copyright Portions Copyright 2003 osCommerce
  8.  * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9.  * @version $Id: upload.php 15838 2010-04-06 17:21:09Z ajeh $
  10.  */
  11. if (!defined('IS_ADMIN_FLAG')) {
  12.   die('Illegal Access');
  13. }
  14. /**
  15.  * upload Class.
  16.  * This class is used to manage file uploads
  17.  *
  18.  * @package classes
  19.  */
  20. class upload extends base {
  21.   var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location;
  22.  
  23.   function upload($file = '', $destination = '', $permissions = '644', $extensions = array() ) {
  24.     $this->set_file($file);
  25.     $this->set_destination($destination);
  26.     $this->set_permissions($permissions);
  27.  
  28.     if (!zen_not_null($extensions)) {
  29.       if (!defined(UPLOAD_FILENAME_EXTENSIONS)) define ('UPLOAD_FILENAME_EXTENSIONS','jpg,jpeg,gif,png,eps,cdr,ai,pdf,tif,tiff,bmp,zip');
  30.       $extensions=explode(" ",preg_replace('/[.,;\s]+/',' ',UPLOAD_FILENAME_EXTENSIONS));
  31.     }
  32.     $this->set_extensions($extensions);
  33.  
  34.     $this->set_output_messages('direct');
  35.  
  36.     if (zen_not_null($this->file) && zen_not_null($this->destination)) {
  37.       $this->set_output_messages('session');
  38.  
  39.       if ( ($this->parse() == true) && ($this->save() == true) ) {
  40.         return true;
  41.       } else {
  42.         // self destruct
  43.         while(list($key,) = each($this)) {
  44.           $this->$key = null;
  45.         }
  46.  
  47.         return false;
  48.       }
  49.     }
  50.   }
  51.  
  52.   // iii Added: $key to differentiate between different files uploaded
  53.   function parse($key = '') {
  54.     global $messageStack;
  55.  
  56.     if (isset($_FILES[$this->file])) {
  57.       if (zen_not_null($key)) {
  58.         $file = array('name' => $_FILES[$this->file]['name'][$key],
  59.         'type' => $_FILES[$this->file]['type'][$key],
  60.         'size' => $_FILES[$this->file]['size'][$key],
  61.         'tmp_name' => $_FILES[$this->file]['tmp_name'][$key]);
  62.       } else {
  63.         $file = array('name' => $_FILES[$this->file]['name'],
  64.         'type' => $_FILES[$this->file]['type'],
  65.         'size' => $_FILES[$this->file]['size'],
  66.         'tmp_name' => $_FILES[$this->file]['tmp_name']);
  67.       }
  68.     } elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) {
  69.       global $HTTP_POST_FILES;
  70.  
  71.       $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
  72.       'type' => $HTTP_POST_FILES[$this->file]['type'],
  73.       'size' => $HTTP_POST_FILES[$this->file]['size'],
  74.       'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
  75.     } else {
  76.       $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
  77.       'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
  78.       'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
  79.       'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
  80.     }
  81.     //if (!zen_not_null($file['tmp_name'])) return false;
  82.     //if ($file['tmp_name'] == 'none') return false;
  83.     //if (!is_uploaded_file($file['tmp_name'])) return false;
  84.  
  85. // not working at this time to test for server limit error
  86.     if (!is_uploaded_file($file['tmp_name'])) {
  87.       if ($this->message_location == 'direct') {
  88.         $messageStack->add_session('header', WARNING_NO_FILE_UPLOADED, 'warning');
  89.       } else {
  90.         $messageStack->add_session('upload', WARNING_NO_FILE_UPLOADED, 'warning');
  91.       }
  92.       return false;
  93.     }
  94.  
  95.     if ( zen_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
  96.       if (zen_not_null($file['size']) and ($file['size'] > MAX_FILE_UPLOAD_SIZE)) {
  97.         if ($this->message_location == 'direct') {
  98.           $messageStack->add_session('header', ERROR_FILE_TOO_BIG, 'error');
  99.         } else {
  100.           $messageStack->add_session('upload', ERROR_FILE_TOO_BIG, 'error');
  101.         }
  102.         return false;
  103.       }
  104.  
  105.       if (sizeof($this->extensions) > 0) {
  106.         if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
  107.           if ($this->message_location == 'direct') {
  108.             $messageStack->add_session('header', ERROR_FILETYPE_NOT_ALLOWED . ' ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
  109.           } else {
  110.             $messageStack->add_session('upload', ERROR_FILETYPE_NOT_ALLOWED . ' - ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
  111.           }
  112.           return false;
  113.         }
  114.       }
  115.  
  116.       $this->set_file($file);
  117.       $this->set_filename($file['name']);
  118.       $this->set_tmp_filename($file['tmp_name']);
  119.  
  120.       return $this->check_destination();
  121.     } else {
  122.       if ($this->message_location == 'direct') {
  123.         $messageStack->add_session('header', WARNING_NO_FILE_UPLOADED, 'warning');
  124.       } else {
  125.         $messageStack->add_session('upload', WARNING_NO_FILE_UPLOADED, 'warning');
  126.       }
  127.       return false;
  128.     }
  129.   }
  130.  
  131.   function save() {
  132.     global $messageStack;
  133.  
  134.     if (substr($this->destination, -1) != '/') $this->destination .= '/';
  135.  
  136.     if (@move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) {
  137.       chmod($this->destination . $this->filename, $this->permissions);
  138.  
  139.       if ($this->message_location == 'direct') {
  140.         $messageStack->add_session('header', SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  141.       } else {
  142.         $messageStack->add_session('upload', SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  143.       }
  144.  
  145.       return true;
  146.     } else {
  147.       if ($this->message_location == 'direct') {
  148.         $messageStack->add_session('header', ERROR_FILE_NOT_SAVED, 'error');
  149.       } else {
  150.         $messageStack->add_session('upload', ERROR_FILE_NOT_SAVED, 'error');
  151.       }
  152.  
  153.       return false;
  154.     }
  155.   }
  156.  
  157.   function set_file($file) {
  158.     $this->file = $file;
  159.   }
  160.  
  161.   function set_destination($destination) {
  162.     $this->destination = $destination;
  163.   }
  164.  
  165.   function set_permissions($permissions) {
  166.     $this->permissions = octdec($permissions);
  167.   }
  168.  
  169.   function set_filename($filename) {
  170.     $this->filename = $filename;
  171.   }
  172.  
  173.   function set_tmp_filename($filename) {
  174.     $this->tmp_filename = $filename;
  175.   }
  176.  
  177.   function set_extensions($extensions) {
  178.     if (zen_not_null($extensions)) {
  179.       if (is_array($extensions)) {
  180.         $this->extensions = $extensions;
  181.       } else {
  182.         $this->extensions = array($extensions);
  183.       }
  184.     } else {
  185.       $this->extensions = array();
  186.     }
  187.   }
  188.  
  189.   function check_destination() {
  190.     global $messageStack;
  191.  
  192.     if (!is_writeable($this->destination)) {
  193.       if (is_dir($this->destination)) {
  194.         if ($this->message_location == 'direct') {
  195.           $messageStack->add_session('header', sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
  196.         } else {
  197.           $messageStack->add_session('upload', sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
  198.         }
  199.       } else {
  200.         if ($this->message_location == 'direct') {
  201.           $messageStack->add_session('header', sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
  202.         } else {
  203.           $messageStack->add_session('upload', sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
  204.         }
  205.       }
  206.  
  207.       return false;
  208.     } else {
  209.       return true;
  210.     }
  211.   }
  212.  
  213.   function set_output_messages($location) {
  214.     switch ($location) {
  215.       case 'session':
  216.       $this->message_location = 'session';
  217.       break;
  218.       case 'direct':
  219.       default:
  220.       $this->message_location = 'direct';
  221.       break;
  222.     }
  223.   }
  224. }
  225.