Take the path before saving CI image

Asked

Viewed 474 times

-1

I am using the following method to save an image, is working properly:

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $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());

        $this->load->view('upload_form', $error);
    }   
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

However I would like before saving the image, pass its route to a variable, but it is giving error that I must convert there, I am new in CI if you can help me.

I tried to make:

$teste = $this->upload->data();
echo $teste;

If I make one foreach will show all the paths I would need is to show only that specific.

  • The "route" would be the URL for the saved image?

2 answers

-1

You will use:

$test = $this->upload->data('file_path'); // Returns the absolute upload path, assigned in $config['file_path'].

$test = $this->upload->data('full_path'); //Returns the absolute path of the file with the file name + extension.

-1

$this->upload->data() is a ASSOCIATIVE ARRAY. To assign the "path" value of the file to a variable we need to specify which of the elements of the ARRAY we want to use, which, in its case, must be 'file_path':

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

If you want a URL to the file you will have to create it, because as far as I read in the documentation, $this->upload->data() doesn’t return that.

This ought to work:

$teste = $this->upload->data();
$file_url = base_url("diretorio_upload/{$teste['file_name']}");
echo $file_url;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.