用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5182|回复: 8
收起左侧

[讨论/交流] 上传图片问题

[复制链接]
发表于 2009-8-22 23:36:14 | 显示全部楼层 |阅读模式
为什么我上传图片的时候,图片是上传上去了,可图片的后缀名却都没了,添加了图片的后缀名上传的图片可以显示
发表于 2009-8-22 23:46:48 | 显示全部楼层
很难理解楼主的文字表述
 楼主| 发表于 2009-8-22 23:58:17 | 显示全部楼层
我的意思是上传上去的图片是这样的,图片的后缀名没了
QQ截图未命名.png
发表于 2009-8-22 23:59:39 | 显示全部楼层
用CI的图片上传类可以获得文件全名,详见手册
 楼主| 发表于 2009-8-23 00:05:10 | 显示全部楼层
输出的是这个

file_name: 830371
file_type: image/jpeg
file_path: C:/wamp/www/c2c/kcy/
full_path: C:/wamp/www/c2c/kcy/830371
raw_name: 830371
orig_name: k2.jpg
file_ext: .jpg
file_size: 64.69
is_image: 1
image_width: 390
image_height: 225
image_type: jpeg
image_size_str: width="390" height="225"
 楼主| 发表于 2009-8-23 00:06:10 | 显示全部楼层
发表于 2009-8-23 00:55:45 | 显示全部楼层
贴代码看看
 楼主| 发表于 2009-8-23 02:31:28 | 显示全部楼层
/*控制器的*/
class Rz extends MY_Controller
{

        public $layout = 'rz';
       
function rz()
{
  parent::Controller();
  $this->load->helper(array('form', 'url'));
}

function index()
{
  $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload()
{

  $config['upload_path'] = './kcy/';
  $config['allowed_types'] = 'gif|jpg|png|JPG';
  $config['max_size'] = '100000';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';
  
  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload())
  {
   $error = array('error' => $this->upload->display_errors());
   
   $this->load->view('upload_form', $error);
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   
   $this->load->view('upload_success', $data);
  }
}




}
 楼主| 发表于 2009-8-23 02:33:09 | 显示全部楼层
Upload.php
function do_upload($field = 'userfile')
        {
                // Is $_FILES[$field] set? If not, no reason to continue.
                if ( ! isset($_FILES[$field]))
                {
                        $this->set_error('upload_no_file_selected');
                        return FALSE;
                }
               
                // Is the upload path valid?
                if ( ! $this->validate_upload_path())
                {
                        // errors will already be set by validate_upload_path() so just return FALSE
                        return FALSE;
                }
                                               
                // Was the file able to be uploaded? If not, determine the reason why.
                if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
                {
                        $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];

                        switch($error)
                        {
                                case 1:        // UPLOAD_ERR_INI_SIZE
                                        $this->set_error('upload_file_exceeds_limit');
                                        break;
                                case 2: // UPLOAD_ERR_FORM_SIZE
                                        $this->set_error('upload_file_exceeds_form_limit');
                                        break;
                                case 3: // UPLOAD_ERR_PARTIAL
                                   $this->set_error('upload_file_partial');
                                        break;
                                case 4: // UPLOAD_ERR_NO_FILE
                                   $this->set_error('upload_no_file_selected');
                                        break;
                                case 6: // UPLOAD_ERR_NO_TMP_DIR
                                        $this->set_error('upload_no_temp_directory');
                                        break;
                                case 7: // UPLOAD_ERR_CANT_WRITE
                                        $this->set_error('upload_unable_to_write_file');
                                        break;
                                case 8: // UPLOAD_ERR_EXTENSION
                                        $this->set_error('upload_stopped_by_extension');
                                        break;
                                default :   $this->set_error('upload_no_file_selected');
                                        break;
                        }

                        return FALSE;
                }

                // Set the uploaded data as class variables
                $this->file_temp = $_FILES[$field]['tmp_name'];               
                $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
                $this->file_size = $_FILES[$field]['size'];               
                $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
                $this->file_type = strtolower($this->file_type);
                $this->file_ext         = $this->get_extension($_FILES[$field]['name']);
                if( $this->file_name == "" )        
                        $this->file_name = $_FILES[$field]['name'];
                else
                        $this->file_name .= $this->file_ext;

               
                // Convert the file size to kilobytes
                if ($this->file_size > 0)
                {
                        $this->file_size = round($this->file_size/1024, 2);
                }

                // Is the file type allowed to be uploaded?
                if ( ! $this->is_allowed_filetype())
                {
                        $this->set_error('upload_invalid_filetype');
                        return FALSE;
                }

                // Is the file size within the allowed maximum?
                if ( ! $this->is_allowed_filesize())
                {
                        $this->set_error('upload_invalid_filesize');
                        return FALSE;
                }

                // Are the image dimensions within the allowed size?
                // Note: This can fail if the server has an open_basdir restriction.
                if ( ! $this->is_allowed_dimensions())
                {
                        $this->set_error('upload_invalid_dimensions');
                        return FALSE;
                }

                // Sanitize the file name for security
                $this->file_name = $this->clean_file_name($this->file_name);

                // Remove white spaces in the name
                if ($this->remove_spaces == TRUE)
                {
                        $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
                }

                /*
                 * Validate the file name
                 * This function appends an number onto the end of
                 * the file if one with the same name already exists.
                 * If it returns false there was a problem.
                 */
                $this->orig_name = $this->file_name;

                if ($this->overwrite == FALSE)
                {
                        $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
                       
                        if ($this->file_name === FALSE)
                        {
                                return FALSE;
                        }
                }

                /*
                 * Move the file to the final destination
                 * To deal with different server configurations
                 * we'll attempt to use copy() first.  If that fails
                 * we'll use move_uploaded_file().  One of the two should
                 * reliably work in most environments
                 */
                if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
                {
                        if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
                        {
                                 $this->set_error('upload_destination_error');
                                 return FALSE;
                        }
                }
               
                /*
                 * Run the file through the XSS hacking filter
                 * This helps prevent malicious code from being
                 * embedded within a file.  Scripts can easily
                 * be disguised as images or other file types.
                 */
                if ($this->xss_clean == TRUE)
                {
                        $this->do_xss_clean();
                }

                /*
                 * Set the finalized image dimensions
                 * This sets the image width/height (assuming the
                 * file was an image).  We use this information
                 * in the "data" function.
                 */
                $this->set_image_properties($this->upload_path.$this->file_name);

                return TRUE;
        }
       
        // --------------------------------------------------------------------
       
        /**
         * Finalized Data Array
         *       
         * Returns an associative array containing all of the information
         * related to the upload, allowing the developer easy access in one array.
         *
         * @access        public
         * @return        array
         */       
        function data()
        {
                return array (
                                                'file_name'                        => $this->file_name,
                                                'file_type'                        => $this->file_type,
                                                'file_path'                        => $this->upload_path,
                                                'full_path'                        => $this->upload_path.$this->file_name,
                                                'raw_name'                        => str_replace($this->file_ext, '', $this->file_name),
                                                'orig_name'                        => $this->orig_name,
                                                'file_ext'                        => $this->file_ext,
                                                'file_size'                        => $this->file_size,
                                                'is_image'                        => $this->is_image(),
                                                'image_width'                => $this->image_width,
                                                'image_height'                => $this->image_height,
                                                'image_type'                => $this->image_type,
                                                'image_size_str'        => $this->image_size_str,
                                        );
        }

本版积分规则