1

For example if I upload the file foo.png how can I get the string "foo.png" in the upload controller?

The controller code is:

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->database();
    }

    function do_upload($folder)
    {
        $config['upload_path'] = './userdata/'. $folder . '/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

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

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            echo $this->upload->display_errors();
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            echo "<p>File sucesfully uploaded</p>";

            $filename = // How do I get the filename here

        }
    }
}
?>

How can I set $filename to the filename of the uploaded file?

3 Answers 3

4

From the official CI manual:

$this->upload->data()
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:
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 in your case the $data variable that holds the result of the $this->upload->data() function should contain all the info that you need about the file you have uploaded.

And specifically $data['upload_data']['file_name'] is what you are looking for.

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

Comments

2

Try this!

$data = $this->upload->data();
echo $data['file_name'];

Comments

1
echo $data['raw_name'].$data['file_ext'];

should do the trick

e.g. you upload your image

if($this->upload->do_upload('upload_data')) {
$data = $this->upload->data();
echo $data['raw_name'].$data['file_ext'];
}

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.