Php CodeIgniter图像上传(将空白插入阵列)
我正在使用的表单包含五个人的信息。或者,用户可以为五个人中的每一个人上传一张照片。我正在尝试将默认图像插入到未上载图像的结果数组$files中。非常感谢您的帮助Php CodeIgniter图像上传(将空白插入阵列),php,image,codeigniter,file-upload,Php,Image,Codeigniter,File Upload,我正在使用的表单包含五个人的信息。或者,用户可以为五个人中的每一个人上传一张照片。我正在尝试将默认图像插入到未上载图像的结果数组$files中。非常感谢您的帮助 function multiple_upload($upload_dir = APPPATH, $config = array()) { $CI =& get_instance(); $files = array(); if(empty($config)) { $config['
function multiple_upload($upload_dir = APPPATH, $config = array())
{
$CI =& get_instance();
$files = array();
if(empty($config))
{
$config['upload_path'] = realpath($upload_dir . '/uploads');
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
$config['max_size'] = '2048';
}
$CI->load->library('upload', $config);
$errors = FALSE;
$this->load->library('image_lib'); // moved outside loop so it'll work
foreach($_FILES as $key => $value)
{
if( ! empty($value['name']))
{
if( ! $CI->upload->do_upload($key))
{
$data['upload_message'] = $CI->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
$CI->load->vars($data);
$errors = TRUE;
}
else
{
// resize the saved image
$path = $CI->upload->data('full_path');
//echo $path['full_path'] . "<Br/>";
$config = array(
'source_image' => $path['full_path'],
'new_image' => $upload_dir . 'uploads/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 150
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
// Build a file array from all uploaded files
$files[] = $CI->upload->data();
}
}
}
// There was errors, we have to delete the uploaded files
if($errors)
{
foreach($files as $key => $file)
{
@unlink($file['full_path']);
}
}
elseif(empty($files) AND empty($data['upload_message']))
{
$CI->lang->load('upload');
$data['upload_message'] = ERR_OPEN.$CI->lang->line('upload_no_file_selected').ERR_CLOSE;
$CI->load->vars($data);
}
else
{
return $files;
}
}
函数多重上传($upload\u dir=APPPATH,$config=array())
{
$CI=&get_instance();
$files=array();
if(空($config))
{
$config['upload_path']=realpath($upload_dir.'/uploads');
$config['allowed_types']='gif | jpg | jpeg | jpe | png';
$config['max_size']='2048';
}
$CI->load->library('upload',$config);
$errors=FALSE;
$this->load->library('image_lib');//已移动到循环外部,因此它可以工作
foreach($\文件为$key=>$value)
{
如果(!empty($value['name']))
{
如果(!$CI->上传->上传($key))
{
$data['upload\u message']=$CI->upload->display\u errors(ERR\u OPEN,ERR\u CLOSE);//ERR\u OPEN和ERR\u CLOSE是配置文件中定义的错误分隔符
$CI->load->vars($data);
$errors=TRUE;
}
其他的
{
//调整已保存图像的大小
$path=$CI->upload->data('full_path');
//echo$path['full_path']。“”; $config=array( 'source_image'=>$path['full_path'], “新建图像”=>$upload\U dir。“上传/拇指”, “保持定量”=>正确, “宽度”=>150, “高度”=>150 ); $this->image\u lib->initialize($config); $this->image_lib->resize(); //从所有上载的文件构建文件数组 $files[]=$CI->upload->data(); } } } //有错误,我们必须删除上传的文件 如果($errors) { foreach($key=>$file形式的文件) { @取消链接($file['full_path']); } } elseif(空($files)和空($data['upload_message'])) { $CI->lang->load('upload'); $data['upload\u message']=ERR\u OPEN.$CI->lang->line('upload\u no\u file\u selected')。ERR\u CLOSE; $CI->load->vars($data); } 其他的 { 返回$files; } }
在验证上传的文件后,进行检查可能会更容易 例如:使用五个空元素初始化一个数组,每个图片一个,然后对于上载的每个文件,将其文件名设置为相应的数组元素。完成后,只需运行所有空元素,只需将文件(或文件路径)固定到默认图像 这样,您可以尽可能地保持文件上传和验证,并单独处理页面的业务逻辑