⚠ 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

Items added to the library: file validation , Create thumbnail for your images , Add numbers to the last file in the file name to avoid interference.



moslemir

moslemir
  • profile picture
  • Member

Posted 16 October 2011 - 17:13 PM

Hello to all my friends


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

braincoder

braincoder
  • profile picture
  • Member

Posted 22 October 2011 - 11:44 AM

Thank you for this modification i was looking for something like that , i tested it and it works good for my core system. :)
it helps me a lot .

Moslem Mansouri

Moslem Mansouri
  • profile picture
  • Member

Posted 25 October 2011 - 20:11 PM

I'm glad it was useful for you

hunterbit

hunterbit
  • profile picture
  • Member

Posted 24 November 2011 - 01:23 AM

[color=#333333][font=arial, sans-serif][size=4]I did[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]try to modify[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]the[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]source code[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]proposed[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]but unfortunately[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]I did not[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]create any[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]thumbnail.[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]What[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]wrong[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]?[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]What could[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]be the problem[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]?[/size][/font][/color]

[color=#333333][font=arial, sans-serif][size=4]Thanks[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]to all[/size][/font][/color]

xxaxxo

xxaxxo
  • profile picture
  • Member

Posted 29 November 2011 - 10:33 AM

This modification works great but there are some things you need to do to make it work that are not mentioned....
I'm using it with CI 2.1 and Grocery CRUD 1.1.4
1. load library upload manually (i'm doing it at the controller $this->load->library('upload'); )
2. create a "Thumbnail" named folder at the upload place it's hardcoded to the modification that the thumbs go to folder: $upload_path/Thumbnail

hunterbit

hunterbit
  • profile picture
  • Member

Posted 30 November 2011 - 22:03 PM

[color=#333333][font=arial, sans-serif][size=4]Now I[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]try[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]this suggestion[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]and will[/size][/font][/color] [color=#333333][font=arial, sans-serif][size=4]let you know.[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]thanks[/size][/font][/color]

gersontk

gersontk
  • profile picture
  • Member

Posted 28 December 2011 - 11:20 AM

nice....im try

hunterbit

hunterbit
  • profile picture
  • Member

Posted 04 February 2012 - 01:04 AM

[color=#880000][color=#333333][font=arial, sans-serif][size=4]unfortunately[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]I do not[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]work in[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]any way[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]I saw the[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]log but[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]no errors[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]I[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]also tried[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]debugging[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]to follow[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the path[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]of[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the source[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]code that[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]comes[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]up[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]below and then[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]goes on[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]without running[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]return[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]FALSE[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4],[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]so[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]should[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]you successfully create[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]thumbnail[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]Instead nothing[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]to do[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]does not create[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]any files[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]How[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]can I fix[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]?[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]What is[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4] [/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the problem[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]?[/size][/font][/color]

[color=#333333][font=arial, sans-serif][size=4]Thank you.[/size][/font][/color]


// Generate the Thumbnail If Is Image[/color]
[color=#000000]$ci [/color][color=#666600]=[/color][color=#000000] [/color][color=#666600]&[/color][color=#000000]get_instance[/color][color=#666600]();[/color]
[color=#000000]$ci[/color][color=#666600]->[/color][color=#000000]load[/color][color=#666600]->[/color][color=#000000]library[/color][color=#666600]([/color][color=#008800]'image_lib'[/color][color=#666600],[/color][color=#000000] $config[/color][color=#666600]);[/color]
[color=#000088]if[/color][color=#000000] [/color][color=#666600]([/color][color=#000000] [/color][color=#666600]![/color][color=#000000] $ci[/color][color=#666600]->[/color][color=#000000]image_lib[/color][color=#666600]->[/color][color=#000000]resize[/color][color=#666600]())[/color]
[color=#666600]{[/color]
[color=#000088]return[/color][color=#000000] FALSE[/color][color=#666600];[/color]
[color=#666600]}[/color]

hunterbit

hunterbit
  • profile picture
  • Member

Posted 04 February 2012 - 01:24 AM

I change code


// Generate the Thumbnail If Is Image
$ci = &get_instance();
$ci->load->library('image_lib', $config);
if ( ! $ci->image_lib->resize())
{
return FALSE;
}



with this code



// creiamo la thumbnail
$ci->load->library('image_lib');
$ci->image_moo
->load($upload_path . $state_info->file_name)
->resize($thumbnail_array[0], $thumbnail_array[1])
->save($upload_path . "Thumbnail/" . $state_info->file_name);


and now it's OK but I dont understand because [size=4][color=#333333][font=arial, sans-serif]does not work[/font][/color][color=#333333][font=arial, sans-serif] [/font][/color][color=#333333][font=arial, sans-serif]with the first code.[/font][/color][/size]

[size=4][color=#333333][font=arial, sans-serif]Thanck[/font][/color][/size]

Moslem Mansouri

Moslem Mansouri
  • profile picture
  • Member

Posted 08 February 2012 - 12:18 PM

[quote name='hunterbit' timestamp='1328317494' post='428']
[color=#880000][color=#333333][font=arial, sans-serif][size=4]unfortunately[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]I do not[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]work in[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]any way[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]I saw the[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]log but[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]no errors[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]I[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]also tried[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]debugging[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]to follow[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the path[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]of[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the source[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]code that[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]comes[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]up[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]below and then[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]goes on[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]without running[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]return[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]FALSE[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4],[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]so[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]should[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]you successfully create[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]thumbnail[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]Instead nothing[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]to do[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]does not create[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]any files[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4].[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]How[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]can I fix[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]?[/size][/font][/color]
[color=#333333][font=arial, sans-serif][size=4]What is[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]the problem[/size][/font][/color][color=#333333][font=arial, sans-serif][size=4]?[/size][/font][/color]

[color=#333333][font=arial, sans-serif][size=4]Thank you.[/size][/font][/color]


// Generate the Thumbnail If Is Image[/color]
[color=#000000]$ci [/color][color=#666600]=[/color][color=#000000] [/color][color=#666600]&[/color][color=#000000]get_instance[/color][color=#666600]();[/color]
[color=#000000]$ci[/color][color=#666600]->[/color][color=#000000]load[/color][color=#666600]->[/color][color=#000000]library[/color][color=#666600]([/color][color=#008800]'image_lib'[/color][color=#666600],[/color][color=#000000] $config[/color][color=#666600]);[/color]
[color=#000088]if[/color][color=#000000] [/color][color=#666600]([/color][color=#000000] [/color][color=#666600]![/color][color=#000000] $ci[/color][color=#666600]->[/color][color=#000000]image_lib[/color][color=#666600]->[/color][color=#000000]resize[/color][color=#666600]())[/color]
[color=#666600]{[/color]
[color=#000088]return[/color][color=#000000] FALSE[/color][color=#666600];[/color]
[color=#666600]}[/color]
[/quote]


Hello
I have no problem.
You can get the error that gives you a picture?
I have not created the folder thumbnail.

Moslem Mansouri

Moslem Mansouri
  • profile picture
  • Member

Posted 08 February 2012 - 12:57 PM

Changes in the code:
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");
}


@web-johnny please add this code in the next version of the library
Thanks Dear Admin

Mohamed Alsemany

Mohamed Alsemany
  • profile picture
  • Member

Posted 21 February 2012 - 23:49 PM

that's good
I hope this will be included to the next version
it's realy need some restrictions on the files extensions

Moslem Mansouri

Moslem Mansouri
  • profile picture
  • Member

Posted 22 February 2012 - 18:57 PM

thanks mohamed

konradinho

konradinho
  • profile picture
  • Member

Posted 02 August 2012 - 08:31 AM

Why not work? :(

This is my code:



public function zarzadzaj_wpisami()
{

try{
$crud = new grocery_CRUD();

$crud->set_table('aktualnosci');
$crud->columns('aktualnosci_tytul','aktualnosci_tresc','aktualnosci_zdjecie','aktualnosci_data');
$crud->fields('aktualnosci_tytul','aktualnosci_tresc','aktualnosci_zdjecie','aktualnosci_data');

$crud->display_as('aktualnosci_tytul','Tytuł wpisu');
$crud->display_as('aktualnosci_tresc','Treść wpisu');
$crud->display_as('aktualnosci_zdjecie','Zdjecie wpisu');
$crud->display_as('aktualnosci_data','Data dodania');

$crud->set_field_upload('aktualnosci_zdjecie','uploads/news', 'jpg|jpeg|gif|png', '25|25');

$output = $crud->render();

$this->_zarzadzaj_wpisami($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}

}

function _zarzadzaj_wpisami($output = null)
{
$this->load->view('admin/zarzadzaj_wpisami',$output);
}


And error:

http://i49.tinypic.com/a4q16r.png

Thanks.