如何在codeigniter中更新pdf文件和图像文件

时间:2016-08-22 12:56:11

标签: php codeigniter

我想在一个do_upload

上传PDF文件和图片文件

我想使用codeigniter在两个不同的目录中上传两个不同的文件。我在我的模型中编写了以下代码。但它只会上传第一张图片。

        if($_POST){
        if($_FILES['productimage']['name']){
        $img = $_FILES['productimage']['name']; 

        $config['upload_path'] = './uploads/products/';
        $config['allowed_types'] = 'png|jpg|gif|bmp';
        $config['overwrite'] = TRUE;

        $this->load->library('upload',$config);
        if(!$this->upload->do_upload('productimage'))
        {
           $errors = array('error' => $this->upload->display_errors());
           $img="";

        }
        else
        {
            $data =$this->upload->data();
            $img=$data['file_name'];
        //print_r($img);die;
        }
        }else{
            $img=$this->input->post('image_old');
        }
  if($_FILES['productpdf']['name']){
    $img = $_FILES['productpdf']['name']; 

  $config['upload_path'] = './uploads/products/';
  $config['allowed_types'] = 'png|jpg|gif|bmp|pdf';
  $config['overwrite'] = TRUE;

  $this->load->library('upload',$config);
  if(!$this->upload->do_upload('productpdf'))
  {
     $errors = array('error' => $this->upload->display_errors());
     $pdf="";

  }
  else
  {
    $data =$this->upload->data();
    $pdf=$data['file_name'];

  }
  }else{
    $pdf=$this->input->post('pdf_old');

  }
//  print_r($img);print_r($pdf);die;
         $title = $this->input->post('productname');
         $content = $this->input->post('description');            
         $status = $this->input->post('status');
         $this->db->where('product_id', $id);
         $this->db->update('products',array('product_name'=>$title,'product_content'=>$content,'product_image'=>$img,'product_file'=>$pdf,'product_status'=>$status));



    $this->db->where('product_id',$id);
    $this->db->delete('products_filter');

    $filters= $_POST['filter'];      
  foreach ($filters as $value) 
  {
$this->db->insert('products_filter',array('product_id' => $id,'products_search_id'=>$value));
  }
 return ($this->db->affected_rows() != 1)? false:true;

 }else{
            redirect(base_url('admin/products/product-list/'.$redirectid));
     }

}

1 个答案:

答案 0 :(得分:0)

请检查我的代码    此代码用于处理不同目录中的pdf或图像文件上载

<?php

     if($_POST){

        if($_FILES['productimage']['name'])
        {

        $custom_file_name = $_FILES['productimage']['name']; 


        $file_ext = end(explode('.', $custom_file_name));

        if($file_ext=='pdf')
        {
            $upload_path_directory='./uploads/products/pdf/';
            $file_field_name="productimage";

        }
        else{
            $upload_path_directory='./uploads/products/images/';
             $file_field_name="productpdf";
        }

        $config['upload_path'] = $upload_path_directory;
        $config['allowed_types'] = 'png|jpg|gif|bmp|pdf';
        $config['overwrite'] = TRUE;

          $this->load->library('upload',$config);
        if(!$this->upload->do_upload("$file_field_name"))
        {
           $errors = array('error' => $this->upload->display_errors());
           $custom_file_name="";

        }
        else
        {
            $data =$this->upload->data();
            $custom_file_name=$data['file_name'];

        }


    ?>
  • 首先要确保上传库库和表单验证,如果不知道libaray将在下面提到的位置*

    $这 - &GT;负载&GT; libaray(&#34;上传&#34);     $这 - &GT;负载&GT; libaray(&#34; form_validation&#34);

    在条件

  • 中添加此代码