Ejemplo n.º 1
0
 function upload_file($origin, $dest, $tmp_name, $overwrite = false, $aid, $imgtype = "portrait", $imgwidth, $imgheight)
 {
     FileManagerController::_checkPermission();
     $origin = basename($origin);
     $full_dest = $dest . $origin;
     $file_name = $origin;
     for ($i = 1; file_exists($full_dest); $i++) {
         if ($overwrite) {
             unlink($full_dest);
             continue;
         }
         $file_ext = strpos($origin, '.') === false ? '' : '.' . substr(strrchr($origin, '.'), 1);
         $file_name = substr($origin, 0, strlen($origin) - strlen($file_ext)) . '_' . $i . $file_ext;
         $full_dest = $dest . $file_name;
     }
     // create new directory
     if (mkdir($dest)) {
         chmod($dest, 0777);
     }
     if (move_uploaded_file($tmp_name, $full_dest)) {
         //Resized thumbnail w=767 for fnbgallery thumb
         $thumb_file_name = '767x575_' . $file_name;
         if ($imgtype == "landscape") {
             $thumb_url = URL_PUBLIC . 'wolfimage?src=public/fnb/gallery/' . $aid . '/' . $file_name . '&h=575';
         } else {
             $thumb_url = URL_PUBLIC . 'wolfimage?src=public/fnb/gallery/' . $aid . '/' . $file_name . '&w=767';
         }
         $thumb_image = FILES_DIR . '/fnb/gallery/' . $aid . '/' . $thumb_file_name;
         if (file_put_contents($thumb_image, file_get_contents($thumb_url))) {
             //Crop resized thumbnail for w=767 for fnbgallery thumb
             $thumb_url_crop = URL_PUBLIC . 'wolfimage?src=public/fnb/gallery/' . $aid . '/' . $thumb_file_name . '&w=767&h=575&c=c';
             $thumb_image_crop = FILES_DIR . '/fnb/gallery/' . $aid . '/' . $thumb_file_name;
             file_put_contents($thumb_image_crop, file_get_contents($thumb_url_crop));
         }
         //Add Image to database
         $data = $_POST['fnbgallery'];
         Flash::set('post_data', (object) $data);
         $oFnbGallery = new FnbGallery();
         $last_seq = $oFnbGallery->getLastFnbGallerySeq($aid);
         $fnbgallery = new FnbGallery($data);
         $fnbgallery->filename = $file_name;
         $fnbgallery->sequence = $last_seq + 1;
         if (!$fnbgallery->save()) {
             Flash::set('error', __('Image could not be added!'));
             redirect(get_url('fnbgallery'));
         } else {
             Flash::set('success', __('Image has been added.'));
         }
         // change mode of the dire to 0644 by default
         chmod($full_dest, 0644);
         return $file_name;
     }
     return false;
 }