Heim  >  Artikel  >  Backend-Entwicklung  >  CodeIgniter类库实现图片上传的例子

CodeIgniter类库实现图片上传的例子

WBOY
WBOYOriginal
2016-07-25 08:53:23960Durchsuche
  • 复制代码

    2,文件上传类:

    1. public function img_upload()

    2. {
    3. $this->load->helper('url');
    4. $config['upload_path'] = './images/'.date('Ym', time()).'/';

    5. $config['allowed_types'] = 'gif|jpg|png';
    6. $config['file_name'] = date('Y_m_d', time()).'_'.sprintf('%02d', rand(0,99));
    7. $config['max_size'] = '500';
    8. $config['max_width'] = '1024';
    9. $config['max_height'] = '768';
    10. $this->load->library('upload', $config);

    11. if ( !$this->upload->do_upload())

    12. {
    13. $error = array('error' => $this->upload->display_errors());
    14. }
    15. else
    16. {
    17. $data = array('upload_data' => $this->upload->data());
    18. }
    19. }
    复制代码

    需要用到的函数: $this->upload->do_upload():根据偏好配置参数执行操作。注意:默认情况下上传的文件来自于提交表单里名为userfile的文件域,并且该表单必须是 "multipart"类型。 $this->upload->display_errors():如果do_upload()返回失败,显示错误信息。此函数不会自动输出,而是返回数据,所以你可以按你的要求安排。 $this->upload->data():辅助函数,它返回上传文件的所有相关信息的数组。



    Stellungnahme:
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn