当我上传多张图片时,我希望能够显示每个文件名。
我可以将文件上传到上传文件夹,但是当我尝试获取多个file_name数据时,它会在下面显示错误。
我在下方收到此错误
严重性:警告消息:非法字符串偏移'file_name'文件名: page / Page_add.php行号:110
这是上传功能的成功部分
$upload_info = $this->upload->data();
foreach ($upload_info as $upload_data) {
echo $upload_data['file_name']; // Line 110
}
上传功能
public function do_upload() {
$directory = FCPATH . 'uploads/';
if (is_dir($directory)) {
foreach ($_FILES as $field_name => $value) {
if ($value['name'] != '') {
$this->load->library('upload');
$this->upload->initialize($this->do_upload_options());
if (!$this->upload->do_upload($field_name)) {
$this->form_validation->set_message('do_upload', $this->upload->display_errors());
return FALSE;
} else {
$upload_info = $this->upload->data();
foreach ($upload_info as $upload_data) {
echo $upload_data['file_name'];
}
}
}
}
} else {
$this->form_validation->set_message('do_upload', 'Cannot Find Directory' .' '. $directory);
return FALSE;
}
}
public function do_upload_options() {
$config = array();
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|png|jpg';
$config['max_size'] = '30000';
$config['overwrite'] = TRUE;
$config['max_width'] = '0';
$config['max_height'] = '0';
return $config;
}
每个字段名称都有自己的名称=“”example name =“fileupload_extra_image0”结尾处的数字是自动生成的
Vardump
array(2) { ["fileupload_extra_image0"]=> array(14) { ["file_name"]=> string(17) "ci_logo_flame.jpg" ["file_type"]=> string(10) "image/jpeg" ["file_path"]=> string(49) "C:/Xampp/htdocs/riwakawebsitedesigns-cms/uploads/" ["full_path"]=> string(66) "C:/Xampp/htdocs/riwakawebsitedesigns-cms/uploads/ci_logo_flame.jpg" ["raw_name"]=> string(13) "ci_logo_flame" ["orig_name"]=> string(17) "ci_logo_flame.jpg" ["client_name"]=> string(17) "ci_logo_flame.jpg" ["file_ext"]=> string(4) ".jpg" ["file_size"]=> float(3.61) ["is_image"]=> bool(true) ["image_width"]=> int(100) ["image_height"]=> int(100) ["image_type"]=> string(4) "jpeg" ["image_size_str"]=> string(24) "width="100" height="100"" } ["fileupload_extra_image1"]=> array(14) { ["file_name"]=> string(10) "family.png" ["file_type"]=> string(9) "image/png" ["file_path"]=> string(49) "C:/Xampp/htdocs/riwakawebsitedesigns-cms/uploads/" ["full_path"]=> string(59) "C:/Xampp/htdocs/riwakawebsitedesigns-cms/uploads/family.png" ["raw_name"]=> string(6) "family" ["orig_name"]=> string(10) "family.png" ["client_name"]=> string(10) "family.png" ["file_ext"]=> string(4) ".png" ["file_size"]=> float(828.1) ["is_image"]=> bool(true) ["image_width"]=> int(670) ["image_height"]=> int(450) ["image_type"]=> string(3) "png" ["image_size_str"]=> string(24) "width="670" height="450"" } }
答案 0 :(得分:1)
public function do_upload()
{
$directory = FCPATH . 'uploads/';
$upload_info = [];//array();
if (is_dir($directory)) {
foreach ($_FILES as $field_name => $value) {
if ($value['name'] != '') {
$this->load->library('upload');
$this->upload->initialize($this->do_upload_options());
if (!$this->upload->do_upload($field_name)) {
$this->form_validation->set_message('do_upload', $this->upload->display_errors());
return FALSE;//You would like to make better control here because first one is not file_name would break process
} else {
$upload_info[$field_name] = $this->upload->data();
}
}
}
if ( count($upload_info) > 0 ) {
foreach ($upload_info as $upload_data) {
echo $upload_data['file_name'];
}
}
} else {
$this->form_validation->set_message('do_upload', 'Cannot Find Directory' .' '. $directory);
return FALSE;
}
}
public function do_upload_options()
{
$config = array();
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|png|jpg';
$config['max_size'] = '30000';
$config['overwrite'] = TRUE;
$config['max_width'] = '0';
$config['max_height'] = '0';
return $config;
}