Advertisement
christiansalazarh

Coco File Uploader - Saving the image to a database

Jan 30th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. /*
  2.     Store an uploaded Image into database using Coco File Uploader
  3.     ==============================================================
  4.  
  5.     a sample class file ready to be used with Coco Extension (Ajax File Uploader)
  6.     https://github.com/christiansalazar/coco
  7. */
  8. class MyModel {
  9.     public function onFileUploadedPromoImage($fullName, $userData){
  10.         // STEP 1:
  11.         // determine the Mime Type, you must store it in your database
  12.         // for future usage
  13.         list($w, $h, $xmime) = getimagesize($fullName);
  14.         $mime = '';
  15.         if($xmime == 2)
  16.             $mime = 'image/jpg';
  17.         if($xmime == 3)
  18.             $mime = 'image/png';
  19.         // STEP 2:
  20.         // store the uploaded file into a "LONG BLOB" in your database
  21.         //     
  22.         $img = new Image();
  23.         $img->mimetype = $mime;
  24.         $img->alt = '';
  25.         $img->original_width = $w;
  26.         $img->original_height = $h;
  27.         $img->image_size = filesize($fullName);
  28.         $img->picture_position = 0;
  29.         // Read the image data into the LONG_BLOB field
  30.         $f = fopen($fullName,"r");
  31.         $img->image = fread($f,$img->image_size);
  32.         fclose($f);
  33.         if($img->save()){
  34.            // ok is stored
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement