2015-12-13 31 views
1

我想在基于codeigniter的多次上传中插入批处理。 这是由于:插入批处理中的非法字符串偏移codeigniter

echo "<pre>"; 
print_r($this->upload->data()); 

阵:

Array 
(
[file_name] => EOLU_111111115450530.jpg 
[file_type] => image/jpeg 
[file_path] => C:/xampp/htdocs/depo/assets/uploads/EOLU 11111111545053/ 
[full_path] => C:/xampp/htdocs/depo/assets/uploads/EOLU 11111111545053/EOLU_111111115450530.jpg 
[raw_name] => EOLU_111111115450530 
[orig_name] => EOLU_111111115450530.jpg 
[client_name] => Archana_Sastry_bollywood_celebrity_actress_model_girl_beautiful_indian_brunette_pretty_cute_beauty_face_lips_eyes_hair_sexy_pose_smile_2560x1600.jpg 
[file_ext] => .jpg 
[file_size] => 201.36 
[is_image] => 1 
[image_width] => 2560 
[image_height] => 1600 
[image_type] => jpeg 
[image_size_str] => width="2560" height="1600" 
) 

Array 
(
[file_name] => EOLU_111111115450531.jpg 
[file_type] => image/jpeg 
[file_path] => C:/xampp/htdocs/depo/assets/uploads/EOLU 11111111545053/ 
[full_path] => C:/xampp/htdocs/depo/assets/uploads/EOLU 11111111545053/EOLU_111111115450531.jpg 
[raw_name] => EOLU_111111115450531 
[orig_name] => EOLU_111111115450531.jpg 
[client_name] => art_lara_croft_girl_spray_background_suit_women_females_girls_sexy_babes_1920x1200.jpg 
[file_ext] => .jpg 
[file_size] => 306.69 
[is_image] => 1 
[image_width] => 1920 
[image_height] => 1200 
[image_type] => jpeg 
[image_size_str] => width="1920" height="1200" 
) 

这是模式

function save_files_info($files) { 
    //start db traction 
    $this->db->trans_start(); 
    //file data 
    $file_data = array(); 
    foreach ($files as $file) { 
     $file_data[] = array(
      "file_name" => $file['file_name'], 
      "file_orig_name" => $file['orig_name'], 
      "file_path" => $file['full_path'] 
     ); 
    }; 
    //insert file data 
    $this->db->insert_batch($this->file, $file_data); 
    //complete the transaction 
    $this->db->trans_complete(); 
    //check transaction status 
    if ($this->db->trans_status() === FALSE) { 
     foreach ($files as $file) { 
      $file_path = $file['full_path']; 
      //delete the file from destination 
      if (file_exists($file_path)) { 
       unlink($file_path); 
      } 
     } 
     //rollback transaction 
     $this->db->trans_rollback(); 
     return FALSE; 
    } else { 
     //commit the transaction 
     $this->db->trans_commit(); 
     return TRUE; 
    } 
} 

我得到这个错误:

Severity: Warning 

Message: Illegal string offset 'file_name' 

Filename: models/m_surveyor.php 

Line Number: 141 

这是我的表格:

mysql> desc tb_files; 
+----------------+------------------+------+-----+-------------------+----------------+ 
| Field   | Type    | Null | Key | Default   | Extra   | 
+----------------+------------------+------+-----+-------------------+----------------+ 
| file_id  | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| file_name  | varchar(255)  | NO |  | NULL    |    | 
| file_orig_name | varchar(255)  | NO |  | NULL    |    | 
| file_path  | varchar(255)  | NO |  | NULL    |    | 
| upload_date | timestamp  | NO |  | CURRENT_TIMESTAMP |    | 
+----------------+------------------+------+-----+-------------------+----------------+ 

什么是:非法字符串偏移量?我已尝试Illegal string offset Warning PHP。但我仍然坚持。任何帮助,如此赞赏。

更新 这是我的控制器:

public function add_file_image($idx) { 
    $last = $this->uri->total_segments(); 
    $id = rawurldecode($this->uri->segment($last)); 
    $pathToUpload = './assets/uploads/' . $id; 

    if (!is_dir($pathToUpload)) { 
     mkdir($pathToUpload, 0755, true); 
    } 
    $dir_exist = true; // flag for checking the directory exist or not 
    if (!is_dir($pathToUpload)) { 
     mkdir($pathToUpload, 0755, true); 
     mkdir($pathToUpload . '/thumbs', 0755, true); 
     $dir_exist = false; // dir not exist 
    } 

    if (!empty($_FILES)) { 
     $config['upload_path'] = $pathToUpload; 
     $config['allowed_types'] = 'gif|jpg|png|'; 
     $config['file_name'] = $id; 
     $config['overwrite'] = true; 
     //$config['encrypt_name'] = TRUE; 
     $this->load->library('upload'); 

     $files = $_FILES; 
     $number_of_files = count($_FILES['file']['name']); 
     $errors = 0; 


     // codeigniter upload just support one fileto upload. so we need a litte trick 
     for ($i = 0; $i < $number_of_files; $i++) { 
      $_FILES['file']['name'] = $files['file']['name'][$i]; 
      $_FILES['file']['type'] = $files['file']['type'][$i]; 
      $_FILES['file']['tmp_name'] = $files['file']['tmp_name'][$i]; 
      $_FILES['file']['error'] = $files['file']['error'][$i]; 
      $_FILES['file']['size'] = $files['file']['size'][$i]; 
      // we have to initialize before upload 

      $config['file_name'] = str_replace("_", " ", $id) . $i; 

      // Execute upload 
      $this->upload->initialize($config); 

      if (!$this->upload->do_upload("file")) { 
       $errors++; 
       echo $this->upload->display_errors(); 
      } else { 
       echo "<pre>"; 
       print_r($this->upload->data()); 

       //Insert to database 
       $this->m_surveyor->save_files_info($this->upload->data()); 
      } 
     } 
     // Lakukan insert dengan menggunakan insert_batch. 
     if ($errors > 0) { 
      echo $errors . "File(s) cannot be uploaded"; 
     } 
    } elseif ($this->input->post('file_to_remove')) { 
     $file_to_remove = $this->input->post('file_to_remove'); 
     unlink("./assets/uploads/" . $file_to_remove); 
    } else { 
     $this->listFiles(); 
    } 
} 
+0

请说明如何从控制器调用方法save_files_info,以及上传处理代码。 – Vaviloff

回答

0

的警告表示数组值为空。在尝试将其分配给其他变量之前,检查与该键相关的值。

这是检查设置值的一种方法。

foreach ($files as $file) { 
    $file_data[] = array(
     "file_name" => isset($file['file_name']) ? $file['file_name']: "", 
     "file_orig_name" => isset($file['orig_name']) ? $file['orig_name']: "", 
     "file_path" => isset($file['full_path']) ? $file['full_path'] : "");