dongshadu2546 2016-01-22 02:48
浏览 40
已采纳

PHP CodeIgniter - 数组上传文件

I run my php code to upload multiple file but until right now i get error like this : PHP erorr Undefined index: link_to_doc_file_

And here is my controller :

function insert_data(){
    $this->form_validation->set_rules('tool_code', 'Tool Code', 'required');

    if ($this->form_validation->run()){

        for($i=1;$i<=10;$i++) {
        $link_to_doc_file_[$i] = (isset($_GET['link_to_doc_file_'][$i]) ? $_GET['link_to_doc_file_'][$i] : null);
        $link_to_doc_file_[$i] = "";
        $file_uploaded = false;
        //UPLOAD DOC
        if(is_uploaded_file($_FILES['link_to_doc_file_']['tmp_name'][$i])){
            $folder = /*base_url() .*/ 'material/';
            $ext = substr($_FILES['link_to_doc_file_']['name'][$i],strpos($_FILES['link_to_doc_file']['name'][$i],'.'));

            $file_name = $this->input->post('tool_code') . (int)microtime(true) . $ext;

            $target_file = $folder .$file_name;

            if(!file_exists($folder))
                mkdir($folder);

            //if(file_exists($target_file)) unlink($target_file);

            if(move_uploaded_file($_FILES['link_to_doc_file_']["tmp_name"][$i], $target_file)){
                $file_uploaded = true;
                $$link_to_doc_file_[$i] = $target_file;
            }
        }
    }

        $tool_code = $this->input->post($tool_code);


        $data_doc = null;

        //Data Description
        for($i=1;$i<=10;$i++){
            $data_doc['title'][$i-1] = $this->input->post('doc_title_' . $i);
            $data_doc['link'][$i-1] = $this->input->post('link_to_doc_file_'.$i);
            $data_doc['show'][$i-1] = $this->input->post('doc_display_' . $i);

        }


        $data['doc'] = $data_doc;

        if($this->input->post('is_edit')){
            //UPDATE
            $this->model_tool->update_tool_document($this, $data);
        }else{
            //INSERT
            $this->model_tool->insert_tool_document($this, $data);
        }

        redirect(MENU_TOOL_DOC);
    }else{

    }
}
}

Here my View :

<div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <?php echo (($id)?"Edit":"Add") . " Document"; ?>
                </div>
                <div class="panel-body">
                    <?php echo form_open_multipart(base_url() . MENU_TOOL_DOC . '/insert_data');?>
                        <div class="row">
                            <div class="col-lg-6">
                                <div class="form-group">
                                <input type="hidden" name="tool_code" value="<?php echo $tool_code;?>">
                                <label>Tool Code : <?php echo $tool_name;?></label>
                                    <?php 
                                                        for($i=1;$i<=10;$i++){ 
                                                    ?>
                                                    <div class="form-group">
                                                        <label>Document <?php echo $i;?></label>

                                                        <div class="form-group">
                                                            <label>Show</label>
                                                            <select class="form-control" name="doc_show_<?php echo $i;?>">
                                                                        <?php
                                                                    for($j=0;$j<count($show);$j++){
                                                                ?>
                                                                        <option value="<?php echo ($j); ?>" <?php echo (isset($row))?(($doc['show'][$i-1] == $j)?"selected":""):"";?>>
                                                                            <?php echo $show[$j];?>
                                                                        </option>
                                                                <?php
                                                                    }
                                                                ?>
                                                            </select> 
                                                        </div>
                                                        <div class="form-group">    
                                                            <label>Title</label>
                                                            <input class="form-control" name="doc_title_<?php echo $i;?>" value="<?php echo (isset($row))?$doc['title'][$i-1]:"";?>">
                                                        </div>
                                                        <div class="form-group form-inline">    
                                                            <label>File</label>
                                                            <input type="file" name="link_to_doc_file_<?php echo $i;?>" value="<?php echo (isset($row))?$doc['link'][$i-1]:"";?>">
                                                        </div>

                                                    </div>
                                                    <?php
                                                        }
                                                    ?>

                            </div>
                            <div class="col-lg-12">
                                <input type="hidden" name="is_edit" value="<?php echo $is_edit; ?>" />
                                <button type="submit" class="btn btn-primary" name="submit">Submit</button>
                                <button type="reset" class="btn btn-default">Reset</button>
                            </div>
                        </div>
                    <?php echo form_close();?>
                    <!-- /.row (nested) -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->

if only one upload i can but if many uploads file not yet until right now.

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-01-22 06:21
    关注

    After your ist update, you are using an incremental variable in form input name.

    <input type="file" name="link_to_doc_file_<?php echo $i;?>">
    

    Now, you are getting undefined index array because you are defining index as:

    link_to_doc_file_
    

    This should be defined as:

    $_FILES['link_to_doc_file_'.$i]
    

    More explanation:

    link_to_doc_file_ != link_to_doc_file_1
    

    Side Note:

    Also check this variable $_GET['link_to_doc_file_'][$i], you need to change it also.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用