doujiao3072 2018-03-28 09:41
浏览 38
已采纳

为什么无法从多个字段上传多个文件?

I have one form. I am trying to upload 3 files from 3 different input fields in Codeigniter. 3 fields are: Insurance, Permit and Registration. Path from the field save accurately in database, but only 1 file going to folder.These 3 have 3 different folders. Registration file uploaded in Insurance folder. Permit and Registration is not uploading.

Here is the controller code for uploading.

<?php
if (!empty($_FILES['insurance']['name'])) {
    $path = $_FILES['insurance']['name'];
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    $rand = 'ins_' . rand(1, 500) . rand(500, 1000) . rand(1, 500);
    $config['upload_path'] = BASEPATH . "../images/insurance/";
    $config['allowed_types'] = '*';
    $config['file_name'] = $rand;
    $this->load->library('upload', $config);
    $this->upload->overwrite = true;
    $this->upload->do_upload('insurance');
    $data = $this->upload->data();
    $_POST['insurance'] = $this->config->base_url() . "images/insurance/" . $rand . '.' . $ext;
} else{
    echo "Error in uploading Insurance Photo";
}

if (!empty($_FILES['permit']['name'])) {
    $path = $_FILES['permit']['name'];
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    $rand = 'pmt_' . rand(1, 500) . rand(500, 1000) . rand(1, 500);
    $config['upload_path'] = BASEPATH . "../images/permit/";
    $config['allowed_types'] = '*';
    $config['file_name'] = $rand;
    $this->load->library('upload', $config);
    $this->upload->overwrite = true;
    $this->upload->do_upload('permit');
    $data = $this->upload->data();
    $_POST['permit'] = $this->config->base_url() . "images/permit/" . $rand . '.' . $ext;
} else{
    echo "Error in uploading Permit Photo";
}

if (!empty($_FILES['registration']['name'])) {
    $path = $_FILES['registration']['name'];
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    $rand = 'reg_' . rand(1, 500) . rand(500, 1000) . rand(1, 500);
    $config['upload_path'] = BASEPATH . "../images/registration/";
    $config['allowed_types'] = '*';
    $config['file_name'] = $rand;
    $this->load->library('upload', $config);
    $this->upload->overwrite = true;
    $this->upload->do_upload('registration');
    $data = $this->upload->data();
    $_POST['registration'] = $this->config->base_url() . "images/registration/" . $rand . '.' . $ext;
} else{
    echo "Error in uploading Registration Photo";
}
?>

Here is the view

<div class="form-group">
    <div class="col-md-12">
        <label class="control-label col-md-4">Insurance Document</label>
        <div id="email-input-wrapper"  class="controls col-sm-6 col-md-8">
            <input class="edit-file" placeholder="Choose Insurance File" disabled="disabled" />
            <div class="fileUpload btn btn-primary">
                <span>Upload Photo</span>
                <input  id="edituploadBtn" type="file"  name="insurance" class="upload" />
            </div>
            <br/>
            <img id="myImgedit" style="width: 100px;height: 100px" src="../images/insurance/no-image.jpg" alt="your image" />
            <?php if (!empty($post['insurance'])) { ?>
                <div id="image-div">
                    <img id="img" src="<?php echo $post['insurance'] ?>" style="height: 100px;width: 100px"/>
                </div>
            <?php } else {
                ?>
                <img id="myno_img" style="width: 100px;height: 100px" src="../images/insurance/no-image.jpg" alt="your image" />
            <?php }
            ?>
        </div>
    </div>
</div>

<div class="form-group">
    <div class="col-md-12">
        <label class="control-label col-md-4">Permit Document</label>
        <div id="email-input-wrapper"  class="controls col-sm-6 col-md-8">
            <input class="edit-file1" placeholder="Choose Permit File" disabled="disabled" />
            <div class="fileUpload btn btn-primary">
                <span>Upload Photo</span>
                <input  id="edituploadBtn1" type="file"  name="permit" class="upload" />
            </div>
            <br/>
            <img id="myImgedit1" style="width: 100px;height: 100px" src="../images/permit/no-image.jpg" alt="your image" />
            <?php if (!empty($post['permit'])) { ?>
                <div id="image-div1">
                    <img id="img" src="<?php echo $post['permit'] ?>" style="height: 100px;width: 100px"/>
                </div>
            <?php } else {
                ?>
                <img id="myno_img1" style="width: 100px;height: 100px" src="../images/permit/no-image.jpg" alt="your image" />
            <?php }
            ?>
        </div>
    </div>
</div>

<div class="form-group">
    <div class="col-md-12">
        <label class="control-label col-md-4">Registration Document</label>
        <div id="email-input-wrapper"  class="controls col-sm-6 col-md-8">
            <input class="edit-file2" placeholder="Choose Registration File" disabled="disabled" />
            <div class="fileUpload btn btn-primary">
                <span>Upload Photo</span>
                <input  id="edituploadBtn2" type="file"  name="registration" class="upload" />
            </div>
            <br/>
            <img id="myImgedit2" style="width: 100px;height: 100px" src="../images/registration/no-image.jpg" alt="your image" />
            <?php if (!empty($post['registration'])) { ?>
                <div id="image-div2">
                    <img id="img" src="<?php echo $post['registration'] ?>" style="height: 100px;width: 100px"/>
                </div>
            <?php } else {
                ?>
                <img id="myno_img2" style="width: 100px;height: 100px" src="../images/registration/no-image.jpg" alt="your image" />
            <?php }
            ?>
        </div>
    </div>
</div>
  • 写回答

1条回答 默认 最新

  • dow66098 2018-03-28 09:49
    关注

    You need to initialize the upload library with your $config instead of loading it again. You load the upload library only once without any config, and then you initialize it each time with your config.

    $this->load->library('upload');
    // (stuff for first file)
    $this->upload->initialize($config);
    // (stuff for second file)
    $this->upload->initialize($config);
    // (...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧