За последние 24 часа нас посетили 17520 программистов и 1722 робота. Сейчас ищут 965 программистов ...

Помогите с загрузчиком файлов

Тема в разделе "PHP и базы данных", создана пользователем viphost, 16 ноя 2010.

  1. viphost

    viphost Активный пользователь

    С нами с:
    23 мар 2009
    Сообщения:
    307
    Симпатии:
    0
    Всем привет, уже день голову ломаю. но так и не додумался сам, если скрипт по загрузки картинок на сервер, но поддерживает он только .jpg формат, а нужно сделать чтоб и .png и .gif поддерживал, но как?!
    Вот содержимое файла который отвечает за загрузку:
    PHP:
    1. <?php
    2. /*
    3.  
    4.  
    5.     $config['upload_path'] = './uploads/';
    6.     $config['allowed_types'] = 'gif|jpg|png';
    7.     $config['max_size'] = '100';
    8.     $config['max_width']  = '1024';
    9.     $config['max_height']  = '768';
    10.    
    11.     $upload = new Upload($config);
    12.  
    13.     $upload->doUpload('field');
    14.  
    15.  
    16.  */
    17. class Upload {
    18.    
    19.     public $max_size       = 0;
    20.     public $max_width      = 0;
    21.     public $max_height     = 0;
    22.     public $allowed_types  = "";
    23.     public $file_temp      = "";
    24.     public $file_name      = "";
    25.     public $orig_name      = "";
    26.     public $file_type      = "";
    27.     public $file_size      = "";
    28.     public $file_ext       = "";
    29.     public $upload_path    = "";
    30.     public $overwrite      = false;
    31.     public $encrypt_name   = false;
    32.     public $is_image       = false;
    33.     public $image_width    = '';
    34.     public $image_height   = '';
    35.     public $image_type     = '';
    36.     public $image_size_str = '';
    37.     public $error_msg      = array();
    38.     public $remove_spaces  = true;
    39.     public $xss_clean      = false;
    40.     public $temp_prefix    = "temp_file_";
    41.  
    42.     static private $mimes;
    43.        
    44.     /**
    45.      * Constructor
    46.      *
    47.      * @access  public
    48.      */
    49.     function __construct($props = array())
    50.     {
    51.         if (count($props) > 0)
    52.         {
    53.             $this->initialize($props);
    54.         }
    55.        
    56.         //log_debug("Upload Class Initialized");
    57.     }
    58.    
    59.     static public function msg($key)
    60.     {
    61.         $lang['upload_userfile_not_set'] = __('Unable to find a post variable called userfile.');
    62.         $lang['upload_file_exceeds_limit'] = __('The uploaded file exceeds the maximum allowed size in your PHP configuration file.');
    63.         $lang['upload_file_exceeds_form_limit'] = __('The uploaded file exceeds the maximum size allowed by the submission form.');
    64.         $lang['upload_file_partial'] = __('The file was only partially uploaded.');
    65.         $lang['upload_no_temp_directory'] = __('The temporary folder is missing.');
    66.         $lang['upload_unable_to_write_file'] = __('The file could not be written to disk.');
    67.         $lang['upload_stopped_by_extension'] = __('The file upload was stopped by extension.');
    68.         $lang['upload_no_file_selected'] = __('You did not select a file to upload.');
    69.         $lang['upload_invalid_filetype'] = __('The filetype you are attempting to upload is not allowed.');
    70.         $lang['upload_invalid_filesize'] = __('The file you are attempting to upload is larger than the permitted size.');
    71.         $lang['upload_invalid_dimensions'] = __('The image you are attempting to upload exceedes the maximum height or width.');
    72.         $lang['upload_destination_error'] = __('A problem was encountered while attempting to move the uploaded file to the final destination.');
    73.         $lang['upload_no_filepath'] = __('The upload path does not appear to be valid.');
    74.         $lang['upload_no_file_types'] = __('You have not specified any allowed file types.');
    75.         $lang['upload_bad_filename'] = __('The file name you submitted already exists on the server.');
    76.         $lang['upload_not_writable'] = __('The upload destination folder does not appear to be writable.');
    77.        
    78.         return $lang[$key]?$lang[$key]:$key;
    79.     }
    80.    
    81.     // --------------------------------------------------------------------
    82.    
    83.     /**
    84.      * Initialize preferences
    85.      *
    86.      * @access  public
    87.      * @param   array
    88.      * @return  void
    89.      */
    90.     function initialize($config = array())
    91.     {
    92.         $defaults = array(
    93.                             'max_size'          => 0,
    94.                             'max_width'         => 0,
    95.                             'max_height'        => 0,
    96.                             'allowed_types'     => "",
    97.                             'file_temp'         => "",
    98.                             'file_name'         => "",
    99.                             'orig_name'         => "",
    100.                             'file_type'         => "",
    101.                             'file_size'         => "",
    102.                             'file_ext'          => "",
    103.                             'upload_path'       => "",
    104.                             'overwrite'         => false,
    105.                             'encrypt_name'      => false,
    106.                             'is_image'          => false,
    107.                             'image_width'       => '',
    108.                             'image_height'      => '',
    109.                             'image_type'        => '',
    110.                             'image_size_str'    => '',
    111.                             'error_msg'         => array(),
    112.                             'mimes'             => array(),
    113.                             'remove_spaces'     => true,
    114.                             'xss_clean'         => false,
    115.                             'temp_prefix'       => "temp_file_"
    116.                         );  
    117.    
    118.    
    119.         foreach ($defaults as $key => $val) {
    120.             if (isset($config[$key])) {
    121.  
    122.                 $method = 'set'.Inflector::camelize($key);                
    123.                
    124.                
    125.                 if (method_exists($this, $method)) {
    126.                     $this->$method($config[$key]);
    127.                 } else {
    128.                     $this->$key = $config[$key];
    129.                 }          
    130.             } else {
    131.                 $this->$key = $val;
    132.             }
    133.         }
    134.     }
    135.    
    136.     // --------------------------------------------------------------------
    137.    
    138.     /**
    139.      * Perform the file upload
    140.      *
    141.      * @access  public
    142.      * @return  bool
    143.      */
    144.     function doUpload($field = 'userfile')
    145.     {
    146.         // Is $_FILES[$field] set? If not, no reason to continue.
    147.         if ( ! isset($_FILES[$field])) {
    148.             //log_error('upload_userfile_not_set');
    149.             return false;
    150.         }
    151.        
    152.         // Is the upload path valid?
    153.         if ( ! $this->validateUploadPath()) {
    154.             return false;
    155.         }
    156.                        
    157.         // Was the file able to be uploaded? If not, determine the reason why.
    158.         if ( ! file_exists($_FILES[$field]['tmp_name'])) {
    159.             $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
    160.  
    161.             switch($error) {
    162.                 case 1:
    163.                     $this->setError('upload_file_exceeds_limit');
    164.                     break;
    165.                 case 3:
    166.                     $this->setError('upload_file_partial');
    167.                     break;
    168.                 case 4:
    169.                     $this->setError('upload_no_file_selected');
    170.                     break;
    171.                 default:
    172.                     $this->setError('upload_no_file_selected');
    173.                     break;
    174.             }
    175.  
    176.             return false;
    177.         }
    178.  
    179.         // Set the uploaded data as class variables
    180.         $this->file_temp = $_FILES[$field]['tmp_name'];    
    181.         $this->file_name = $_FILES[$field]['name'];
    182.         $this->file_size = $_FILES[$field]['size'];    
    183.         $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
    184.        
    185.         $this->file_type = strtolower($this->file_type);
    186.         $this->file_ext  = $this->getExtension($_FILES[$field]['name']);
    187.        
    188.         // Convert the file size to kilobytes
    189.         if ($this->file_size > 0) {
    190.             $this->file_size = round($this->file_size/1024, 2);
    191.         }
    192.  
    193.         // Is the file type allowed to be uploaded?
    194.         if ( ! $this->isAllowedFiletype()) {
    195.             $this->setError('upload_invalid_filetype');
    196.             return false;
    197.         }
    198.  
    199.         // Is the file size within the allowed maximum?
    200.         if ( ! $this->isAllowedFilesize()) {
    201.             $this->setError('upload_invalid_filesize');
    202.             return false;
    203.         }
    204.  
    205.         // Are the image dimensions within the allowed size?
    206.         // Note: This can fail if the server has an open_basdir restriction.
    207.         if ( ! $this->isAllowedDimensions()) {
    208.             $this->setError('upload_invalid_dimensions');
    209.             return false;
    210.         }
    211.  
    212.         // Sanitize the file name for security
    213.         $this->file_name = $this->cleanFileName($this->file_name);
    214.  
    215.         // Remove white spaces in the name
    216.         if ($this->remove_spaces == true) {
    217.             $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
    218.         }
    219.  
    220.         /*
    221.          * Validate the file name
    222.          * This function appends an number onto the end of
    223.          * the file if one with the same name already exists.
    224.          * If it returns false there was a problem.
    225.          */
    226.         $this->orig_name = $this->file_name;
    227.  
    228.         if ($this->overwrite == false) {
    229.             $this->file_name = $this->setFilename($this->upload_path, $this->file_name);
    230.            
    231.             if ($this->file_name === false) {
    232.                 return false;
    233.             }
    234.         }
    235.  
    236.         /*
    237.          * Move the file to the final destination
    238.          * To deal with different server configurations
    239.          * we'll attempt to use copy() first.  If that fails
    240.          * we'll use move_uploaded_file().  One of the two should
    241.          * reliably work in most environments
    242.          */
    243.         if ( ! @copy($this->file_temp, $this->upload_path . $this->file_name)) {
    244.             if ( ! @move_uploaded_file($this->file_temp, $this->upload_path . $this->file_name)) {
    245.                  $this->setError('upload_destination_error');
    246.                  return false;
    247.             }
    248.         }
    249.        
    250.         /*
    251.          * Run the file through the XSS hacking filter
    252.          * This helps prevent malicious code from being
    253.          * embedded within a file.  Scripts can easily
    254.          * be disguised as images or other file types.
    255.          */
    256.         if ($this->xss_clean == true)
    257.         {
    258.             // $this->doXssClean();
    259.         }
    260.  
    261.         /*
    262.          * Set the finalized image dimensions
    263.          * This sets the image width/height (assuming the
    264.          * file was an image).  We use this information
    265.          * in the "data" function.
    266.          */
    267.         $this->setImageProperties($this->upload_path . $this->file_name);
    268.  
    269.         return true;
    270.     }
    271.    
    272.     // --------------------------------------------------------------------
    273.    
    274.     /**
    275.      * Finalized Data Array
    276.      *  
    277.      * Returns an associative array containing all of the information
    278.      * related to the upload, allowing the developer easy access in one array.
    279.      *
    280.      * @access  public
    281.      * @return  array
    282.      */
    283.     function data()
    284.     {
    285.         return array (
    286.                         'file_name'         => $this->file_name,
    287.                         'file_type'         => $this->file_type,
    288.                         'file_path'         => $this->upload_path,
    289.                         'full_path'         => $this->upload_path.$this->file_name,
    290.                         'raw_name'          => str_replace($this->file_ext, '', $this->file_name),
    291.                         'orig_name'         => $this->orig_name,
    292.                         'file_ext'          => $this->file_ext,
    293.                         'file_size'         => $this->file_size,
    294.                         'is_image'          => $this->isImage(),
    295.                         'image_width'       => $this->image_width,
    296.                         'image_height'      => $this->image_height,
    297.                         'image_type'        => $this->image_type,
    298.                         'image_size_str'    => $this->image_size_str,
    299.                     );
    300.     }
    301.    
    302.     // --------------------------------------------------------------------
    303.    
    304.     /**
    305.      * Set Upload Path
    306.      *
    307.      * @access  public
    308.      * @param   string
    309.      * @return  void
    310.      */
    311.     function setUploadPath($path)
    312.     {
    313.         $this->upload_path = $path;
    314.     }
    315.    
    316.     // --------------------------------------------------------------------
    317.    
    318.     /**
    319.      * Set the file name
    320.      *
    321.      * This function takes a filename/path as input and looks for the
    322.      * existence of a file with the same name. If found, it will append a
    323.      * number to the end of the filename to avoid overwriting a pre-existing file.
    324.      *
    325.      * @access  public
    326.      * @param   string
    327.      * @param   string
    328.      * @return  string
    329.      */
    330.     function setFilename($path, $filename)
    331.     {
    332.         if ($this->encrypt_name == true) {      
    333.             mt_srand();
    334.             $filename = md5(uniqid(mt_rand())).$this->file_ext;            
    335.         }
    336.    
    337.         if ( ! file_exists($path.$filename)) {
    338.             return $filename;
    339.         }
    340.    
    341.         $filename = str_replace($this->file_ext, '', $filename);
    342.        
    343.         $new_filename = '';
    344.         for ($i = 1; $i < 100; $i++) {          
    345.             if ( ! file_exists($path.$filename.$i.$this->file_ext)) {
    346.                 $new_filename = $filename.$i.$this->file_ext;
    347.                 break;
    348.             }
    349.         }
    350.  
    351.         if ($new_filename == '') {
    352.             $this->setError('upload_bad_filename');
    353.             return false;
    354.         } else {
    355.             return $new_filename;
    356.         }
    357.     }
    358.    
    359.     // --------------------------------------------------------------------
    360.    
    361.     /**
    362.      * Set Maximum File Size
    363.      *
    364.      * @access  public
    365.      * @param   integer
    366.      * @return  void
    367.      */
    368.     function setMaxSize($n)
    369.     {
    370.         $this->max_size = ( ! @eregi("^[[:digit:]]+$", $n)) ? 0 : $n;
    371.     }
    372.    
    373.     // --------------------------------------------------------------------
    374.    
    375.     /**
    376.      * Set Maximum Image Width
    377.      *
    378.      * @access  public
    379.      * @param   integer
    380.      * @return  void
    381.      */
    382.     function setMaxWidth($n)
    383.     {
    384.         $this->max_width = ( ! @eregi("^[[:digit:]]+$", $n)) ? 0 : $n;
    385.     }
    386.    
    387.     // --------------------------------------------------------------------
    388.    
    389.     /**
    390.      * Set Maximum Image Height
    391.      *
    392.      * @access  public
    393.      * @param   integer
    394.      * @return  void
    395.      */
    396.     function setMaxHeight($n)
    397.     {
    398.         $this->max_height = ( ! @eregi("^[[:digit:]]+$", $n)) ? 0 : $n;
    399.     }
    400.    
    401.     // --------------------------------------------------------------------
    402.    
    403.     /**
    404.      * Set Allowed File Types
    405.      *
    406.      * @access  public
    407.      * @param   string
    408.      * @return  void
    409.      */
    410.     function setAllowedTypes($types)
    411.     {
    412.         $this->allowed_types = explode('|', $types);
    413.     }
    414.    
    415.     // --------------------------------------------------------------------
    416.    
    417.     /**
    418.      * Set Image Properties
    419.      *
    420.      * Uses GD to determine the width/height/type of image
    421.      *
    422.      * @access  public
    423.      * @param   string
    424.      * @return  void
    425.      */
    426.     function setImageProperties($path = '')
    427.     {
    428.         if ( ! $this->isImage()) {
    429.             return;
    430.         }
    431.  
    432.         if (function_exists('getimagesize')) {
    433.             if (false !== ($D = @getimagesize($path))) {  
    434.                 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
    435.  
    436.                 $this->image_width      = $D['0'];
    437.                 $this->image_height     = $D['1'];
    438.                 $this->image_type       = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
    439.                 $this->image_size_str   = $D['3'];  // string containing height and width
    440.             }
    441.         }
    442.     }
    443.    
    444.     // --------------------------------------------------------------------
    445.    
    446.     /**
    447.      * Set XSS Clean
    448.      *
    449.      * Enables the XSS flag so that the file that was uploaded
    450.      * will be run through the XSS filter.
    451.      *
    452.      * @access  public
    453.      * @param   bool
    454.      * @return  void
    455.      */
    456.     function setXssClean($flag = false)
    457.     {
    458.         $this->xss_clean = ($flag == true) ? true : false;
    459.     }
    460.    
    461.     // --------------------------------------------------------------------
    462.    
    463.     /**
    464.      * Validate the image
    465.      *
    466.      * @access  public
    467.      * @return  bool
    468.      */
    469.     function isImage()
    470.     {
    471.         $img_mimes = array(
    472.                             'image/gif',
    473.                             'image/jpg',
    474.                             'image/jpe',
    475.                             'image/jpeg',
    476.                             'image/pjpeg',
    477.                             'image/png',
    478.                             'image/x-png'
    479.                            );
    480.  
    481.  
    482.         return (in_array($this->file_type, $img_mimes, true)) ? true : false;
    483.     }
    484.    
    485.     // --------------------------------------------------------------------
    486.    
    487.     /**
    488.      * Verify that the filetype is allowed
    489.      *
    490.      * @access  public
    491.      * @return  bool
    492.      */
    493.     function isAllowedFiletype()
    494.     {
    495.         if (count($this->allowed_types) == 0) {
    496.             $this->setError('upload_no_file_types');
    497.             return false;
    498.         }
    499.                
    500.         foreach ($this->allowed_types as $val) {
    501.             $mime = self::mimesTypes(strtolower($val));
    502.        
    503.             if (is_array($mime)) {
    504.                 if (in_array($this->file_type, $mime, true)) {
    505.                     return true;
    506.                 }
    507.             } else {
    508.                 if ($mime == $this->file_type) {
    509.                     return true;
    510.                 }  
    511.             }      
    512.         }
    513.        
    514.         return false;
    515.     }
    516.    
    517.     // --------------------------------------------------------------------
    518.    
    519.     /**
    520.      * Verify that the file is within the allowed size
    521.      *
    522.      * @access  public
    523.      * @return  bool
    524.      */
    525.     function isAllowedFilesize()
    526.     {
    527.         if ($this->max_size != 0  AND  $this->file_size > $this->max_size) {
    528.             return false;
    529.         } else {
    530.             return true;
    531.         }
    532.     }
    533.    
    534.     // --------------------------------------------------------------------
    535.    
    536.     /**
    537.      * Verify that the image is within the allowed width/height
    538.      *
    539.      * @access  public
    540.      * @return  bool
    541.      */
    542.     function isAllowedDimensions()
    543.     {
    544.         if ( ! $this->isImage()) {
    545.             return true;
    546.         }
    547.  
    548.         if (function_exists('getimagesize')) {
    549.             $D = @getimagesize($this->file_temp);
    550.  
    551.             if ($this->max_width > 0 AND $D['0'] > $this->max_width) {
    552.                 return false;
    553.             }
    554.  
    555.             if ($this->max_height > 0 AND $D['1'] > $this->max_height) {
    556.                 return false;
    557.             }
    558.  
    559.             return true;
    560.         }
    561.  
    562.         return true;
    563.     }
    564.    
    565.     // --------------------------------------------------------------------
    566.    
    567.     /**
    568.      * Validate Upload Path
    569.      *
    570.      * Verifies that it is a valid upload path with proper permissions.
    571.      *
    572.      *
    573.      * @access  public
    574.      * @return  bool
    575.      */
    576.     function validateUploadPath()
    577.     {
    578.         if ($this->upload_path == '') {
    579.             $this->setError('upload_no_filepath');
    580.             return false;
    581.         }
    582.        
    583.         if (function_exists('realpath') AND @realpath($this->upload_path) !== false) {
    584.             $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
    585.         }
    586.  
    587.         if ( ! @is_dir($this->upload_path)) {
    588.             $this->setError('upload_no_filepath');
    589.             return false;
    590.         }
    591.  
    592.         if ( ! is_writable($this->upload_path)) {
    593.             $this->setError('upload_not_writable');
    594.             return false;
    595.         }
    596.  
    597.         $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/",  $this->upload_path);
    598.         return true;
    599.     }
    600.    
    601.     // --------------------------------------------------------------------
    602.    
    603.     /**
    604.      * Extract the file extension
    605.      *
    606.      * @access  public
    607.      * @param   string
    608.      * @return  string
    609.      */
    610.     function getExtension($filename)
    611.     {
    612.         $x = explode('.', $filename);
    613.         return '.'.end($x);
    614.     }  
    615.    
    616.     // --------------------------------------------------------------------
    617.    
    618.     /**
    619.      * Clean the file name for security
    620.      *
    621.      * @access  public
    622.      * @param   string
    623.      * @return  string
    624.      */    
    625.     function cleanFileName($filename)
    626.     {
    627.         $bad = array(
    628.                         "<!--",
    629.                         "-->",
    630.                         "'",
    631.                         "<",
    632.                         ">",
    633.                         '"',
    634.                         '&',
    635.                         '$',
    636.                         '=',
    637.                         ';',
    638.                         '?',
    639.                         '/',
    640.                         "%20",
    641.                         "%22",
    642.                         "%3c",      // <
    643.                         "%253c",    // <
    644.                         "%3e",      // >
    645.                         "%0e",      // >
    646.                         "%28",      // (
    647.                         "%29",      // )
    648.                         "%2528",    // (
    649.                         "%26",      // &
    650.                         "%24",      // $
    651.                         "%3f",      // ?
    652.                         "%3b",      // ;
    653.                         "%3d"       // =
    654.                     );
    655.                    
    656.         foreach ($bad as $val) {
    657.             $filename = str_replace($val, '', $filename);
    658.         }
    659.  
    660.         return $filename;
    661.     }
    662.    
    663.     // --------------------------------------------------------------------
    664.    
    665.     /**
    666.      * Runs the file through the XSS clean function
    667.      *
    668.      * This prevents people from embedding malicious code in their files.
    669.      * I'm not sure that it won't negatively affect certain files in unexpected ways,
    670.      * but so far I haven't found that it causes trouble.
    671.      *
    672.      * @access  public
    673.      * @return  void
    674.      */
    675.     function doXssClean_old()
    676.     {      
    677.        
    678.         // this function is not modifyed
    679.        
    680.         $file = $this->upload_path.$this->file_name;
    681.        
    682.         if (filesize($file) == 0) {
    683.             return false;
    684.         }
    685.    
    686.         if ( ! $fp = @fopen($file, 'rb')) {
    687.             return false;
    688.         }
    689.            
    690.         flock($fp, LOCK_EX);
    691.  
    692.         $data = fread($fp, filesize($file));
    693.        
    694.         $CI =& get_instance();  
    695.         $data = $CI->input->xss_clean($data);
    696.  
    697.         fwrite($fp, $data);
    698.         flock($fp, LOCK_UN);
    699.         fclose($fp);
    700.     }
    701.    
    702.     // --------------------------------------------------------------------
    703.    
    704.     /**
    705.      * Set an error message
    706.      *
    707.      * @access  public
    708.      * @param   string
    709.      * @return  void
    710.      */
    711.     function setError($msg)
    712.     {
    713.         if (is_array($msg))
    714.         {
    715.             foreach ($msg as $val)
    716.             {
    717.                 $this->error_msg[] = self::msg($msg);
    718.                 //log_error($msg);
    719.             }      
    720.         }
    721.         else
    722.         {
    723.             $this->error_msg[] =  self::msg($msg);
    724.             //log_error($msg);
    725.         }
    726.     }
    727.    
    728.     // --------------------------------------------------------------------
    729.    
    730.     /**
    731.      * Display the error message
    732.      *
    733.      * @access  public
    734.      * @param   string
    735.      * @param   string
    736.      * @return  string
    737.      */
    738.     function displayErrors($open = '<p>', $close = '</p>')
    739.     {
    740.         $str = '';
    741.         foreach ($this->error_msg as $val) {
    742.             $str .= $open.$val.$close;
    743.         }
    744.    
    745.         return $str;
    746.     }
    747.    
    748.     // --------------------------------------------------------------------
    749.    
    750.     /**
    751.      * List of Mime Types
    752.      *
    753.      * This is a list of mime types.  We use it to validate
    754.      * the "allowed types" set by the developer
    755.      *
    756.      * @access  public
    757.      * @param   string
    758.      * @return  string
    759.      */
    760.     static public function mimesTypes($mime)
    761.     {
    762.         /*
    763.         | -------------------------------------------------------------------
    764.         | MIME TYPES
    765.         | -------------------------------------------------------------------
    766.         | This file contains an array of mime types.  It is used by the
    767.         | Upload class to help identify allowed file types.
    768.         |
    769.         */
    770.        
    771.         self::$mimes = array(   'hqx'   =>  'application/mac-binhex40',
    772.                         'cpt'   =>  'application/mac-compactpro',
    773.                         'csv'   =>  array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
    774.                         'bin'   =>  'application/macbinary',
    775.                         'dms'   =>  'application/octet-stream',
    776.                         'lha'   =>  'application/octet-stream',
    777.                         'lzh'   =>  'application/octet-stream',
    778.                         'exe'   =>  'application/octet-stream',
    779.                         'class' =>  'application/octet-stream',
    780.                         'psd'   =>  'application/x-photoshop',
    781.                         'so'    =>  'application/octet-stream',
    782.                         'sea'   =>  'application/octet-stream',
    783.                         'dll'   =>  'application/octet-stream',
    784.                         'oda'   =>  'application/oda',
    785.                         'pdf'   =>  array('application/pdf', 'application/x-download'),
    786.                         'ai'    =>  'application/postscript',
    787.                         'eps'   =>  'application/postscript',
    788.                         'ps'    =>  'application/postscript',
    789.                         'smi'   =>  'application/smil',
    790.                         'smil'  =>  'application/smil',
    791.                         'mif'   =>  'application/vnd.mif',
    792.                         'xls'   =>  array('application/excel', 'application/vnd.ms-excel'),
    793.                         'ppt'   =>  'application/powerpoint',
    794.                         'wbxml' =>  'application/wbxml',
    795.                         'wmlc'  =>  'application/wmlc',
    796.                         'dcr'   =>  'application/x-director',
    797.                         'dir'   =>  'application/x-director',
    798.                         'dxr'   =>  'application/x-director',
    799.                         'dvi'   =>  'application/x-dvi',
    800.                         'gtar'  =>  'application/x-gtar',
    801.                         'gz'    =>  'application/x-gzip',
    802.                         'php'   =>  'application/x-httpd-php',
    803.                         'php4'  =>  'application/x-httpd-php',
    804.                         'php3'  =>  'application/x-httpd-php',
    805.                         'phtml' =>  'application/x-httpd-php',
    806.                         'phps'  =>  'application/x-httpd-php-source',
    807.                         'js'    =>  'application/x-javascript',
    808.                         'swf'   =>  'application/x-shockwave-flash',
    809.                         'sit'   =>  'application/x-stuffit',
    810.                         'tar'   =>  'application/x-tar',
    811.                         'tgz'   =>  'application/x-tar',
    812.                         'xhtml' =>  'application/xhtml+xml',
    813.                         'xht'   =>  'application/xhtml+xml',
    814.                         'zip'   => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
    815.                         'mid'   =>  'audio/midi',
    816.                         'midi'  =>  'audio/midi',
    817.                         'mpga'  =>  'audio/mpeg',
    818.                         'mp2'   =>  'audio/mpeg',
    819.                         'mp3'   =>  'audio/mpeg',
    820.                         'aif'   =>  'audio/x-aiff',
    821.                         'aiff'  =>  'audio/x-aiff',
    822.                         'aifc'  =>  'audio/x-aiff',
    823.                         'ram'   =>  'audio/x-pn-realaudio',
    824.                         'rm'    =>  'audio/x-pn-realaudio',
    825.                         'rpm'   =>  'audio/x-pn-realaudio-plugin',
    826.                         'ra'    =>  'audio/x-realaudio',
    827.                         'rv'    =>  'video/vnd.rn-realvideo',
    828.                         'wav'   =>  'audio/x-wav',
    829.                         'bmp'   =>  'image/bmp',
    830.                         'gif'   =>  'image/gif',
    831.                         'jpeg'  =>  array('image/jpeg', 'image/pjpeg'),
    832.                         'jpg'   =>  array('image/jpeg', 'image/pjpeg'),
    833.                         'jpe'   =>  array('image/jpeg', 'image/pjpeg'),
    834.                         'png'   =>  array('image/png',  'image/x-png'),
    835.                         'tiff'  =>  'image/tiff',
    836.                         'tif'   =>  'image/tiff',
    837.                         'css'   =>  'text/css',
    838.                         'html'  =>  'text/html',
    839.                         'htm'   =>  'text/html',
    840.                         'shtml' =>  'text/html',
    841.                         'txt'   =>  'text/plain',
    842.                         'text'  =>  'text/plain',
    843.                         'log'   =>  array('text/plain', 'text/x-log'),
    844.                         'rtx'   =>  'text/richtext',
    845.                         'rtf'   =>  'text/rtf',
    846.                         'xml'   =>  'text/xml',
    847.                         'xsl'   =>  'text/xml',
    848.                         'mpeg'  =>  'video/mpeg',
    849.                         'mpg'   =>  'video/mpeg',
    850.                         'mpe'   =>  'video/mpeg',
    851.                         'qt'    =>  'video/quicktime',
    852.                         'mov'   =>  'video/quicktime',
    853.                         'avi'   =>  'video/x-msvideo',
    854.                         'movie' =>  'video/x-sgi-movie',
    855.                         'doc'   =>  'application/msword',
    856.                         'word'  =>  array('application/msword', 'application/octet-stream'),
    857.                         'xl'    =>  'application/excel',
    858.                         'eml'   =>  'message/rfc822'
    859.                     );
    860.        
    861.    
    862.         return ( ! isset(self::$mimes[$mime])) ? false : self::$mimes[$mime];
    863.     }
    864.  
    865. } // End Upload Class
    866.  
    Как я понял что здесь все настройки
    PHP:
    1. class Upload {
    2.    
    3.     public $max_size       = 0;
    4.     public $max_width      = 0;
    5.     public $max_height     = 0;
    6.     public $allowed_types  = "";
    7.     public $file_temp      = "";
    8.     public $file_name      = "";
    9.     public $orig_name      = "";
    10.     public $file_type      = "";
    11.     public $file_size      = "";
    12.     public $file_ext       = "";
    13.     public $upload_path    = "";
    14.     public $overwrite      = false;
    15.     public $encrypt_name   = false;
    16.     public $is_image       = false;
    17.     public $image_width    = '';
    18.     public $image_height   = '';
    19.     public $image_type     = '';
    20.     public $image_size_str = '';
    21.     public $error_msg      = array();
    22.     public $remove_spaces  = true;
    23.     public $xss_clean      = false;
    24.     public $temp_prefix    = "temp_file_";
    25.  
    26.     static private $mimes;
    Пробовал писать что то вроде public $file_ext = "gif|jpg|png";
    но никаких изменений. как грузил только .jpg, так и грузит :(
    Помогите разобраться
     
  2. igordata

    igordata Суперстар
    Команда форума Модератор

    С нами с:
    18 мар 2010
    Сообщения:
    32.408
    Симпатии:
    1.768
    ты при создании класса должен ему сообщить о том, что ты намерен принимать

    например:
    PHP:
    1.  
    2. <?
    3. $upload = new Upload();
    4. $upload->setAllowedTypes('jpg|jpeg|gif|png|bmp');
    А ты показываешь только сам класс, прототип, который ВОБЩЕ не содержит никаких разрешений. По дефолту он должен выдать ошибку, что в не обнаружил разрешенных типов для загрузки. Раз jpg он все-таки грузит, значит где-то есть такая строка, как я показал. Ищи, правь. или покажи весь код тут.

    но если уж он тебя до слез доведет, то правь 96 строку тогда. Но это не правильно.
     
  3. viphost

    viphost Активный пользователь

    С нами с:
    23 мар 2009
    Сообщения:
    307
    Симпатии:
    0
    Это весть код и есть )

    Ошибку пишет при загрузки .png формата

    пробовал 96 строку редактировать 'allowed_types' => "gif|jpg|png",
    тоже ошибку выдает.
    Далее менял
    строка 412 $this->allowed_types = explode('|', $types); менял на $this->allowed_types = explode('gif|jpg|png', $types);

    и ничего не происходит, пишет тоже самое (