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 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)