What I am trying to say though is that you don’t need to lose the folder if this is stored in the database as a row for every entry really
For example you can use: callback_after_insert to insert in the row to a field the upload_year and use it this way:
if ($crud->getState() === 'add') {
set_field_upload(‘file_url’,‘assets/uploads/files/’.date(‘Y’));
} else if ($crud->getState() === 'edit') {
// Take the information that you need from $crud->getStateInfo();
// and then have a query for that entry. The result will be something like that:
// $photoYear = $row->upload_year;
set_field_upload(‘file_url’,‘assets/uploads/files/’ . $photoYear);
}
Then you can use the callback_column and do not use the set_field_upload on the list state. For example something like this:
if ($crud->getState() === 'list' || $crud->getState() === 'ajax_list') {
$crud->callback_column('file_url', function () {
return "<a href='/assets/uploads/files/{$row->upload_year}/$value'>$value</a>";
});
} else if ($crud->getState() === 'add') {
set_field_upload('file_url','assets/uploads/files/'.date('Y'));
} else if ($crud->getState() === 'edit') {
// Take the information that you need from $crud->getStateInfo();
// and then have a query for that entry. The result will be something like that:
// $photoYear = $row->upload_year;
set_field_upload('file_url','assets/uploads/files/' . $photoYear);
}
i define this function inside controller $crud->callback_column('file_url',array($this,'_callback_upload_field'));
and create this
public function _callback_upload_field($value, $row)
{
return “<a href=’”.site_url(‘assets/uploads/files/’.$row->folder_file)."/".$row->file_url."’ target=’_blank’>$row->file_url";
}
can I ask another issue with set_field_upload('field', 'path file upload', 'set file types')
I see that inside grocery crud lib there is another argument for this function that defines what file types allowed.
I tried to set a file type(e.g pdf) but is not working.
and as you can see there is a reference to set_field_upload( 1 argue, 2 argue, there is a 3rd argument)
if you will see inside lib/grocery_crud there is this :
/** Check if the upload Url folder exists. If not then throw an exception **/
if (!is_dir(FCPATH.$upload_dir)) {
throw new Exception("It seems that the folder \"".FCPATH.$upload_dir."\" for the field name
\"".$field_name."\" doesn't exists. Please create the folder and try again.");
}
$this->upload_fields[$field_name] = (object) array(
'field_name' => $field_name,
'upload_path' => $upload_dir,
'allowed_file_types' => $allowed_file_types,
'encrypted_field_name' => $this->_unique_field_name($field_name));
return $this;
}
the code where i found in the GIT has been written from John Scoumbourdis and reports that allowed file types / per field with set_field_upload now!
I tried to set file type but not working.
i think the above code bypassed from general definition of this : $config['grocery_crud_file_upload_allow_file_types']='pdf|zip|rar|7z';
any ideas? I want to set individual file types per upload field.
i dont understand !
the function set_upload_field(‘field’,‘assets/files/upload’, ‘pdf’)
but i debug code :
public function set_field_upload($field_name, $upload_dir = ‘’, $allowed_file_types = ‘’)
{
$upload_dir = !empty($upload_dir) && substr($upload_dir,-1,1) == ‘/’
? substr($upload_dir,0,-1)
: $upload_dir;
$upload_dir = !empty($upload_dir) ? $upload_dir : ‘assets/uploads/files’;
/** Check if the upload Url folder exists. If not then throw an exception **/
if (!is_dir(FCPATH.$upload_dir)) {
throw new Exception("It seems that the folder \"".FCPATH.$upload_dir."\" for the field name
\"".$field_name."\" doesn't exists. Please create the folder and try again.");
}
$this->upload_fields[$field_name] = (object) array(
'field_name' => $field_name,
'upload_path' => $upload_dir,
'allowed_file_types' => $allowed_file_types,
'encrypted_field_name' => $this->_unique_field_name($field_name));
return $this;
}
the argument ‘pdf’ don’t pushed to $allowed_file_types at the defined function.
ignore comments.
I dont understand why $crud->set_field_upload(‘file_url’,$p1) when i try to upload a file , cant get the full path from value $p1 so the function set_field_upload gets only the default path ‘assets/upload/files’.
why ? what is going wrong? the session value is for testing.
I am so sorry that I was away for so long. I had to attend to personal issues, and was away from my computer all that time. Please do you still need help in this, so I could look it up ?
hello again,
may i revive this post and i would like to ask , inside getState==edit how can i get a database field ? to
add it on set_field_upload function.
inside controller i create a function to get the folder name and i call it inside edit like this :
but when I go to edit state and I am trying to upload a file , the file uploads to the default folder (assets/uploads/files) if upload_dir is empty but its not.
I print the $photoyear value which is the name of the folder. So I don’t understand why I can’t upload to a specific folder .
can you help with a more detailed example ; Also i cant find more info about getStateInfo. An example.