Welcome Guest, Not a member yet? Register   Sign In
"You did not select a file to upload." error when uploading files
#1

[eluser]johnmerlino[/eluser]
Hey all,

I have a form that allows users to upload multiple files depending on how many files are associated (in a has many relationship) with the posts record:

Code:
foreach($records as $post){
        echo form_open_multipart("homes/update/$post->id");  

         echo label('Update Title');
        echo form_input('title',$post->title);
        echo label('Update Body');
        echo form_textarea('body',$post->body);
        
         $images = $post->images->include_join_fields()->get();
        if(!is_null($images->image_file_name)){  
             echo label('Update Images');
        
            foreach($images as $image){
                
                echo form_upload("file[]");  
            }
        }

    }
    echo form_submit('submit','Update');

This is the method that it posts to:

Code:
public function update(){
            
            $this->load->library('upload');
            
            while(list($key,$value) = each($_FILES["file"]["name"])){
                
                  if(!empty($value)){
                     $config['file_name'] = $_FILES["file"]["name"][$key];    
                     $config['upload_path']='./system/application/public/images/'; //"/images/member/$user_id";
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size']    = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';
                    $config['overwrite'] = true;
                    $this->upload->initialize($config);
                    
                     if(!$this->upload->do_upload()){
                        $error = array('error' => $this->upload->display_errors());
                        echo $error['error'];
                    }
                    else {
                        $data = array('upload_data' => $this->upload->data());
                        $image_file_name = $data['upload_data']['file_name'];
                        $image = new Image();
                        $image->get_where(array('image_file_name' => $image_file_name),1);
                        if(!$image->exists()){
                            $image = new Image();
                            $image->image_file_name = $image_file_name;
                            $image->post_id = $this->uri->segment(3);
                            $image->save();
                        }
                    }
             }
            $post = new Post();
            $post->where('id', $this->uri->segment(3))->get();
            $post->title = $this->input->post('title');
            $post->body = $this->input->post('body');
             if($post->save()){
                 $this->session->set_flashdata('flash_message', 'The post has been successfully updated.');
                redirect('homes/index');
            }
            else {
                $this->session->set_flashdata('flash_message', 'An error has occurred. Please try updating the post again.');
                redirect('homes/edit');
                $post->check_last_query();
            }
          }
        }

I keep getting an error "You did not select a file to upload."

I had this working at one point. But when I decided to allow multiple files to be uploaded and therefore introduced the while iterator to iterate through each of the files in the _FILES array, that's when this error started occurring.

I know I posted on here back to back days asking questions. But I am completely confused as to why this error shows up.

Thanks for response.




Theme © iAndrew 2016 - Forum software by © MyBB