EllisLab text mark
Advanced Search
2 of 2
2
   
Form Data and File Upload
Posted: 01 December 2009 02:03 PM   [ Ignore ]   [ # 16 ]   [ Rating: 0 ]
Avatar
Joined: 2008-09-07
119 posts

In my case it was being sent every time. I don’t know if it was something in the CI code or if that is how it always is with a file field in a form (haven’t really looked into it).

Just happy it works now.  grin

 Signature 

Branson Web Design
Handmade Fashion Jewelry

 
Posted: 01 December 2009 08:43 PM   [ Ignore ]   [ # 17 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-26
163 posts

Looks like you guys have been busy today.

My thoughts were along the same lines as überfuzz’s. But yeah, it’s not something I’ve actually tried. It would seems that the File Upload class could use an extension to incorporate some sort of method that enables you to check for uploaded files without accessing the $_FILES superglobal.

Good work.

jeremy

 Signature 

Jeremy Gimbel, web nerd and developer and ExpressionEngine enthusiast, Mastermind at Conflux Group, Inc.
twitter: @dreadfullyposh
web: http://confluxgroup.com
blog: http://dreadfullyposh.com

 
Posted: 02 December 2009 08:15 AM   [ Ignore ]   [ # 18 ]   [ Rating: 0 ]
Joined: 2009-11-27
5 posts

my solution is:

if($_FILES[‘news_img’][‘name’] != NULL)
{
  //do your upload code
}

It’s ok for me, tested.

 
Posted: 21 January 2010 03:51 AM   [ Ignore ]   [ # 19 ]   [ Rating: 0 ]
Avatar
Joined: 2009-11-27
59 posts

HELP please…

i got also the same problem in this thread…

i read and follow all your discussion here but it had’nt work in my form..

please help me..


here’s my code

VIEW

<div class="infield">
                                    
<?php echo form_open_multipart('at_reports/post');?>
                                    
<input type="file" name="userfile" class="tb" title="<?=lang('icon');?>"/>
                                    <
input type="submit" value="upload" />
                                    </
form>
                                    <!--<
input type="text" name="icon" class="tb" title="<?=lang ('icon');?>" />-->
                                </
div


CONTROLLER

IN MY FUNCTION POST..HERE’S MY FILE UPLOADING CODE

function post() {
            
//$this->output->enable_profiler(true);
            
$c "";
            
$data['action'$this->input->post('action');

.....

 else if (
$data['action']=='save'{
                $item 
$this->input->post("item");
                
$dt['module_name'$this->input->post('module_name');
    
                
$report_exist $this->Reports->getDetailsByName($dt);

                if (
$report_exist && $item != $report_exist['module_id'])
                
{
                    $c 
.= 'Report already exists.';
                    
$data['action''exist';
                
}
                
else
                
{
                
                
                $config[
'upload_path''./reports/module/';
                
$config['allowed_types''gif|jpg|png';
                
$config['max_size']    '100';
                
$config['max_width']  '1024';
                
$config['max_height']  '768';
                
                
$this->load->library('upload'$config);
                
                
               
            
                
                
$this->upload->do_upload(); 
                
                
$file_data $this->upload->data();
                
$file_name $file_data['file_name'];
                
            
            
                            
                
$fields = array(
                
"module_name"=>$this->input->post('module_name'),
                
"slug"=>$this->input->post('slug'),     
                 
"icon"=>$file_data['file_name'],       
                
"report_file_name"=>$this->input->post('report_file_name'),
                
"sql_select"=>$this->input->post('sql_select'),
                
"sql_filter"=>$this->input->post('sql_filter'),
                
"sql_order"=>$this->input->post('sql_order')
                );         

                
/*foreach ($fields as $field)
                {
                    if (isset($_POST[$field])) $dt[$field] = $this->input->post($field);
                }*/
                
                
$item $this->input->post("item");
                
$rows $this->input->post("row0");
                        
               
                
$data['item'$this->Reports->save($fields,$item,$rows);    

                
$c .= 'Successfully saved item';


                
}
            $data[
'response'$c;
            
$json['json'$data;
                
            

MODEL

function save($data,$item,$details{
        
if ($item == ''{
            $data[
'link'"reports/view";
            
            
$this->db->trans_start();
            
$this->db->insert('sec_reports'$data);
            
$item $this->db->insert_id();
            
$this->save_detail($item,$details);            
            
$this->db->trans_complete();
            
}
        
else {
            $this
->db->trans_start();
            
$this->db->where('module_id'$item);
            
$this->db->update('sec_reports'$data);
            
$this->save_detail($item,$details);                
            
$this->db->trans_complete();
        
}
        
return $item;
    

i got only other fields to be save in database..only the icon field does’nt save in database…the value always saved is empty or null…

please help me to find the problem in my code..

tnx in advance..

 Signature 

“If You never try, You’ll never know!!!” tongue wink

 
Posted: 21 January 2010 11:29 AM   [ Ignore ]   [ # 20 ]   [ Rating: 0 ]
Avatar
Joined: 2008-09-07
119 posts

Which solution did you try? What exactly is happening in your case?

 Signature 

Branson Web Design
Handmade Fashion Jewelry

 
Posted: 21 January 2010 11:15 PM   [ Ignore ]   [ # 21 ]   [ Rating: 0 ]
Avatar
Joined: 2009-11-27
59 posts
wowdezign - 21 January 2010 04:29 PM

Which solution did you try? What exactly is happening in your case?

uhmm..all the solution u mention in this thread i tried in my form…but it doesnt work on me..the above code i posted is the recent code i have after trying ur solution..please help me..i thought the problem is in my controller function???in the array $fields where i use POST?


in my case ...

i have 6 input text field..and 1 uplading field for image

and my problem is…no value has been saved in my icon field in my database…but all other input text field value has been succesfully saved in the database…


here’s my sample form below for you to figure out my problem…

 Signature 

“If You never try, You’ll never know!!!” tongue wink

 
Posted: 07 December 2010 09:13 AM   [ Ignore ]   [ # 22 ]   [ Rating: 0 ]
Joined: 2008-06-24
7 posts

Hey ... How would i work this with multiple file fields?

I have 5 fields, their names are userfile[] ... I cant seem to get your examples to work.

I have tried this:

if (isset($_FILES['userfile']) && $_FILES['userfile']['error'!= 4){
    
echo 'THIS IS NOT FUNNY';

and also this

if($_FILES['userfile']['error'!= 4){
    
echo 'WORK DAMNIT';

Neither of which works.

Cheers,

 
Posted: 07 December 2010 03:08 PM   [ Ignore ]   [ # 23 ]   [ Rating: 0 ]
Joined: 2008-04-29
44 posts
Intervelopment - 07 December 2010 02:13 PM

Hey ... How would i work this with multiple file fields?

I have 5 fields, their names are userfile[] ... I cant seem to get your examples to work.

I have tried this:

if (isset($_FILES['userfile']) && $_FILES['userfile']['error'!= 4){
    
echo 'THIS IS NOT FUNNY';

and also this

if($_FILES['userfile']['error'!= 4){
    
echo 'WORK DAMNIT';

Neither of which works.

Cheers,

You will need to go 1 level deeper, as you have an array of “userfile"s:

if($_FILES[0]['userfile']['error'!= 4){
    
echo 'WORK DAMNIT';

Obviously you have to do this in a loop, and replace the zero with what is applicable (or use foreach.).

 
Posted: 07 December 2010 10:55 PM   [ Ignore ]   [ # 24 ]   [ Rating: 0 ]
Avatar
Joined: 2010-02-21
21 posts

Hey,

I tried your suggestion .... It didnt work, but i tried something else which worked.

if($_FILES['userfile']['error'][0] == 4){
 
Posted: 08 December 2010 02:14 PM   [ Ignore ]   [ # 25 ]   [ Rating: 0 ]
Joined: 2008-04-29
44 posts
Funky Fresh - 08 December 2010 03:55 AM

Hey,

I tried your suggestion .... It didnt work, but i tried something else which worked.

if($_FILES['userfile']['error'][0] == 4){

Apologies, I did get the counter around the wrong way, thanks for posting the correct solution.

 
Posted: 05 October 2012 05:38 AM   [ Ignore ]   [ # 26 ]   [ Rating: 0 ]
Joined: 2012-10-05
3 posts

i also have a problem. The $_FILES is empty when i pass it as a POST value. Here is my code. hope you can help me guys.

this is my FORM

<form name="frmAudioadd1" id="frmAudioadd1" method="POST" action="<?php echo base_url()?>video/save1" enctype="multipart/form-data">                                                  

<
table width="600">
  <
tr>
    <
td width="20" valign="top"><div class="datalabel" align="left">Title</div></td>
    <
td>
     <
input type="text" name="video_title1" id="video_title1" value="" size="38"/>
     <
input type="hidden" name="source1" id="source1" value="" /> 
    </
td>
  </
tr>     
  <
tr>   
    <
td width="20"><div class="datalabel" align="left">Video</div></td>        
    <
td width="250"
      <
div align="left">
         <
input type="file" id="video" name="video" >
      </
div>
    </
td>
  </
tr>   
  <
tr>
    <
td width="20"><div class="datalabel" align="left">Description</div></td>
    <
td width="250" height="300">
      <
div align="left">
       <
textarea name="description1" id="description1" cols="50" rows="15"></textarea>     
</
div></td>                        
   </
tr>  
    <
tr>
    <
td width="20"><div class="datalabel" align="left">Featured&nbsp;</div></td>
       <
td width="250">
        <
select name="featured1" id="featured1" class="select">
          <
option value="">- please select -</option>
          <
option value="1">YES</option>
           <
option value="2">NO</option>
      </
select>
    </
tr
  <
tr>
  <
td width="20"><div class="datalabel" align="left">Archive&nbsp;</div></td>
  <
td width="250"> <select name="archive_name1" id="archive_name1" class="select">
    <
option value="">- please select -</option>    
    
<?php
      
if($video_archive){                        
      
foreach($video_archive as $indexcountry){
     ?>
    
<option value="<?php echo $indexcountry["id"]; ?>">
    
<?php echo $indexcountry["archive_name"]?></option>
    
<?php
        }
         }
     ?>
     
</tr
    <
tr>
      <
td>&nbsp;</td>
    <
td><input type="button" name="cmdSave1" id="cmdSave1">/input></td>
                                                </
tr>
                                                 </
table>
                                           </
form


This is my function in saving the video.

function save1()
    
{   
        $path 
'uploads/video';  
        
$config['upload_path'$path;
        
$config['allowed_types''mp4|mpg|m4v';
        
$config['max_size']   '10000000'//size in kilobytes 
     //   $config['max_width'] = '295';
     //   $config['max_height'] = '250'; 
        
        
$this->load->library('upload'$config);
        

        if(
$_FILES['video']['name']!="" && $_FILES['video']['name']!="")
        
{                                      
            $target_path_small 
$path .'/'basename$_FILES['video']['name']); 
            
move_uploaded_file($_FILES['video']['tmp_name']$target_path_small)
        

    } 
 
Posted: 05 October 2012 05:54 AM   [ Ignore ]   [ # 27 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-25
1009 posts

I prefer to let the fileupload be a part of the form_valdation
and use a callback function to upload like this:

if ( isset($_FILES['userfile']['name']) AND strlen($_FILES['userfile']['name']) > )
{
 $this
->form_validation->set_rules('userfile''Image''callback__do_image_upload');

Otherwise you need more if statements in case the fileupload failed when
it comes to process the form data in general.

 
Posted: 07 October 2012 09:02 PM   [ Ignore ]   [ # 28 ]   [ Rating: 0 ]
Joined: 2012-10-05
3 posts
LuckyFella73 - 05 October 2012 05:54 AM

I prefer to let the fileupload be a part of the form_valdation
and use a callback function to upload like this:

if ( isset($_FILES['userfile']['name']) AND strlen($_FILES['userfile']['name']) > )
{
 $this
->form_validation->set_rules('userfile''Image''callback__do_image_upload');

Otherwise you need more if statements in case the fileupload failed when
it comes to process the form data in general.

thank you for looking up bro. smile  i’ll try this one and see if this works.

 
Posted: 07 October 2012 09:16 PM   [ Ignore ]   [ # 29 ]   [ Rating: 0 ]
Joined: 2012-10-05
3 posts

after days of browsing the internet. ahaha. finally i got the video upload working.
for those who had the same issue as mine. look up the code shown above, my 1st post. I think thats a pretty correct to upload photo and dont forget to:
1st - add the correct mime type for the audio, video, image on the array if it still dont exist. as for mine. i added the:
= ‘wmv’  =>  array(‘video/wmv’, ‘video/x-ms-wmv’, ‘flv-application/octet-stream’, ‘application/octet-stream’),
= ‘flv’  =>  array(‘video/flv’, ‘video/x-flv’, ‘flv-application/octet-stream’, ‘application/octet-stream’),
= ‘m4v’ => ‘video/x-m4v’,
= ‘mp4’ => ‘video/mp4’,
since the are mostly the videos that i will be uploading.
2nd - I added:
  = AddType video/x-m4v m4v
  = AddType audio/x-m4a m4a
  = AddType video/mp4 mp4
on the .htaccess
3rd - if you upload videos which is more that 2mb or 5mb. surely CI will provide you an error. The code which really provided me with the success:
= php_value upload_max_filesize 50M
= php_value post_max_size 50M
= php_value max_execution_time 200
= php_value max_input_time 200
i added this also on the .htaccess so that it can upload up to 50mb video.

~ i hope this could help and if their is a problem about my solution. feel free to tell me why. i am just also a novice. smile

 
2 of 2
2