I'll congratulate the opening of the forum
This useful little library in my upload and delete files, edit it
Items added to the library:
1 - file validation.
2 - Create thumbnail for your images.
3 - Add numbers to the last file in the file name to avoid interference
function upload_file find, and the following change:
protected function upload_file($state_info)
{
if(isset($state_info->field_name) && isset($this->upload_fields[$state_info->field_name]))
{
$upload_info = $this->upload_fields[$state_info->field_name];
// Set useful variables
$upload_path = $this->upload_fields[$state_info->field_name]->upload_path . '/';
$file_type = $this->upload_fields[$state_info->field_name]->file_type;
$thumbnail = $this->upload_fields[$state_info->field_name]->thumbnail;
$upload_file = explode('.', $state_info->file_name);
$upload_file_ext = end($upload_file);
$upload_file_name = substr($state_info->file_name, 0, ((strlen($upload_file_ext)+1)*-1));
$upload_file_name_orig = $upload_file_name;
if($file_type != NULL)
{
$file_type_array = explode('|',$file_type);
if(!in_array($upload_file_ext,$file_type_array))
{
//return FALSE;
}
}
// Create upload path folder if it doesn't already exist
if (!is_dir($upload_path)) {
mkdir($upload_path);
}
$i = 1;
while(file_exists($upload_path . $upload_file_name . '.' . $upload_file_ext)){
// Our file exists. Rename.
$upload_file_name = $upload_file_name_orig . '_' . $i;
$state_info->file_name = $upload_file_name . '.' . $upload_file_ext;
$i++;
}
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
$target = fopen("{$upload_info->upload_path}/{$state_info->file_name}", "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
if($thumbnail != NULL)
{
// Create Thumbnail folder if it doesn't already exist
if (!is_dir($upload_path . "Thumbnail")) {
mkdir($upload_path . "Thumbnail");
}
// File Uploaded
$thumbnail_array = explode('|',$thumbnail);
$config = array (
'source_image' => $upload_path . $state_info->file_name,
'new_image' => $upload_path . "Thumbnail/" . $state_info->file_name,
'maintain_ration' => FALSE,
'width' => $thumbnail_array[0],
'height' => $thumbnail_array[1]
);
// Generate the Thumbnail If Is Image
$ci = &get_instance();
$ci->load->library('image_lib', $config);
if ( ! $ci->image_lib->resize())
{
//return FALSE;
}
}
return (object)array('file_name' => $state_info->file_name);
}
else
{
//return false;
}
}
function delete_file find, and the following change:
protected function delete_file($state_info)
{
if(isset($state_info->field_name) && isset($this->upload_fields[$state_info->field_name]))
{
$upload_info = $this->upload_fields[$state_info->field_name];
if(file_exists("{$upload_info->upload_path}/{$state_info->file_name}"))
{
if( unlink("{$upload_info->upload_path}/{$state_info->file_name}") )
{
$this->basic_model->db_file_delete($state_info->field_name, $state_info->file_name);
if(file_exists("{$upload_info->upload_path}/Thumbnail/{$state_info->file_name}"))
{
unlink("{$upload_info->upload_path}/Thumbnail/{$state_info->file_name}");
}
return true;
}
else
{
return false;
}
}
else
{
$this->basic_model->db_file_delete($state_info->field_name, $state_info->file_name);
return true;
}
}
else
{
return false;
}
}
function set_field_upload find, and the following change:
/**
*
* Enter description here ...
* @param string $field_name
* @param string $upload_path
* @param array $File_type
*/
public function set_field_upload($field_name, $upload_path , $file_type = NULL , $thumbnail = NULL)
{
$upload_path = substr($upload_path,-1,1) == '/' ? substr($upload_path,0,-1) : $upload_path;
$this->upload_fields[$field_name] = (object)array( 'field_name' => $field_name , 'upload_path' => $upload_path , 'file_type' => $file_type , 'thumbnail' => $thumbnail);
}
Finished editing the library. Now consider the following example:
function news()
{
try{
$crud = new grocery_CRUD();
$crud->set_table('news');
//$crud->set_field_upload('Fild Name','Path Of Upload' [, 'File Type Validate , 'Thumbnail Width|Thumbnail Height']);
// Upload Without Validate And Thumbnail
$crud->set_field_upload('mp3','songs');
// Upload Without Thumbnail
$crud->set_field_upload('mp3','songs' , 'mp3');
$crud->set_field_upload('poster','images/songs' , 'jpg|jpeg|gif|png');
// Upload With Validate And Thumbnail
$crud->set_field_upload('picture','images/news' , 'jpg|jpeg|gif|png' , '25|25');
$output = $crud->render();
$this->_example_output($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
Which is very useful for me
I hope you will also be useful for
...................................................................
M sorry for my poor English
---------------------------------------------------------------------------------------------------
[color=#ff0000]Changes in the code:[/color]
I added a few lines of code
Some friends had a problem with the product thumbnail and thumbnail folders themselves did not know who should build the project.
I added the code to the destination folder and auto-thumbnailing not need to build it manually.
// Create upload path folder if it doesn't already exist
if (!is_dir($upload_path)){
mkdir($upload_path);
}
// Create Thumbnail folder if it doesn't already exist
if (!is_dir($upload_path . "Thumbnail")) {
mkdir($upload_path . "Thumbnail");
}
Admin please add this code in the next version of the library
Thanks Dear Admin