function ebook()
{
$crud = new grocery_CRUD();
$crud->set_table('ebooks');
$crud->set_field_upload('file_name','libs/pdf');
//$crud->set_field_upload('image_captions','libs/image-captions'); //disabled for html upload
$crud->edit_fields('document_title','image_captions','file_name');
$crud->columns('document_title','image_captions','file_name');
$crud->callback_field('image_captions',array($this,'uploadimg')); //change into html upload
$crud->callback_before_insert(array($this, 'upload')); //do upload
$crud->callback_before_upload(array($this, 'cekpdf'));
$crud->callback_before_delete(array($this, 'delete'));
//$crud->callback_after_upload(array($this,'resize'));
$output = $crud->render();
$this->_example_output($output);
}
this my callback
function uploadimg()
{
return '<input id="field-image_captions" name="image_captions" type="file" value="" maxlength="100">';
}
function delete($primary_key)
{
$image = $this->db->get_where('ebooks', array('ebook_id'=>$primary_key), 1)->row();
if (unlink('libs/image_captions/'.$image->image_captions))
return true;
else
return false;
}
function upload($post_array)
{
if(!empty($post_array['image_captions'])) //if I remove the if, it can upload but no change to database, but when I put the if, always return false
{
$cover = 'image_captions';
$this->load->library('upload');
$cover_name="";
$conf_cover['upload_path'] = 'libs/image-captions/';
$conf_cover['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($conf_cover);
if (!$this->upload->do_upload($cover))
{
return false;
}
else
{
$data = $this->upload->data();
$cover_name = $data['file_name'];
$config = array(
'source_image' => $data['full_path'],
'width' => 95,
'height' => 130
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
$post_array['image_captions']=$cover_name;
return $post_array;
}else{return false;}
}
function cekpdf($files_to_upload,$field_info)
{
foreach($files_to_upload as $value) {
$ext = pathinfo($value['name'], PATHINFO_EXTENSION);
}
$allowed_formats = array("pdf");
if(in_array($ext,$allowed_formats))
{
return true;
}
else
{
return 'Document file must be pdf format';
}
}
function resize($uploader_response,$field_info, $files_to_upload)
{
$this->load->library('image_moo');
//Is only one file uploaded so it ok to use it with $uploader_response[0].
$file_uploaded = $field_info->upload_path.'/'.$uploader_response[0]->name;
$this->image_moo->load($file_uploaded)->resize(95,130)->save($file_uploaded,true);
return true;
}
I already can resize image and specify the file type but the setting parameter is global for any upload
My question is.. is it possible to validate upload file_name to pdf and upload image_captions to jpg,png,gif ?
if cannot, I have tried to use the html upload but the data did not insert to database even thought the upload successfully ?