【问题标题】:CodeIgniter File upload - How to save file name and path?CodeIgniter 文件上传 - 如何保存文件名和路径?
【发布时间】:2018-02-22 00:09:59
【问题描述】:

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

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

导入视图:

<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 } ?>

【问题讨论】:

    标签: php codeigniter file file-upload upload


    【解决方案1】:

    试试这个

    控制器替换为您的代码

    $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-&gt;upload-&gt;do_upload('file') 在这个file 是你的html file tag 名字

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 1970-01-01
      • 2010-12-31
      • 2011-04-08
      • 1970-01-01
      • 2016-11-30
      • 2012-04-22
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多