1

I am developing an website in php where user can upload csv file. after clicking 'upload' button, file is renamed and saved in the format 'upload'.time(). I need to show the contents of the uploaded file just after uploading the file. So far I have been able to upload file correctly and I have tested with a static filename and have been able to read csv file perfectly. but when I am trying to get recently uploaded file name just after uploading I am not getting anywhere. searched through the whole site but didn't get any solution that worked for me. I am using codeigniter 2.1.0 .

Controller file:

<?php     
class Csv_ci extends CI_Controller {

function is_logged() {
    $is_logged_in = $this->session->userdata('is_logged_in');
    if (!isset($is_logged_in) || $is_logged_in != true)
        return false;
    else
        return true;
}

function index() {
    if ($this->is_logged() == TRUE) {
        $this->load->model('Csv_model');

        if ($this->input->post('upload')) {
            $name = $this->Csv_model->do_upload();
            //print_r($name);
            if (isset($name)) {
                $filePath = "localhost/map_ci/csv/" . $name;
                //$filePath = './csv/bank_data.csv';
                $row = 1;
                if (($handle = fopen($filePath, "r")) !== FALSE) {
                    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
                        $num = count($data);

                        $file_data[$row]['district_name'] = $data[1];
                        $file_data[$row]['bank_name'] = $data[2];
                        $file_data[$row]['area_name'] = $data[3];
                        $file_data[$row]['address'] = $data[4];
                        $row++;
                    }

                    fclose($handle);
                }
                $this->load->view('csvshow', array('file_data' => $file_data));
            }
        }
    }
    else $this->load->view('invalid_member');
     }
}

Model file:

 <?php

class Csv_model extends CI_Model {

var $gallery_path;
var $gallery_path_url;

function Csv_model() {
    parent::__construct();


    $this->gallery_path = realpath(APPPATH . '../csv');
    $this->gallery_path_url = base_url() . 'csv/';
}

function do_upload() {
    $config = array(
        'upload_path' => $this->gallery_path,
        'allowed_types' => 'text/csv|csv|text/x-comma-separated-values|text/comma-separated-values|application/x-csv|text/x-csv|text/csv|application/csv|',
        'max_size' => '5000',
        'file_name' => 'upload' . time()
    );


    $this->load->library('upload', $config);

    if (!$this->upload->do_upload())
        echo $this->upload->display_errors();
    else {
        $file_info = $this->upload->data();
        $csvfilepath = "csv/" . $file_info['file_name'];
        $this->addfromcsv($csvfilepath);
        $filename = $file_info['file_name'];
        // print_r($filename);
        return $filename;
    }
} 
}

to retrieve file name I am doing file_info['file_name']. Please help me find a solution.

Update: My current problem has been solved after removing $csvfilepath = "csv/" . $file_info['file_name']; $this->addfromcsv($csvfilepath); these two lines from my code.Now its returning uploaded file name.yet to check whether fully working or not.

Update 2: code functioning well thanks all for your help.

2
  • are you sure $name contains something? If I'm not mistaken you need to make use of the address of the file in the file system instead of localhost/map_ci/csv/ it should be something like d:/folder/filename.jpg Commented Jul 7, 2012 at 7:09
  • I tried printing whatever in $filename but nothing was dumped so $name is not returning anything. first I need the name and then I can correct the filepath properly. any idea on how to fix it? Commented Jul 7, 2012 at 9:05

4 Answers 4

2

You can return the concatenation of $config['upload_path'] & $config['file_name'] from do_upload() and use it to read the corresponding file.

Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is in $this->upload->data()

try returning $config['file_name'] from do_upload()

Comments

0
$data = $this->upload->data();
print_r($data);

returns:

Array
(
[file_name]    => mypic.jpg
[file_type]    => image/jpeg
[file_path]    => /path/to/your/upload/
[full_path]    => /path/to/your/upload/jpg.jpg
[raw_name]     => mypic
[orig_name]    => mypic.jpg
[client_name]  => mypic.jpg
[file_ext]     => .jpg
[file_size]    => 22.2
[is_image]     => 1
[image_width]  => 800
[image_height] => 600
[image_type]   => jpeg
[image_size_str] => width="800" height="200"
)`

so: $file_name = $data['file_name'];

-or-

$file = $data['file_path'].$data['file_name'];

1 Comment

thanks. even though I tried doing same as your suggestion first $file_info['file_name'] ,it didn't work , but now its working when I removed $csvfilepath = "csv/" . $file_info['file_name']; $this->addfromcsv($csvfilepath);
0

just use:

$this->upload->file_name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.