CodeIgniter文件上传 - 如何保存文件名和路径?

时间:2017-09-13 10:16:59

标签: php codeigniter file file-upload upload

我需要保存选定的文件名和路径以重复使用它。 当我上传文件时,我的代码将检查内部数据是否正确。数据将实施到数据库中并与帐户建立连接。如果帐户不存在,但有类似的帐户,我有一个表格,可以选择正确的帐户。点击该帐户,该文件应该实施到该帐户。 问题是我无法重定向到上传功能,因为我的文件路径和文件名丢失了。

所以我需要保存这个,但我不知道如何。

导入视图:

<div class="h-ctr">
        <?php    
       echo form_open_multipart('interview/import_data');
       echo form_upload('file');
       echo '<br/>';
       ?>
    </div>
    <br>
    <div class="h-ctr">
        <?php
            echo form_submit(null, 'Upload');
            echo form_close();

        ?>
    </div>

我的import_data控制器(领口):

 $config['upload_path'] = FCPATH. 'uploads/';
    $config['allowed_types'] = 'xlsx';
    $this->load->library('upload', $config);

    if($this->upload->do_upload('file')) {
        $data = $this->upload->data();
        chmod($data['full_path'], 0777);
    }
    else{
        if($this->upload->data('file_ext') != ".xlsx"){
            $type = $this->upload->data('file_ext');
            if(!empty($type)){
                $error = '<b>Incorrect filetype: "'. $type.'"!</b>';
            }
            else{
                $error = '<b>No file selected!</b>';
            }
        }
        $this->notification->set("Error!", "Interview could not be imported. Reason: $error");
        redirect('interview/import');
    }   

    $this->load->library('excel');

我对类似客户的看法:

<div class="h-ctr">

<ul class="content-list"> 
    <?php foreach($similaraccount as $simacc){ ?>
    <a class="content-list-item" href="<?php echo site_url("interview/import_data/"); ?>" >
        <li class="media">
            <div class="media-body">

                <div class="media-heading"><?php echo $simacc->name; ?></div>
                <div class="media-hint"><?php echo $simacc->region; ?></div>

            </div>
        </li>
    </a>
    <?php } ?>

1 个答案:

答案 0 :(得分:0)

试试这个

控制器替换为您的代码

$file_name = time() . "." . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'xlsx|XLSX|xls|XLS';
$config['max_size'] = '2048000';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$config['file_name'] = $file_name;
$config['overwrite'] = true;

$obj->upload->initialize($config);
if (!$obj->upload->do_upload('file')) {

  $this->session->set_falshdata("error", $this->upload->display_errors());
  redirect('interview/import');
  //return $obj->upload->display_errors();
} else {
  $data = $this->upload->data();
  //here you can write code to save file name as well as path of the file 
  // $data will give you the file name by which file got saved n file `path` 
  // checked try echo "<pre>";print_r($data);die;
}
$this->load->library('excel');

您可以根据需要使用$file_name,但扩展名应与我使用的相同 此$obj->upload->do_upload('file')中的file是您的html file tag名称