0

我在一个页面上有一个表单。它有 5 个文本字段和 3 个上传文件字段。我需要将文本和文件路径写入数据库。我在网上看过很多例子,但大多数是上传单个文件或从同一个上传字段上传多个文件。

我是 CodeIgniter 的新手,所以代码片段会很有帮助。

提前谢谢了。

4

2 回答 2

1

希望这可以帮助

$config['upload_path'] = $path; //$path=any path you want to save the file to...
$config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
$config['max_size'] = '1024'; //max file size

$config['max_width']  = '1024';//if file type is image
$config['max_height']  = '768';//if file type is image
//etc config for file properties, you can check all of them out on website

现在假设你有 3 个文件,你想保存为1.jpg, 2.jpg,3.gif,它们是通过 3 个输入字段上传的,pic1, pic2, pic3这就是你要做的

for($ite=1;$ite<=3;$ite++){
    if(!empty($_FILES["pic".$ite]["name"])){ //if file is present
        $ext = pathinfo($_FILES['pic'.$ite]['name'], PATHINFO_EXTENSION); //get extension of file
        $config["file_name"]="$ite.$ext"; //rename file to 1.jpg,2.jpg or 3.jpg, depending on file number and its extension

        $this->upload->initialize($config); //upload library of codeigniter initialize function with config properties set earlier
        if(!$this->upload->do_upload("pic".$ite)){
            //error code

        }

        }
    }
于 2012-08-29T01:20:58.597 回答
1

另一个建议是:

function upload()
{
    $config['upload_path'] = $path; //$path=any path you want to save the file to...
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
    $config['max_size'] = '1024'; //max file size

    $config['max_width']  = '1024';//if file type is image
    $config['max_height']  = '768';//if file type is image

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

    foreach($_FILES as $Key => $File)
    {
        if($File['size'] > 0)
        {
            if($this->upload->do_upload($Key))
            {
                $data = $this->upload->data();
                echo $data['file_name'];
            }
            else
            {
                // throw error
                echo $this->upload->display_errors();
            }
        }
    }
}

这将自动适用于您发布的所有文件输入,它不关心名称或数量:)

于 2012-08-29T10:06:29.237 回答