Welcome Guest, Not a member yet? Register   Sign In
File Upload Not Working with Validation
#1

[eluser]mpar612[/eluser]
Hi Everyone,

I am still pretty new to codeigniter and am having difficulty getting a file to upload and pass validation. The first line of code below is the form validation and it does call the artist_upload function, but it goes straight to the "You must upload a .gif, .jpg, .png or .pdf" and doesn't appear to be recognizing that a file is even uploaded. Any thoughts? Thanks in advance!

The form field is called 'artist_image'.

Code:
$this->form_validation->set_rules('artist_image', 'Artist Image', 'callback_artist_upload');

My function that handles the file upload:
Code:
public function artist_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '10000';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';
  
  $this->load->library('upload', $config);

  echo $_FILES['artist_image'];

if (isset($_FILES['artist_image']) && !empty($_FILES['artist_image']['name']))
  {
   if ($this->upload->do_upload('artist_image'))
   {
    // set a $_POST value for file for use in db query
    $_POST['artist_image'] = $this->upload->data('file_name');
    return true;
   }
   else
   {
    // display error because does not meet $config requirements
    $this->form_validation->set_message('artist_upload', $this->upload->display_errors());
    return false;
   }
  }
  else
  {
   // display error because nothing was uploaded
   $this->form_validation->set_message('artist_upload', "You must upload a .gif, .jpg, .png or .pdf");
   return false;
  }
}
#2

[eluser]TheFuzzy0ne[/eluser]
Please post your view.
#3

[eluser]mpar612[/eluser]
Thank you for taking a look at this! Any feedback anyone could provide would be great. Thanks!

The View file is huge and it wouldn't let me post all of it or attach it. Below is most of the form. There are a few areas that have "...", this means that the file upload fields are duplicated for a total of four files for each section.

View:
Code:
<?php echo form_open('admin/add_artist'); ?>
<div id="addMainWrapper">
&lt;!-- start #Main --&gt;
   <div id="addWrapper">
      <div id="addHeader">Main Information</div>
      <div id="addName1">&lt;?php echo form_label('Artist Name:', 'artist_name');?&gt;</div>
      <div id="addField1">&lt;?php echo form_input('artist_name', set_value('artist_name'), 'id="artist_name"'); ?&gt;</div>
      
      <div id="addName1">&lt;?php echo form_label('Artist Image:', 'artist_image');?&gt;</div>
      <div id="addField1">&lt;?php echo form_upload('artist_image', set_value('artist_image'), 'id="artist_image"'); ?&gt;</div>
      
      <div id="addName1">&lt;?php echo form_label('Bio:', 'bio');?&gt;</div>
      <div id="addField1">&lt;?php echo form_input('bio', set_value('bio'), 'id="bio"'); ?&gt;</div>
      
      <div id="addName1">&lt;?php echo form_label('Tour Link - http://:', 'tour_link');?&gt;</div>
      <div id="addField1">&lt;?php echo form_input('tour', set_value('tour'), 'id="tour"'); ?&gt;</div>      
   </div>

&lt;!-- start #Press Release --&gt;
   <div id="addWrapper">
      <div id="addHeader">Press Release</div>
      <div id="addName">&lt;?php echo form_label('Press Title:', 'press_title');?&gt;</div>
      <div id="addField">&lt;?php echo form_input('press_release_1', set_value('press_release_1'), 'id="press_release_1"'); ?&gt;</div>
      <div id="addName1">File:</div>
      <div id="addField1">&lt;?php echo form_upload('press_release_upload_1', set_value('press_release_upload_1'), 'id="press_release_upload_1"'); ?&gt;</div>
      
...
  
   </div>
</div>


<div id="addMainWrapper">
&lt;!-- start #Press Clips --&gt;
   <div id="addWrapper">
      <div id="addHeader">Press Clips</div>
      <div id="addName">&lt;?php echo form_label('Clip Title:', 'clip_title');?&gt;</div>
      <div id="addField">&lt;?php echo form_input('press_clips_1', set_value('press_clips_1'), 'id="press_clips_1"'); ?&gt;</div>
      <div id="addName1">File:</div>
      <div id="addField1">&lt;?php echo form_upload('press_clips_upload_1', set_value('press_clips_upload_1'), 'id="press_clips_upload_1"'); ?&gt;</div>

...
      
   </div>


&lt;!-- start #Photos --&gt;
   <div id="addWrapper">
      <div id="addHeader">Photos/Logos/Cover Art</div>
      <div id="addName">&lt;?php echo form_label('Image Title:', 'image_title');?&gt;</div>
      <div id="addField">&lt;?php echo form_input('image_title_1', set_value('image_title_1'), 'id="image_title_1"'); ?&gt;</div>
      <div id="addName1">File:</div>
      <div id="addField1">&lt;?php echo form_upload('image_upload_1', set_value('image_upload_1'), 'id="image_upload_1"'); ?&gt;</div>

...

<div id="addSubmit">&lt;?php echo form_submit('submit', 'submit'); ?&gt;</div>
&lt;?php echo validation_errors(); ?&gt;  
  
&lt;?php echo form_close(); ?&gt;

Controller:
Code:
public function add_artist()
{
  $data['title'] = 'Create a shop item';  
  
  $this->form_validation->set_rules('artist_name', 'Artist Name', 'required');
  $this->form_validation->set_rules('artist_image', 'Artist Image', 'callback_artist_upload');
  $this->form_validation->set_rules('press_release_upload_1', 'Press Release', 'callback_file_upload');
  $this->form_validation->set_rules('press_release_upload_2', 'Press Release', 'callback_file_upload');
  $this->form_validation->set_rules('press_release_upload_3', 'Press Release', 'callback_file_upload');
  $this->form_validation->set_rules('press_release_upload_4', 'Press Release', 'callback_file_upload');
  $this->form_validation->set_rules('press_clips_upload_1', 'Press Clips', 'callback_file_upload');
  $this->form_validation->set_rules('press_clips_upload_2', 'Press Clips', 'callback_file_upload');
  $this->form_validation->set_rules('press_clips_upload_3', 'Press Clips', 'callback_file_upload');
  $this->form_validation->set_rules('press_clips_upload_4', 'Press Clips', 'callback_file_upload');
  $this->form_validation->set_rules('image_upload_1', 'Image Upload', 'callback_file_upload');
  $this->form_validation->set_rules('image_upload_2', 'Image Upload', 'callback_file_upload');
  $this->form_validation->set_rules('image_upload_3', 'Image Upload', 'callback_file_upload');
  $this->form_validation->set_rules('image_upload_4', 'Image Upload', 'callback_file_upload');
  
  if ($this->form_validation->run() === FALSE)
  {
   //$this->load->view('templates/header', $data);
   $this->load->view('admin/add');
   //$this->load->view('templates/footer');
   return false;
  }
  else
  {
   $this->admin_model->add_artist();
  
   $this->load->view('admin/success', $data);
   return true;
  }
}

public function artist_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '10000';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';
  
  $this->load->library('upload', $config);

  echo $_FILES['artist_image'];

if (isset($_FILES['artist_image']) && !empty($_FILES['artist_image']['name']))
  {
   if ($this->upload->do_upload('artist_image'))
   {
    // set a $_POST value for file for use in db query
    $_POST['artist_image'] = $this->upload->data('file_name');
    return true;
   }
   else
   {
    // display error because does not meet $config requirements
    $this->form_validation->set_message('artist_upload', $this->upload->display_errors());
    return false;
   }
  }
  else
  {
   // display error because nothing was uploaded
   $this->form_validation->set_message('artist_upload', "You must upload a .gif, .jpg, .png or .pdf");
   return false;
  }
}
#4

[eluser]mpar612[/eluser]
I got it. form_open should be form_open_multipart. Sorry for any confusion and time spent. Thank you. This minor problem drove me insane for 24 hours.
#5

[eluser]TheFuzzy0ne[/eluser]
That was going to be my next comment. Well spotted. Smile




Theme © iAndrew 2016 - Forum software by © MyBB