【问题标题】:CodeIgniter - upload multiple text entries and files from single formCodeIgniter - 从单个表单上传多个文本条目和文件
【发布时间】:2012-08-29 19:50:37
【问题描述】:

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

我是 CodeIgniter 的新手,所以代码 sn-ps 会很有帮助。

非常感谢。

【问题讨论】:

    标签: codeigniter


    【解决方案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();
                }
            }
        }
    }
    

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

    【讨论】:

    • 我遇到了另一个问题。当我将表单条目写入数据库时​​,我可以很好地完成文本,但是我将如何将文件名写入数据库中的相应字段?由于我们正在循环...
    • $data 包含从$this->upload->data() 返回的信息 - 请参阅codeigniter.com/user_guide/libraries/file_uploading.html 的用户指南
    • @Gavin 您好先生,如果用户将一些非图像文件与图像混合并上传该数据怎么办?我尝试了这种情况并得到一些图像正在上传,然后出现错误。有什么解决办法吗?
    • @ankitsuthar 嘿 - 如果我没记错的话,错误很可能归结为 allowed_types 配置。如果您更新了allowed_types 以包含您想要接受的其他类型并上传混合图像,例如word 文档,则只有那些可识别为图像(即图像/gif、图像/png)的文件将根据@987654328 进行验证@ 和 max_height 限制。希望有帮助吗?
    • @Gavin 这是我的问题,先生。希望你能在这方面帮助我。 stackoverflow.com/questions/41630185/…
    【解决方案2】:

    希望这有帮助

    $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
    
            }
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-04-01
      • 2019-10-28
      • 2023-03-04
      • 1970-01-01
      • 2020-02-01
      • 2017-02-20
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多