繁体   English   中英

在codeigniter中上传图片,是否需要userfile?

[英]uploading images in codeigniter, is userfile required?

我正在使用下面的功能将图像文件上传到服务器(Localhost)。 很好,正在上传图片。 但是我需要两个图像字段,因此一旦单击提交按钮,两个图像都应该上传。 我使用了此处介绍的功能Codeigniter多个文件上传 ,但是不起作用。

我收到此错误消息

A PHP Error was encountered

Severity: Warning

Message: is_uploaded_file() expects parameter 1 to be string, array given

Filename: libraries/Upload.php

Line Number: 161

好吧,我不明白错误在哪里。 我用来上传单张图片的功能是

function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'jpeg|png|gif';
        $config['max_size'] = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
           //failed display the errors
        }
        else
        {
            //success

        }
    }

此外,我还想问一下我是否可以更改选择的输入字段名称。 即我总是需要实现<input type="file" name="userfile"/> 可以更改名称吗? 我尝试更改它,然后收到消息未选择文件 ,所以这意味着我无法更改它。

您必须循环浏览上载的文件,就像提供的链接中显示的一样。

function do_upload() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'jpeg|png|gif';
        $config['max_size'] = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';

        $this->load->library('upload', $config);

        foreach ($_FILES as $key => $value) {

            if (!empty($value['tmp_name'])) {

                if ( ! $this->upload->do_upload($key)) {
                    $error = array('error' => $this->upload->display_errors());
                    //failed display the errors
                } else {
                    //success
                }

            }
        }
}

并尝试使用如下HTML:

<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />

您可以根据需要更改输入字段的名称。

如果您正在使用文件多选,请执行以下操作:

HTML(带有数组名称的HTML5文件输入允许选择多个文件):

<input type="file" name="userfile[]"/>

要么

<input type="file" name="userfile[]"/>
<input type="file" name="userfile[]"/>

CodeIgniter中的PHP:

// load upload library (put your own settings there)
$this->load->library('upload', array('allowed_types' => 'svg|gif|jpg|png', 'upload_path' => $GLOBALS['config']['upload_path'], ));

// normalise files array
$input_name = 'userfile'; // change it when needed to match your html
$field_names = array('name', 'type', 'tmp_name', 'error', 'size', );
$keys = array();
foreach($field_names as $field_name){
    if (isset($_FILES[$input_name][$field_name])) foreach($_FILES[$input_name][$field_name] as $key => $value){
        $_FILES[$input_name.'_'.$key][$field_name] = $value;
        $keys[$key] = $key;
    }
}
unset($_FILES[$input_name]); // just in case

foreach ($keys as $key){

    $new_file = $this->upload->do_upload($input_name.'_'.$key);

    // do your stuff with each uploaded file here, delete for example:
    $upload_data = $this->upload->data();
    unlink($upload_data['file_name']);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM