简体   繁体   中英

grocery crud getState for uploads while inserting and updating

I am stuck with codeigniter, Grocery CRUD code, here is portion of the code that 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM