CodeIgniter Forums

Full Version: [solved] upload problem - dont see it - pls have a look
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, 
I've got an upload function that should work. Obviously something is wrong. It just reloads the site.
I try to upload a png-file with 12kb and I dont get into the "if($this->upload->do_upload()) {". 
Tryed localhost and on onlineserver.  What the hack is it? Any hint or help really appreciated!
Controller:
PHP Code:
...
public function 
mediaupload($media=FALSE)
    {
        if (!
$this->ion_auth->logged_in() || $media==FALSE){redirect(base_url(), 'refresh');}        

 
       $public_path                      base_url('assets/media/'.strtolower($media));
 
       $config['upload_path'           dirname($_SERVER['DOCUMENT_ROOT']).'/assets/media/'.strtolower($media);
 
       $config['allowed_types'         'gif|jpg|jpeg|png|pdf|mp3|mp4';
 
       $config['max_size'              '81920'
 
       $config['remove_spaces'         TRUE //it will remove all space
 
       $config['encrypt_name'          TRUE  //it wil encrypte the original file name
 
       $this->load->library('upload'$config);

 
       if($this->upload->do_upload())
 
       
 
           $this->media->saveMedia($public_path);
 
           echo "<pre>"print_r($this->upload->data()); print_r($this->input->post());echo "</pre>"; exit;
 
           if($this->upload->display_errors())
 
                 $this->upload->display_errors();
 
                   $_SESSION['errormessage'] = $this->upload->display_errors();
 
                   $this->session->mark_as_flash('errormessage');
 
                   }
 
           //redirect(base_url('media/'.$media), 'refresh');
 
       }
 
      else
 
       {
 
           $id                           $this->session->userdata('user_id');
 
           $user                         $this->ion_auth->user($id)->row();
 
           $data['title'               $this->lang->line('mediaupload').' ('.$this->lang->line($media).')';
 
           $data['menu'                $this->menu();
 
           $data['csrf'                array('name' => $this->security->get_csrf_token_name(),'hash' => $this->security->get_csrf_hash());
 
           $data['category'            $media;
 
           $data['typeDropdown'        $this->typeDropdown();
 
           $data['sub_heading'         $this->lang->line('please_upload');
 
           $data['username'            $user->first_name;
 
           $data['view'                'media/upload';
 
           if(isset($_SESSION['errormessage'])){$data['errormessage']=$_SESSION['errormessage'];}
 
           $this->load->view('backend',$data);
 
       
    }
... 


Model:
PHP Code:
...
public function 
saveMedia($public_path)
 
   {
 
       $data['file_name'       $this->upload->data('file_name');
 
       $data['file_type'       $this->upload->data('file_type');
 
       $data['file_path'       $this->upload->data('file_path');
 
       $data['full_path'       $this->upload->data('full_path');
 
       $data['public_path'     $public_path.'/'.$data['file_name'];
 
       $data['raw_name'        $this->upload->data('raw_name');
 
       $data['orig_name'       $this->upload->data('orig_name');
//        $data['client_name']    =   $this->upload->data('client_name');
 
       $data['file_ext'        $this->upload->data('file_ext');
 
       $data['file_size'       $this->upload->data('file_size');
 
       $data['is_image'        $this->upload->data('is_image');
 
       $data['image_width'     $this->upload->data('image_width');
 
       $data['image_height'    $this->upload->data('image_height');
 
       $data['image_type'      $this->upload->data('image_type');
 
       $data['image_size_str'] =   $this->upload->data('image_size_str');
 
       $data['category'        $this->input->post('category');
 
       $data['user_id'         $this->session->userdata('user_id');
 
       $data['created_at'      date('Y-m-d H:i:s',time());
 
       $data['created_by'      $this->session->userdata('user_id');
 
       $data['updated_by'      $this->session->userdata('user_id');
 
       if($this->input->post('new_name') && ($this->input->post('new_name') != ''))
 
           {
 
               $filename $this->input->post('new_name');
 
               if($this->hasExtension($filename))
 
                     $data['name'    $filename }
 
               else  $data['name'    $filename $data['file_ext'];  }
 
           }
 
       else  $data['name'    $data['orig_name']; }
 
       if($this->input->post('alt') && ($this->input->post('alt') != ''))
 
             $data['alt'             $this->input->post('alt');}
 
       else  $data['alt'     $this->cleanAltText($data['orig_name']);}
 
       $data['copyright'       $this->input->post('copyright');
 
       $data['type'            $this->input->post('type');
 
       $this->db->insert('media'$data);
 
       $this->save_thumbnail($data['full_path'], $data['raw_name'],'50','50');
 
       if($this->second_size($data['type'])!=FALSE){$this->resize_and_crop($data['type'], $data['file_name'], $this->second_size($data['type']));}
 
   }
... 


View:
Code:
...
<?php if(isset($errormessage)&&($errormessage!='')){echo '<div class="alert">'.$errormessage.'</div>';} ?>
           <div class="row">
               <div class="col-md-12">
<!-- -->
   <?php echo form_open_multipart(base_url('media/mediaupload/'.$category));?>
<p>
   <label for="category"><?=$this->lang->line('Category')?></label><br />
<?php //if($category != 'image'){$category = substr($category,0,-1);} ?>
   <?=$category?> <?=form_hidden('category',$category)?>
   </p>
<p>
   <label for="type"><?=$this->lang->line('type')?></label><br />
   <?=isset($typeDropdown)? $typeDropdown : ''?>
   </p>
<p>
   <label for="category"><?=$this->lang->line('choose_file')?></label><br />
   <input type="file" name="userfile" size="20" />
   </p>
<p>
   <label for="alt"><?=$this->lang->line('alt_name')?></label><br />
   <?=form_input('alt',set_value('alt'),'class="col-md-2"')?><br />
   </p>
<p>
   <label for="name"><?=$this->lang->line('differing_name')?></label><br />
   <?=form_input('new_name',set_value('new_name'),'class="col-md-2" placeholder="'.$this->lang->line('if_needed').'"')?><br />
   </p>
<p>
   <label for="name"><?=$this->lang->line('copyright')?></label><br />
   <?=form_input('copyright',set_value('copyright',''),'class="col-md-2" placeholder="'.$this->lang->line('copyright_owner').'"')?><br />
   </p>
<br />
<p>
   <input type="submit" value="upload" />
   </p>
</form>
...

I tryed "if($this->upload->do_upload('userfile))", but it did not change anything.
Just havin a quick look: either an authentification problem or the media parameter is not set.

If I were you, I would change
 redirect(base_url(), 'refresh');
with something like
 echo 'no login or no parameter';
(just for debugging) to make sure where the problem is.
(07-29-2016, 06:05 AM)ComputingFroggy Wrote: [ -> ]Just havin a quick look: either an authentification problem or the media parameter is not set.

If I were you, I would change
 redirect(base_url(), 'refresh');
with something like
 echo 'no login or no parameter';
(just for debugging) to make sure where the problem is.

Thanks, will try, 
but I do not get a "$this->upload->do_upload() == TRUE", so it stays in the ELSE-part.
Dear lord your code formatting. I have no idea how you read that.

Comment this line out and see what happens:
Code:
if (!$this->ion_auth->logged_in() || $media==FALSE){redirect(base_url(), 'refresh');}

If it still doesn't work, var_dump this line:
Code:
dirname($_SERVER['DOCUMENT_ROOT']).'/assets/media/'.strtolower($media)

Does that path exist and is it writable?

If it is, inspect the form tag with dev tools and post it.
Yes, following spjonez suggestion, I found out my $config['upload_path'] was wrong. There was a change in the file/folder-system which I had completely forgotten.