Php Codeigniter 2.1-上载时图像名称工作不正常
我正在使用这个功能上传图像,除了一个部分外,它正在工作。如果要上载多个图像,则所有图像都从第一个图像获取其名称(overwrite设置为off,因此CI将在名称末尾添加数字)。我怎样才能解决这个问题Php Codeigniter 2.1-上载时图像名称工作不正常,php,codeigniter,codeigniter-2,Php,Codeigniter,Codeigniter 2,我正在使用这个功能上传图像,除了一个部分外,它正在工作。如果要上载多个图像,则所有图像都从第一个图像获取其名称(overwrite设置为off,因此CI将在名称末尾添加数字)。我怎样才能解决这个问题 function img_upload($folder) { $this->path = './public/img/' . $folder; $imgs = array(); $count = 0; foreach($_FILES as $key =>
function img_upload($folder) {
$this->path = './public/img/' . $folder;
$imgs = array();
$count = 0;
foreach($_FILES as $key => $value):
$img_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
$img_name = $this->char_replace($img_name, '_');
$count++;
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->path,
'file_name' => $img_name
);
$this->CI->load->library('image_lib');
$this->CI->image_lib->clear();
$this->CI->load->library('upload', $config);
if($key != 'logo'):
if (!$this->CI->upload->do_upload($key)) {
} else {
$image = $this->CI->upload->data();
$imgs[] = $image['file_name'];
}
endif;
endforeach;
if(empty($imgs)):
return FALSE;
else:
return implode(',', $imgs);
endif;
}
功能char\u replace正常工作
function char_replace($text, $rep_simbol = " ")
{
$char = array('!', '&', '?', '/', '/\/', ':', ';', '#', '<', '>', '=', '^', '@', '~', '`', '[', ']', '{', '}');
return $name = str_replace($char, $rep_simbol, $text);
}
函数char\u replace($text,$rep\u simbol=”“)
{
$char=array(“!”、“&”、“?”、“/”、“/\/”、“:”、“;”、“\”、“=”、“^'、“@'、“~”、“?”、“[”、“]'、“{”、“}”);
返回$name=str\u replace($char,$rep\u simbol,$text);
}
$this->CI->upload->do\u upload($key)
希望$\u文件['key']
只包含一个文件
您可以做的是,复制$\u文件
,在其中循环,并为每个文件设置$\u文件['key']
的值
function img_upload($folder) {
$this->path = './public/img/' . $folder;
$imgs = array();
// Copy of $_FILES
$thisFiles = $_FILES;
// Loop through copy of $_FILES
foreach($theFiles as $key => &$value){
// Create the $_FILES array for each individual file,
// so that do_upload can read it correctly
if(!is_array($value['name'])){
// If it's not an array, make it one,
// this will make our future code easier
foreach($value as $kv => &$val){
$val = array($val);
}
}
// Loop through each file and upload each one
foreach($value['name'] as $count=>$img_name){
$img_name = $this->char_replace($img_name, '_');
foreach($_FILES[$key] as $k => &$v){
// CodeIgniter will think this is the $_FILES array
$v = $theFiles[$key][$k][$count];
}
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->path,
'file_name' => $img_name
);
$this->CI->load->library('image_lib');
$this->CI->image_lib->clear();
$this->CI->load->library('upload', $config);
if($key != 'logo'){
if (!$this->CI->upload->do_upload($key)) {
}
else {
$image = $this->CI->upload->data();
$imgs[] = $image['file_name'];
}
}
}
}
return !empty($imgs) ? implode(',', $imgs) : FALSE;
}
注意:这是未经测试的。请不要使用
if():endif使用if(){}
语法编写代码>语法@我没看到。我改变了这个函数,一些旧代码保留了下来。感谢您的深入了解。您的$count
变量仅在foreach
每次运行时递增。您希望内部循环在多个文件上循环。这是因为$This->CI->upload->do_upload
的工作方式。它希望$\u文件['key']
只有一个文件,而不是一个文件数组。对于我自己的程序,我也有同样的问题,我使用了一个愚蠢的变通方法来解决它。我备份了真实的$\u文件
数组,在副本中循环,然后每次都重新设置$\u文件
数组,这样$this->CI->upload->do\u upload
就可以将其作为一个文件读取。你能告诉我你的方式代码吗?它需要一些调整,但我明白了一般的想法。谢谢你的帮助:)就像我说的,这是未经测试的。这基本上就是你需要做的,因为$this->CI->upload->do\u upload($key)
假设$\u文件['key']
只有一个文件。