简体   繁体   中英

CodeIgniter File upload - How to save file name and path?

I need to save the selected file name and path to reuse it. When I upload a file, my code will check whether the data inside is correct. The data get implemented into the database and gets connected with an Account. If the Account don't exists but there are similar accounts, I have a table where the right account can be selected. With clicking onto the account the file should be implemented to this account. The problem is that I can't redirect to the upload function because , my filepath and filename has lost.

So I need to save this but I dont know how.

Import View:

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

My import_data Controler (neckline):

 $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');

My view with the similar customers:

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

Try This

Controller Replace with your code

$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');

Where $file_name you can use as you like but extension should be same like i used $obj->upload->do_upload('file') in this file is your html file tag name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM