这是文件上传的基本功能。 我需要它返回一个包含所有图像名称的字符串,如果没有要上传的图像返回FALSE。我怎么能这样做?
function img_upload($folder) {
$this->path = './public/img/' . $folder;
$imgs = array();
$g = true;
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->path
);
$CI = &get_instance();
$CI->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
if (!$CI->upload->do_upload($key)) {
return FALSE;
} else {
$q = $CI->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $this->path . '/' . $q['file_name'];
$config['new_image'] = $this->path . '/thumbs';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 128;
$config['height'] = 128;
$CI->load->library('image_lib', $config);
$CI->image_lib->resize();
echo $q['file_name'];
$imgs = array_push($imgs, $q['file_name']);
}
}
}
答案 0 :(得分:1)
如果我理解你的正确,那么这段代码可能对你有帮助..
function img_upload($folder) {
$this->path = './public/img/' . $folder;
$imgs = array();
$g = true;
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->path
);
$CI = &get_instance();
$CI->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
if (!$CI->upload->do_upload($key)) {
return FALSE;
} else {
$q = $CI->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $this->path . '/' . $q['file_name'];
$allimages = array();
$allimages = $q['file_name'];
$config['new_image'] = $this->path . '/thumbs';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 128;
$config['height'] = 128;
$CI->load->library('image_lib', $config);
$CI->image_lib->resize();
echo $q['file_name'];
$imgs = array_push($imgs, $q['file_name']);
if(count($allimages) > 0)
{
return $allimagename = ('',$allimages);
}
else
{
return 'Thre is no images';
}
}
}
}