Example #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 client thumb
         $thumb_file_name = '767x575_' . $file_name;
         if ($imgtype == "landscape") {
             $thumb_url = URL_PUBLIC . 'wolfimage?src=public/client/' . $aid . '/' . $file_name . '&h=575';
         } else {
             $thumb_url = URL_PUBLIC . 'wolfimage?src=public/client/' . $aid . '/' . $file_name . '&w=767';
         }
         $thumb_image = FILES_DIR . '/client/' . $aid . '/' . $thumb_file_name;
         if (file_put_contents($thumb_image, file_get_contents($thumb_url))) {
             //Crop resized thumbnail for w=767 for client thumb
             $thumb_url_crop = URL_PUBLIC . 'wolfimage?src=public/client/' . $aid . '/' . $thumb_file_name . '&w=767&h=575&c=c';
             $thumb_image_crop = FILES_DIR . '/client/' . $aid . '/' . $thumb_file_name;
             file_put_contents($thumb_image_crop, file_get_contents($thumb_url_crop));
         }
         //Add Image to database
         $data = $_POST['client'];
         Flash::set('post_data', (object) $data);
         $oClient = new Client();
         $last_seq = $oClient->getLastClientSeq($aid);
         $client = new Client($data);
         $client->filename = $file_name;
         $client->sequence = $last_seq + 1;
         if (!$client->save()) {
             Flash::set('error', __('Image could not be added!'));
             redirect(get_url('client'));
         } 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;
 }