/**
  * Makes a fully qualifies image path with either
  * random name or original name.
  * 
  * @param  Upload  $image      
  * @param  boolean $useOriginal
  * @return string
  */
 public function makePath(Upload $image, $useOriginal)
 {
     $base = public_path('assets/uploads/images/');
     if ($useOriginal) {
         return $base . $image->getClientOriginalName() . '.' . $image->getClientOriginalExtension();
     }
     return $base . str_random(10) . '.' . $image->getClientOriginalExtension();
 }
Exemple #2
0
 /**
  * Handle the file upload. Returns the array on success, or false
  * on failure.
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  * @param String $path where to upload file
  * @return array|bool
  */
 public function handle(UploadedFile $file, $path = 'uploads')
 {
     $input = array();
     $fileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
     // Detect and transform Croppa pattern to avoid problem with Croppa::delete()
     $fileName = preg_replace('#([0-9_]+)x([0-9_]+)#', "\$1-\$2", $fileName);
     $input['path'] = $path;
     $input['extension'] = '.' . $file->getClientOriginalExtension();
     $input['filesize'] = $file->getClientSize();
     $input['mimetype'] = $file->getClientMimeType();
     $input['filename'] = $fileName . $input['extension'];
     $fileTypes = Config::get('file.types');
     $input['type'] = $fileTypes[strtolower($file->getClientOriginalExtension())];
     $filecounter = 1;
     while (file_exists($input['path'] . '/' . $input['filename'])) {
         $input['filename'] = $fileName . '_' . $filecounter++ . $input['extension'];
     }
     try {
         $file->move($input['path'], $input['filename']);
         list($input['width'], $input['height']) = getimagesize($input['path'] . '/' . $input['filename']);
         return $input;
     } catch (FileException $e) {
         Notification::error($e->getmessage());
         return false;
     }
 }
Exemple #3
0
 /**
  * Get the file name for the photo
  *
  * @return string
  */
 public function fileName()
 {
     if (!is_null($this->name) && $this->name) {
         return $this->name;
     }
     $name = sha1($this->file->getClientOriginalName() . '-' . microtime());
     $extension = $this->file->getClientOriginalExtension();
     return "{$name}.{$extension}";
 }
Exemple #4
0
 public static function upload(UploadedFile $file, $uploadPath = null)
 {
     if (is_null($uploadPath)) {
         $uploadPath = public_path() . '/uploads/';
     }
     $fileName = str_slug(getFileName($file->getClientOriginalName())) . '.' . $file->getClientOriginalExtension();
     //Make file name unique so it doesn't overwrite any old photos
     while (file_exists($uploadPath . $fileName)) {
         $fileName = str_slug(getFileName($file->getClientOriginalName())) . '-' . str_random(5) . '.' . $file->getClientOriginalExtension();
     }
     $file->move($uploadPath, $fileName);
     return ['filename' => $fileName, 'fullpath' => $uploadPath . $fileName];
 }
 /**
  * 上传单个文件
  *
  * @param  \Symfony\Component\HttpFoundation\File\UploadedFile $file
  * @param  bool                                                $autoName
  * @return null|string
  */
 public function uploadFile(UploadedFile $file, $autoName = true)
 {
     //print_r($file);exit;
     if ($autoName) {
         $filename = sprintf('%s.%s', uniqid(), $file->getClientOriginalExtension());
     } else {
         $filename = $file->getClientOriginalName();
     }
     $relativePath = '';
     switch ($this->groupedBy) {
         case static::GROUPED_BY_MONTH:
             $dateObj = new \DateTime();
             $relativePath = $dateObj->format('Y-m') . '/';
             break;
         case static::GROUPED_BY_DATE:
             $dateObj = new \DateTime();
             $relativePath = $dateObj->format('Y-m') . '/' . $dateObj->format('d') . '/';
             break;
         default:
     }
     $toDir = $this->getUploadDir() . $relativePath;
     if (!$this->fs->exists($toDir)) {
         $this->fs->mkdir($toDir);
     }
     $file->move($toDir, $filename);
     return $relativePath . $filename;
 }
Exemple #6
0
 /**
  * Converts UploadedFile of users into array of User objects
  *
  * @param UploadedFile $file
  * @return array
  */
 public function parseUserFile(UploadedFile $file)
 {
     $data = array('users' => array(), 'columns' => array());
     switch ($file->getClientOriginalExtension()) {
         case 'csv':
             $file = $file->openFile();
             $data['columns'] = $file->fgetcsv();
             while (!$file->eof()) {
                 $user = new User();
                 $row = $file->fgetcsv();
                 if (array(null) == $row) {
                     continue;
                 }
                 foreach ($row as $key => $userProperty) {
                     $functionName = 'set' . ucfirst($data['columns'][$key]);
                     if (!method_exists($user, $functionName)) {
                         throw new MappingException('User has no property ' . $data['columns'][$key]);
                     }
                     $user->{$functionName}($userProperty);
                 }
                 $this->isUserValid($user);
                 $data['users'][] = $user;
             }
             break;
     }
     return $data;
 }
 public function upload(UploadedFile $file, $folder)
 {
     $path = $this->path . $folder . '/';
     $name = md5(uniqid(rand(), 1)) . '.' . $file->getClientOriginalExtension();
     $file->move($path, $name);
     return url('img', [$folder, $name]);
 }
 /**
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile|array $file
  *
  * @return string
  */
 public static function generateFilename($file)
 {
     $filename = md5(uniqid() . '_' . $file->getClientOriginalName());
     $extension = $file->getClientOriginalExtension();
     $filename = $extension ? $filename . '.' . $extension : $filename;
     return $filename;
 }
Exemple #9
0
 /**
  * Put and save a file in the public directory
  *
  * @param string path of the file
  * @return mixed keypath of file or false if error occurred during uploading
  */
 public static function putUploadedFile(UploadedFile $file)
 {
     if ($file->isValid()) {
         //Remove all the slashes that doesn't serve
         FileStorage::clearPublicStartPath();
         //Retrive and save the file extension of the file uploaded
         $fileExtension = $file->getClientOriginalExtension();
         //Save the public path with the start path
         $absolutePath = public_path() . '/' . FileStorage::$publicStartPath;
         //Generate a random name to use for the file uploaded
         $keyFile = FileStorage::generateKey(FileStorage::$keyLength) . '.' . $fileExtension;
         //Check if the file with the $keyFile name doesn't exist, else, regenerate it
         while (file_exists($absolutePath . '/' . ord($keyFile[0]) . '/' . $keyFile)) {
             $keyFile = FileStorage::generateKey(FileStorage::$keyLength) . '.' . $fileExtension;
         }
         //Move the uploaded file and save
         $file->move($absolutePath . '/' . ord($keyFile[0]), $keyFile);
         //Save the keypath (start path, sub path, file name)
         $keyPath = FileStorage::$publicStartPath . '/' . ord($keyFile[0]) . '/' . $keyFile;
         //Return public path of the file
         return $keyPath;
     } else {
         return false;
     }
 }
 /**
  * Generate a proper filename.
  *
  * @param UploadedFile $file
  * @return bool|string
  */
 private function getFilename(UploadedFile $file)
 {
     $path = $file->getClientOriginalName();
     $path .= '_' . dechex(time());
     $path .= '.' . $file->getClientOriginalExtension();
     return $path;
 }
Exemple #11
0
 /**
  * {@inheritdoc}
  */
 public function canUpload(UploadedFile $file)
 {
     if ($file->getMimeType() == 'text/plain' && $file->getClientOriginalExtension() == 'md') {
         return 5;
     }
     return 0;
 }
 protected function upload(UploadedFile $file, $oldFile)
 {
     $list = "";
     //        foreach($files as $file)
     //        {
     //            $validator = Validator::make( array('file' => $file) , array('file' => array($this->Rule) ) );
     //
     //            if($validator->fails())
     //            {
     //laravel內建的驗證無法使用(可能是bug吧),所以自己寫一個
     foreach ($this->Rule as $rule) {
         if ($file->getClientOriginalExtension() == $rule) {
             if ($file->isValid()) {
                 if ($this->groupno != "") {
                     $year = substr($this->groupno, 1, 3);
                     $destinationPath = public_path() . '/upload/' . $year . '/' . $this->groupno;
                 } else {
                     $destinationPath = public_path() . '/upload/teacher';
                 }
                 $fileName = $file->getClientOriginalName();
                 File::delete($destinationPath . '/' . $oldFile);
                 $file->move($destinationPath, $fileName);
                 //用 "|" 隔開檔名
                 $list .= $fileName . "|";
             }
         }
     }
     //            }
     //        }
     $list = substr($list, 0, -1);
     return $list;
 }
 /**
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  *
  * @return string
  */
 protected function uploadSingleFile($file)
 {
     $filename = uniqid() . '.' . $file->getClientOriginalExtension();
     $targetDirectory = public_path($this->config->get('paxifi.files.uploads_directory'));
     $file->move($targetDirectory, $filename);
     return $this->config->get('app.url') . '/' . basename($targetDirectory) . '/' . $filename;
 }
 public function upload(UploadedFile $file, $path = null)
 {
     // Check if the file's mime type is in the list of allowed mime types.
     if (!in_array($file->getClientMimeType(), self::$allowedMimeTypes)) {
         throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
     }
     if ($path) {
         $filename = sprintf('%s/%s.%s', $path, md5(time() * rand(0, 99)), $file->getClientOriginalExtension());
     } else {
         $filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), md5(time() * rand(0, 99)), $file->getClientOriginalExtension());
     }
     $adapter = $this->filesystem->getAdapter();
     $adapter->setMetadata($filename, array('contentType' => $file->getClientMimeType()));
     $adapter->write($filename, file_get_contents($file->getPathname()));
     return $filename;
 }
 /**
  * Upload a single file.
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  *
  * @param string $target
  * @return string
  */
 protected function uploadSingleFile($file, $target = null)
 {
     $filename = uniqid() . '.' . $file->getClientOriginalExtension();
     $filename = $target ? $target . '/' . $filename : $filename;
     $content = file_get_contents($file->getRealPath());
     $this->flysystem->write($filename, $content);
     return cloudfront_asset($filename);
 }
Exemple #16
0
 public static function generatePostImage(UploadedFile $file)
 {
     $fileName = self::renameFile($file->getClientOriginalName(), $file->getClientOriginalExtension());
     // Get full path
     $fullPath = self::createImage($file, $fileName);
     // Return full path
     return $fullPath;
 }
Exemple #17
0
 /**
  * Return the filename which is passed or get from file
  * Filename is slug.
  *
  * @param UploadedFile $image
  * @param $filename
  * @return string
  */
 protected function getFilename(UploadedFile $image, $filename)
 {
     if (empty($filename)) {
         // use filename from uploaded file
         $filename = str_replace('.' . $image->getClientOriginalExtension(), '', $image->getClientOriginalName());
     }
     $slugify = new Slugify();
     return $slugify->slugify($filename);
 }
Exemple #18
0
 public function setFile(\Symfony\Component\HttpFoundation\File\UploadedFile $file)
 {
     // temporarily save file for further handling
     $this->tempFile = $file;
     $this->filename = $file->getClientOriginalName();
     $this->size = $file->getClientSize();
     $this->extension = $file->getClientOriginalExtension();
     $this->mimetype = $file->getMimeType();
 }
 public function create(UploadedFile $file, User $user)
 {
     $filename = sha1(rand(11111, 99999));
     $extension = $file->getClientOriginalExtension();
     Storage::put('images/' . $filename . '.' . $extension, file_get_contents($file->getRealPath()));
     $image = new Image(['user_id' => $user->id, 'filename' => $file->getClientOriginalName(), 'path' => $filename . '.' . $extension, 'mime_type' => $file->getMimeType(), 'location' => 'local', 'status' => Image::STATUS_PENDING]);
     $image->save();
     return $image;
 }
Exemple #20
0
 /**
  * Upload file to storage.
  *
  * @return void
  */
 public function upload(UploadedFile $uploaded_file)
 {
     $filename = str_pad($this->id, 5, '0', STR_PAD_LEFT) . '.' . $uploaded_file->getClientOriginalExtension();
     $path = storage_path('app') . "/tmp/";
     $uploaded_file->move($path, $filename);
     $this->storage->move("tmp/" . $filename, $this->getPath() . "/" . $filename);
     $this->filename = $filename;
     $this->save();
 }
 /**
  * @param UploadedFile $file
  * @return string
  */
 protected function moveToLocalStorage($file)
 {
     if (!file_exists(storage_path('app/doctor-information'))) {
         \Storage::makeDirectory('doctor-information');
     }
     $disk_filename = Carbon::now()->format('YmdHis') . '.' . $file->getClientOriginalExtension();
     $file->move(storage_path('app/doctor-information'), $disk_filename);
     return $disk_filename;
 }
Exemple #22
0
 public function upload(UploadedFile $file)
 {
     $original = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
     $sanitize = preg_replace('/[^a-zA-Z0-9]+/', '-', $original);
     $fileName = $sanitize . '.' . $file->getClientOriginalExtension();
     $destination = public_path() . DIRECTORY_SEPARATOR . 'uploads/courses';
     $uploaded = $file->move($destination, $fileName);
     return $fileName;
 }
Exemple #23
0
 public function __construct($context, UploadedFile $file)
 {
     $this->uploadsPath = Config::get('image_crop_resizer.uploads_path');
     $this->contexts = Config::get('image_crop_resizer');
     $this->checkDir($context);
     $this->file = $file;
     $this->filename = $this->generateImageName($context, $file->getClientOriginalExtension());
     $this->determineFilesizeLimit();
     $this->process($context);
 }
Exemple #24
0
 /**
  * Upload the file
  * @param UploadedFile 	$file 	File to be uploaded
  * @param string 		$path 	Directory to store the file
  */
 public function upload(UploadedFile $file, $path = null)
 {
     $extension = $file->getClientOriginalExtension();
     $filename = $this->getFilename($extension);
     $path = $this->getPath($path);
     if ($file->move($path, $filename) == false) {
         throw new UploadFailedException('Unable to upload the file');
     }
     return $filename;
 }
Exemple #25
0
 /**
  * Handles Upload files.
  *
  * @param UploadedFile $file
  * @param $folder
  * @return string
  */
 private function upload(UploadedFile $file, $folder)
 {
     $path = $this->path . $folder . '/';
     $name = md5(uniqid(rand(), 1)) . '.' . $file->getClientOriginalExtension();
     if ($file->move($path, $name)) {
         return $name;
     } else {
         return;
     }
 }
 /**
  * Upload the user's photo
  */
 public function uploadPhoto()
 {
     if ($this->photo == null) {
         return;
     }
     $path = __DIR__ . '/../../../../../web/uploads/users/img';
     $name = uniqid() . "." . $this->photo->getClientOriginalExtension();
     $this->photo->move($path, $name);
     $this->setPhotoPath($name);
 }
 /**
  * @param UploadedFile $uploadedFile
  * @return null|string
  */
 protected function handleFileUpload($uploadedFile, $delimiter = ',', $enclosure = '"')
 {
     $errMsg = '';
     if (!$uploadedFile instanceof UploadedFile) {
         $errMsg = _("No file selected");
     } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
         $errMsg = _("Larger than upload_max_filesize ") . ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
     } elseif ($uploadedFile->getClientOriginalExtension() != 'csv') {
         $errMsg = _('Invalid extension ') . $uploadedFile->getClientOriginalExtension() . ' of file ' . $uploadedFile->getClientOriginalName();
     }
     if (!empty($errMsg)) {
         return $errMsg;
     }
     /** @var LicenseCsvImport */
     $licenseCsvImport = $this->getObject('app.license_csv_import');
     $licenseCsvImport->setDelimiter($delimiter);
     $licenseCsvImport->setEnclosure($enclosure);
     return $licenseCsvImport->handleFile($uploadedFile->getRealPath());
 }
Exemple #28
0
 /**
  * @param UploadedFile $file
  * @param string $path
  * @return string
  */
 public function processFile(UploadedFile $file, $folder, $existing = null)
 {
     if ($existing) {
         $this->files->destroyFile($folder . '/' . $existing);
     }
     $ext = $file->getClientOriginalExtension();
     $filename = $this->files->createFilename($ext, $folder);
     $file->move(storage_path('app/' . $folder), $filename);
     return $filename;
 }
 public function uploadLogo()
 {
     if ($this->logo == null) {
         return;
     }
     $path = __DIR__ . '/../../../../../web/uploads/school/img';
     $name = uniqid() . "." . $this->logo->getClientOriginalExtension();
     $this->logo->move($path, $name);
     $this->setLogoPath($name);
 }
Exemple #30
-1
 /**
  * Upload provided file and create matching FileRecord object
  *
  * @param UploadedFile $file
  * @param $clientIp
  * @return FileRecord
  */
 public function uploadFile(UploadedFile $file, $clientIp)
 {
     $extension = Str::lower($file->getClientOriginalExtension());
     $generatedName = $this->generateName($extension);
     // Create SHA-256 hash of file
     $fileHash = hash_file('sha256', $file->getPathname());
     // Check if file already exists
     $existingFile = FileRecord::where('hash', '=', $fileHash)->first();
     if ($existingFile) {
         return $existingFile;
     }
     // Query previous scans in VirusTotal for this file
     if (config('virustotal.enabled') === true) {
         $this->checkVirusTotalForHash($fileHash);
     }
     // Get filesize
     $filesize = $file->getSize();
     // Check max upload size
     $maxUploadSize = config('upload.max_size');
     if ($filesize > $maxUploadSize) {
         throw new MaxUploadSizeException();
     }
     // Move the file
     $uploadDirectory = config('upload.directory');
     $file->move($uploadDirectory, $generatedName);
     // Create the record
     /** @var FileRecord $record */
     $record = FileRecord::create(['client_name' => $file->getClientOriginalName(), 'generated_name' => $generatedName, 'filesize' => $filesize, 'hash' => $fileHash, 'uploaded_by_ip' => $clientIp]);
     return $record;
 }