dongshao2967 2017-02-08 17:00
浏览 67
已采纳

在codeigniter中上传图像时,图像不会保存到路径文件夹中

When I upload an image in Codeigniter its name is saved to the database but not in the default folder. I'am using Ajax to submit the image

My "view" profile

  <div id="timelineBackground">
<?php 

{
    $image_properties = array('src' => base_url("uploads/" . $timeline_image),'width' => '1000px','height'=> '400px','id'=>'coverimg', 'title' => 'That was quite a night','rel'   => 'lightbox');
    //echo img($image_properties);

   ?>
<div id="timelineselector">

<?php echo form_open_multipart('',["id"=>"form_cover"]); ?>
<input type="hidden" name="email" value="<?php echo $email ;?>" >

<?php echo form_upload(["name"=>"timelineimage"]); ?>

<?php echo form_submit(["name"=>"submit","value"=>"Submit"]); ?>

<?php echo form_close(); ?>

</div><?php
}
?></div>

here is my ajax code

jQuery('#form_cover').submit(function(e){

        e.preventDefault();
        var formData = new FormData(this);
        var url= '<?php echo base_url("user/coverimage"); ?>';
        formData.value
        jQuery.ajax({

            type: "POST",
            url:url,
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data)
            {

              $('#coverimg').attr('src',data);
            },
            error: function(data){
            //error function
            }
       });            
    });

my controller "user"

public function coverimage(){

  $config = [
      'upload_path' =>    './uploads/',
      'allowed_types' =>    'jpg|gif|png|jpeg',
      'max_size'            => 10000000000000,
      'max_width'            => 1024000000,
      'max_height'           => 7680000000,
                 ];

    $this->load->library('upload', $config);

  if(!$this->upload->do_upload('timelineimage')  ) {
     $post = $this->input->post();
      unset($post['submit']);
       $upload_data = $this->upload->data();
     $file_name=$_FILES['timelineimage'];


      $this->load->model('Pmodel');
      $this->Pmodel->timeline_upload_model($post,$file_name);


        $image_properties = array(
        'src'   => base_url("./uploads/" . $file_name['name']),

        'width' => '200px',
        'height'=> '200px',
        'title' => 'That was quite a night',
        'rel'   => 'lightbox'
);

       // echo img($image_properties);
echo base_url("uploads/" . $file_name['name']);
     exit();
    }else {

      $upload_error = $this->upload->display_errors();
      $this->load->view('admin/add_article',compact('upload_error'));
    }

}

my model "Pmodel"

    public function timeline_upload_model($arr,$arra)
{

    $email=$arr['email'];
    $image=$arra['name'];

$data=array('timelineimage'=>$image);

 $query=$this->db->where('email',$email)->update('user_data',$data);

    return $query;



}

Why does it only show the name but not the image?

  • 写回答

1条回答 默认 最新

  • dongzhanlu8890 2017-02-09 16:23
    关注

    I found the answer and posted the code, in case anyone else wants to look at this...

    the 'view'

     <div id="timelineBackground">
    <?php 
    
    {
    
        $image_properties = array('src' => base_url("uploads/" . $timeline_image),'width' => '1000px','height'=> '400px','id'=>'coverimg', 'title' => 'That was quite a night','rel'   => 'lightbox');
     echo img($image_properties);
    
       ?>
    <div id="timelineselector">
    
    <?php echo form_open_multipart('user/coverimage',["id"=>"form_cover"]); ?>
    <input type="hidden" name="id" value="<?php echo $id ;?>" >
    
    <?php echo form_upload(["name"=>"timelineimage"]); ?>
    
    <?php echo form_submit(["name"=>"submit","value"=>"Submit"]); ?>
    
    <?php echo form_close(); ?>
    
    </div><?php
    }
    ?></div>
    
    'ajax'
    
        jQuery('#form_cover').submit(function(e){
    
            e.preventDefault();
            var formData = new FormData(this);
            var url= '<?php echo base_url("user/coverimage"); ?>';
            formData.value
            jQuery.ajax({
    
                type: "POST",
                url:url,
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function(data)
                {
    
                  $('#coverimg').attr('src',data);
                },
                error: function(data){
                //error function
                }
           });            
        });
    

    and the controller:

         public function coverimage()
      {
        print_r($_POST);
        print_r($_FILES);
    
          $config = [
                      'upload_path' =>    './uploads/',
                      'allowed_types' =>    'jpg|gif|png|jpeg',
                      'max_size'            => 10000000000000,
                      'max_width'            => 1024000000,
                      'max_height'           => 7680000000,
                    ];
                  $this->load->library('upload', $config);
                  $this->upload->initialize($config); 
                  $timelineimage="timelineimage";
            if(!$this->upload->do_upload($timelineimage)) 
             {
              $upload_error = $this->upload->display_errors();
                $this->load->view('dashboard/profile',compact('upload_error'));
    
    
              }
              else 
              {
    
                $post = $this->input->post();
              unset($post['submit']);
              //print_r($post);
    
              $upload_data = $this->upload->data();
              //print_r($upload_data);
    
              $file_name=$_FILES['timelineimage'];
              $this->load->model('Pmodel');
              $this->Pmodel->timeline_upload_model($post,$file_name);
    
    
             $image_path= base_url("uploads/".$upload_data['raw_name'].$upload_data['file_ext']);
    
    //echo base_url("uploads/" . $file_name['name']);
    
              }
    
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?