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条回答 默认 最新

  • doudu35999489610 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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongya6997 2016-01-22 03:34

    You're looking for $FILES['link_to_doc_file'] but you're setting it with an integer and the end:

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

    in your upload script you have to look for that same integer

    if(is_uploaded_file($_FILES['link_to_doc_file_$some_integer']['tmp_name'][$i])){
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题