2016-08-22 1 views
2

Я хочу, чтобы загрузить файл в формате PDF, а также файл изображения с на один do_uploadкак обновить PDF-файл и файл изображения в codeignitor

Я хочу, чтобы загрузить два файла в два разных каталога с помощью 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)); 
    } 

}

ответ

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, укажите ниже *

    $ this-> load-> libaray ("upload"); $ this-> load-> libaray ("form_validation");

    добавить этот код, если условие

Смежные вопросы