dongzong3053 2015-07-27 08:17
浏览 28
已采纳

在codeigniter中选择文件名后显示图像

Controller:

public function do_register()
{
    $path = $_FILES['image']['name'];
    $imgext=strtolower(strrchr($path,'.'));
    $imgname= $this->generateRandomString().$imgext;
    if($path!='')
    {
        $im= $this->config->item('base_url').'/uploads'.'/'.$imgname;
        $x=$this->do_upload($imgname);
        $data['img']=$im;
    }

    $this->search_model->register_user($data);
    $this->load->view('register_view');  
}

function generateRandomString()
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $randomString = '';
    for ($i = 0; $i < 8; $i++)
    {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

function do_upload($img)
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '575000';
    $config['file_name'] = $img;
    $this->load->library('upload',$config);
    if ( ! $this->upload->do_upload('image'))
    {
        $error = array('error' => $this->upload->display_errors());
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        return $data;
    }
    return;
}

This is the controller for image upload.When i select the image foe uploading the image is not displayed while i selecting the filename? what is the solution for this? How to display the image after selecting the filename? Please provide a code for this?

  • 写回答

2条回答 默认 最新

  • dongqian7545 2015-07-27 11:00
    关注

    For Image preview before uploading check this FIDDLE.

    Code for limiting your upload size.

      $(document).on('change','.coverimage',function(){
          files = this.files;
          size = files[0].size;
          //max size 50kb => 50*1000
          if( size > 1000141){
             alert('Please upload less than 1mb file');
             return false;
          }
          return true;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form role="form" method="post" enctype="multipart/form-data">
       <div class="form-group">
          <label for="file">Choose your Image: </label>
          <input type="file"  class="coverimage" id="file1" name="file[]" multiple>
       </div>
       <button id='submit_btn' class="btn btn-default">upload</button>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)