Saeed Masoumi
Saeed Masoumi

Reputation: 8916

How to create file uploader in codeigniter?

I see CI documentation to create an image uploader. here is my Controller :

<?php
class Upload extends CI_Controller {
function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

function index()
{
    $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload()
{
    $this->load->helper(array('form', 'url'));
    $this->load->helper('url');
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '0';
    $config['max_width']  = '0';
    $config['max_height']  = '0';

    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload())
    {
        echo $this->upload->display_error();
        return FALSE;
    }
    else
    {
        $data = $this->upload->data();
        return TRUE;
    }
}
}

and here is my View

<html>
<head>
    <title>Upload Form</title>
</head>
<body>
<form action="upload/do_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

But when i upload an image it doesn't upload it and $this->upload->display_error() show nothing(my uploads folder which is in my ci root folder also has 777 permision and i'm using wamp)

Upvotes: 0

Views: 203

Answers (2)

Saeed Masoumi
Saeed Masoumi

Reputation: 8916

I change my function named function do_upload() to function somethingelse() and my problem solved (because i'm calling a built in function)

Upvotes: 0

Nil&#39;z
Nil&#39;z

Reputation: 7475

Two pointers for you:

<input type="file" name="userfile" size="20" />

Check if the file you're using is not exceeding and the file is being uploaded.

echo $this->upload->display_errors();

See the display_errors with an s.

Suggestions:

In you form's action attribute use a an absolute URL, like:

action="<?php echo base_url() ?>upload/do_upload"

Inside your function do_upload(), do a print_r($_FILEs), to confirm that the file is uploaded correctly with no errors.

Upvotes: 1

Related Questions