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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?