Patrick
Patrick

Reputation: 1275

can't upload file with Codeigniter

i have a form that works perfectly - but it doesn't pass the file to the controller

<?php
    $this->load->helper('form');

    //open form
        $attributes = array('class' => 'contact_form');
        echo form_open_multipart('admin/add_record', $attributes);
        echo form_hidden('current_area', $current_area);

        echo form_fieldset('Dati da inserire', array('class' => 'form_fieldset'));
            echo "<p>";
            echo form_label('Titolo:', 'title', array('class' => 'form_label'));
                echo form_input('title', '', "class='form_textfield'");
            echo "</p>";
        echo form_fieldset_close();

        echo form_fieldset('Immagine', array('class' => 'form_fieldset'));
            echo '<p>';
                echo form_label('Immagine:', 'image', array('class' => 'form_label'));
                    echo form_upload('image', '', "class='form_filefield'");
            echo '</p>';
        echo form_fieldset_close(); 

        echo form_fieldset('Testo', array('class' => 'form_fieldset'));
            echo form_label('', 'description', array('class' => 'form_label'));
                echo form_textarea('description', '', array('class' => 'form_textarea', 'cols'  => 45, 'rows'  => 10));
        echo form_fieldset_close();

            //display submit button
            echo "<p>";
                echo form_submit(array('class' => 'form_submit', 'name' => 'submit'), "Inserisci!");
            echo "</p>";

        echo form_close();

?>

if i dump $_POST variables at the receiving end, i see all of them EXCLUDING the file (variable named image above).

the thing is, if I change

echo form_upload('image', '', "class='form_filefield'");

to

echo form_input('image', '', "class='form_filefield'");

it works, and the 'image' variable gets dumped like any other.

Any ideas?

thanks, Patrick

EDIT:

it seems some sort of a bug, as I've simplified the form and the controller and it still doesn't work:

VIEW:

<?php $this->load->helper('form', 'url'); ?>

<?php echo form_open_multipart('admin/add_record2');

echo form_upload('userfile', 'userfile');
?>



<br /><br />

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

</form>

CONTROLLER:

function add_record2()
{
    //prints content of entire POST variable
echo '<pre>'; print_r($_POST);echo '</pre>';
}

OUTPUT: an empty array...

Array
(
)

NEW EDIT I've managed to get it to work. Two things were needed: a) set the destination directory's permissions to 777 b) type the destination directory in UNIX format (i'm on a local machine, so /users/patrick/sites/....../uploads instead of localhost/site/img/uploads. I'm still not sure why this, and i need to remember to change this when i upload the site to webhost..

Upvotes: 0

Views: 2368

Answers (1)

elo80ka
elo80ka

Reputation: 15825

I think you should be using $_FILES instead (or $HTTP_POST_FILES if you're using a version of PHP < 4.1.0).

If that still doesn't work, check the MAX_FILE_SIZE setting in your php.ini, and make sure it isn't set too low.

Upvotes: 1

Related Questions