php - Getting file name after uploading the file in CI

527

I want to store file name in a database and file in a folder when I click on the button. File is successfully uploaded but I didn't get the file name. Here is my view :

<?php echo form_open_multipart('home/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>

Here is my controller :

public function do_upload()
    {

        $this->load->helper(array('form', 'url'));
        $image_path = realpath(APPPATH . '../assets/pics');
        $config['upload_path'] = $image_path; //base_url('assets/pics'); //APPPATH . 'uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
         $upload_data = $this->upload->data(); 
         $file_name =   $upload_data['file_name'];
        if ( ! $this->upload->do_upload())
        {
            echo 'error';
        }
        else
        {
            echo $file_name .' uploaded...';

        }

    }
515

Answer

Solution:

In your success part of upload add variable$file_data = $this-upload->data();

if ( ! $this->upload->do_upload()) {

echo 'error';

} else {

$file_data = $this-upload->data();


echo $file_data['file_name'] .' uploaded...';

}
38

Answer

Solution:

Pass input field name to thedo_upload() method:

if ( ! $this->upload->do_upload("userfile")) {
        echo 'error';
}
else {


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

     //OR pass to view
}

People are also looking for solutions to the problem: PHP form won't submit to $_POST

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.