Advertisement
Guest User

File Upload

a guest
Mar 27th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. class UtilController extends Controller {
  2.  
  3.     function updateshowPosterImage() {
  4.  
  5.         $Page = Page::get()->filter("ID","17633")->First();
  6.         if(!$Page->BannerImageID) {
  7.             $page_name = $Page->MenuTitle;
  8.             $file_name = 'banner-image-'.$show_name.'.jpg';
  9.             $file_path = $themeFolder.'/images/page/banner/'.$file_name;
  10.             $file_upload_path = sprintf("%s/page/banner",Config::inst()->get('Upload', 'uploads_folder'));
  11.             if (!file_exists(ASSETS_PATH . DIRECTORY_SEPARATOR . $file_upload_path)) { 
  12.                     mkdir(ASSETS_PATH . DIRECTORY_SEPARATOR . $file_upload_path, 0777, true);
  13.             }
  14.             $file_upload_file =  $file_upload_path . DIRECTORY_SEPARATOR .$file_name;
  15.             $file_contents = '';
  16.             set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
  17.                 // error was suppressed with the @-operator
  18.                 if (0 === error_reporting()) {
  19.                 return false;
  20.                 }
  21.  
  22.                 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  23.             });
  24.            
  25.             try {
  26.                 if (Director::fileExists($file_path)) {
  27.                     $uploadImage = function() use ($file_name, $file_upload_file,$file_path ) {
  28.                         if(!file_exists(ASSETS_PATH . DIRECTORY_SEPARATOR. $file_upload_file)) {
  29.                             copy(BASE_PATH . DIRECTORY_SEPARATOR . $file_path, ASSETS_PATH . DIRECTORY_SEPARATOR. $file_upload_file);
  30.                         }
  31.                         // emulates the $_FILES array
  32.                         $fileconfig = array(
  33.                             'name' => $file_name,
  34.                             'type' => 'text/plaintext',
  35.                             'size' => filesize(ASSETS_PATH . DIRECTORY_SEPARATOR . $file_upload_file),
  36.                             'tmp_name' => ASSETS_PATH . DIRECTORY_SEPARATOR . $file_upload_file,
  37.                             'extension' => 'jpg',
  38.                             'error' => UPLOAD_ERR_OK,
  39.                         );
  40.  
  41.                         //test upload into default folder
  42.                         $upload = new Upload();
  43.                         $upload->validate($fileconfig);
  44.                         $upload->validator->setAllowedExtensions(array('jpg'));
  45.                         $upload->load($fileconfig);
  46.                         Debug::show($upload->getErrors());
  47.                         return $upload->getFile();
  48.                     };
  49.                     $image = $uploadImage();
  50.                     Debug::show($image);
  51.                    
  52.                 }
  53.             } catch(Exception $e) {
  54.                 Debug::show($e->getMessage());
  55.             }
  56.         }
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement