Welcome Guest, Not a member yet? Register   Sign In
File Upload Help
#1

[eluser]malzahar[/eluser]
I'm having trouble getting file uploads to work with my form. I made sure the upload folder permissions are correct (755) and the library looks to be configured properly but when I try to do an upload it just fails but does not return any errors. If anyone could take a look at this for me it would be much appreciated.

Parts of the form that matters.
Code:
echo form_open_multipart('form/form_validate', $attributes);
echo form_upload('userfile');
echo form_submit('submit', 'Submit');
echo form_close();

Part of the controller that matters
Code:
function form_validate() {
//some validation rules none that apply to the file upload
if($this->form_validation->run() == TRUE) {
$config = array(
     'allowed_types' => 'jpg|jpeg|pdf',
     'upload_path' => realpath(APPPATH . '../uploads/'),
     'max_size' => 1500,
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')) {
     echo "failed to upload";
     echo $this->upload->display_errors('<p>', '</p>');
}
else {
     echo "file uploaded";
}
}
#2

[eluser]ppwalks[/eluser]
Code:
if ($this->upload->do_upload('product_thumb'))
            {
                $data = array(
                    'thumbnail' => $this->upload->data()
                );
     $file_name = $data['thumbnail']['file_name'];
    
            }
            else
            {
                echo $this->upload->display_errors();
            }


I had the same problem a week ago, prepare your do_upload like mine and all should be good
#3

[eluser]ppwalks[/eluser]
Are you inserting into database, if so then make sure you pass thhe variable to the model, 'product_thumb' is the name of my input field
#4

[eluser]malzahar[/eluser]
I'm inserting the other form data into a database and that appears to be working fine. Once the insert completes it goes to the upload code as shown below.

Code:
if($this->My_Data_Model->my_insert_function()) {
if($this->form_validation->run() == TRUE) {
$config = array(
     'allowed_types' => 'jpg|jpeg|pdf',
     'upload_path' => realpath(APPPATH . '../uploads/'),
     'max_size' => 1500,
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')) {
     echo "failed to upload";
     echo $this->upload->display_errors('<p>', '</p>');
}
else {
     echo "file uploaded";
}
}

After using $this->output->enable_profiler(TRUE); and viewing the post data it doesn't even show a value for $this->input->post('userfile') or in my $_FILES array so I'm not sure why the file isn't even being referenced.
#5

[eluser]ppwalks[/eluser]
That is because you need to extract the data from the do_upload, do upload is an array so you need to extract the ['file_name'] as shown above and move into the variable, it should then show up
#6

[eluser]ppwalks[/eluser]
The code above is not inserting yet I have to move the variable into the db
#7

[eluser]malzahar[/eluser]
ah got it working thanks for your help!




Theme © iAndrew 2016 - Forum software by © MyBB