【问题标题】:CodeIgniter Image Upload (Insert blank into array)CodeIgniter 图片上传(将空白插入数组)
【发布时间】:2012-09-06 17:02:26
【问题描述】:

我正在使用的表单包含五个人的信息。可选地,用户可以为五个人中的每个人上传一张照片。我正在尝试将默认图像插入到未上传图像的结果数组 $files 中。非常感谢您的帮助。

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;

    }

}

【问题讨论】:

    标签: php image codeigniter file-upload


    【解决方案1】:

    验证上传的文件后进行检查可能会更容易。

    例如:用五个空元素初始化一个数组,每个图片一个,然后对于每个上传的文件,将其文件名设置为适当的数组元素。完成后,只需运行任何空元素,只需将文件(或文件路径)添加到默认图像即可。

    这样,您可以尽可能地保持文件上传和验证,并单独处理页面的业务逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-04
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 2013-09-18
      相关资源
      最近更新 更多