Welcome Guest, Not a member yet? Register   Sign In
Printing the web path of the file just uploaded
#1

[eluser]heretic[/eluser]
Hi everyone,

I already managed to upload files using this guide: http://ellislab.com/codeigniter/user-gui...ading.html
You can find a demo at http://www.dustskill.com/index.php/upload/
What i am now trying to do is to make it show the web adress of the uploaded file on upload_success.php It already shows information about the uploaded file there
. For example, when the file name.txt is uploaded to /uploads, the actual file path would be /home/dustskill/uploads/name.txt. Resulting of this, the web adress would be dustskill.com/uploads/name.txt

Any Suggestions? Thanks a lot in advance!
#2

[eluser]ciGR[/eluser]
Hi and welcome!
You can use the
Code:
$data = $this->upload->data();
and after you can get for example the
Code:
$data['full_path'] => /path/to/your/upload/image_name.jpg
or
$data['file_name'] => image_name.jpg
if you get the file name, you can access it for example

Code:
$upload_dir = './uploads/';
$addr = base_url() . $upload_dir . $data['file_name'];

check the http://ellislab.com/codeigniter/user-gui...ading.html, in the $this->upload->data() section
#3

[eluser]heretic[/eluser]
Thanks a lot for the fast reply!
I am not really experienced in PHP (to be honest, im a bloody beginner). Could you tell me how to implement this code?
Thanks a lot!
#4

[eluser]ciGR[/eluser]
In your controller, after the successful image upload.
read the http://ellislab.com/codeigniter/user-gui...ading.html will help you!
#5

[eluser]heretic[/eluser]
[quote author="ciGR" date="1269723607"]In your controller, after the successful image upload.
read the http://ellislab.com/codeigniter/user-gui...ading.html will help you![/quote]

i have read the guide, i said so in my first post.
Could you give me step-by-step instructions? Looks like I still cant get it to work.
#6

[eluser]ciGR[/eluser]
Give some code of your controller to say you.
#7

[eluser]heretic[/eluser]
It is basically the same code than in the guide except for a little hack i made to allow any filetype to be uploaded.
Heres the code

Code:
<?php

class Upload extends Controller {
    
    function Upload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    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);
        }
    }    
}


?>
#8

[eluser]ciGR[/eluser]
Code:
<?php

class Upload extends Controller {
    
    /**
     * @var String $upload_path
     * the dir to upload the files
     * I believe its good practice to have in global scope in your class
     * for example you can use it, in a other method to show the image or
     * create a download link for the file
     */
     var $upload_path = "./uploads/";
    
    
    function Upload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = $this->upload_path;
    //    $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 // This is the section when we have success to the file upload process
        {
            $file_info =  $this->upload->data();
            
                /*
                 $file_info is an array for example like this
                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
                    [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"
                )*/
                            
            //we use the file_name
            $file_name = $file_info['file_name'];
            
            //create the web address of the file just uploaded
            $web_addr_of_file = base_url() . $this->upload_path . $file_name;
            
            //next you can pass it in your view
            $data['web_addr'] = $web_addr_of_file;
            $this->load->view('upload_success', $data);
        }
    }    
}

?>
#9

[eluser]heretic[/eluser]
this does completely make sense to me but i still cant get it to work. you can have a look at the rest of code following my link in the first post.
#10

[eluser]ciGR[/eluser]
[quote author="heretic" date="1269791099"]this does completely make sense to me but i still cant get it to work. you can have a look at the rest of code following my link in the first post.[/quote]

As I can see the erros I get are about don't pass the variables to your view.
for example:
Quote:Message: Undefined variable: upload_data
Filename: views/upload_success.php
means that you try to access the variable $upload_data which not pass from your controller before.

for example you must have
Code:
//in your controller
$data['upload_data']= ..something
$this->load->view('your_view',$data);

//in your view
echo $upload_data

Maybe you must read again the MVC, understand the mean of controllers, views, models.

Also uncomment the
Code:
//$config['allowed_types'] = 'gif|jpg|png';
for security reasons.
Never let the user upload whatever file_type want.




Theme © iAndrew 2016 - Forum software by © MyBB