⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

getState for uploads while inserting and updating



Ganesh
  • profile picture
  • Member

Posted 08 January 2014 - 14:50 PM

Hi guys I am stuck with my code, here is portion of the code keeps troubling me:

       if($state == 'add'){//add state
           $upload_path = '/files/dir1/';
       }
       else if($state == 'edit'){//edit state
           $upload_path = '/files/dir2/';
       }
       $this->crud->set_field_upload('image_url',  $upload_path);

Here I am trying to swap the $upload_path according to the current state. But this one is giving me the trouble. Everything is working fine except the file is getting uploaded to default $upload_dir which is set in the Grocery_CRUD.php. After spending a lot of time i found that the $state value is 'upload_file' for all the file uploads(regardless of the add or edit operations). i badly need to differentiate between the insert or update state. I tried to sort it out using

if(strpos($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], '/add')){
             $upload_path = '/files/dir1/';
}
else if(strpos($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], '/edit')){
             $upload_path = '/files/dir2/';
}
and even tried to use:
strpos(current_url, '/add')
strpos(current_url, '/edit')

Everything failed as the Grocery_CRUD.php library file is not able to tell me anything other than 'upload_file'  as the current state. 

 

Kindly help me!  :(

Thanks in advance.

 


Ganesh
  • profile picture
  • Member

Posted 10 January 2014 - 07:31 AM

I have figured it out myself, the trick is to create a session variable and check it with the previous state before the upload was called.

for that create a variable in session, check it's value against add or edit and assign the path accordingly.

$upload_path = '';
if( $state == 'add' || $state == 'edit'){//only create the session if $state is add or edit
   $this->session->set_userdata('statevar',$state);
}
if($this->session->userdata('statevar') == 'add'){//add state
   $upload_path = 'files/dir1/';
}
else if($this->session->userdata('statevar') == 'edit'){//edit state
   $upload_path = 'files/dir2/';
}
$this->crud->set_field_upload('imgurl', $upload_path);