2017-09-13 222 views
0

我需要保存选定的文件名和路径以重新使用它。 当我上传一个文件时,我的代码会检查里面的数据是否正确。数据在数据库中得到实施并与账户相关联。如果账户不存在但有类似的账户,我有一个表格可以选择正确的账户。点击账户后,该文件应该被实施到这个账户。 问题是我无法重定向到上传功能,因为我的文件路径和文件名已丢失。CodeIgniter文件上传 - 如何保存文件名和路径?

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

进口查看:

<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的Controler(颈线):

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

回答

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,只要你喜欢,你可以使用,但就像我在这个file使用 $obj->upload->do_upload('file')扩展应该是相同的是你的名字html file tag

相关问题