/** * Handles an uploaded file by putting it in the $targetDir. * * @param UploadedFile $uploadedFile File object that has been uploaded - usually taken from Request object. * @param string $targetDir [optional] Where to (relatively to the storage root dir) put the file? * @param array $allowed [optional] What files are allowed? If not matching then will throw exception. * @param int $maxFileSize [optional] What is the maximum allowed file size for this file? * @return File */ public function handleUploadedFile(UploadedFile $uploadedFile, $targetDir = '/', array $allowed = array(), $maxFileSize = 0) { array_walk($allowed, function ($ext) { return strtolower($ext); }); $targetDir = trim($targetDir, '/'); $targetDir = $this->path . $targetDir . (empty($targetDir) ? '' : '/'); $filenameElements = explode('.', $uploadedFile->getClientOriginalName()); $extension = array_pop($filenameElements); $extension = strtolower($extension); $filename = implode('.', $filenameElements); $targetName = StringUtils::fileNameFriendly($filename . '-' . StringUtils::random() . '.' . $extension); $targetPath = $targetDir . $targetName; // create unique file name while (file_exists($targetPath)) { $targetName = StringUtils::fileNameFriendly($filename . '-' . StringUtils::random() . '.' . $extension); $targetPath = $targetDir . $targetName; } // basic check for allowed type if (!empty($allowed) && !in_array($extension, $allowed)) { throw new FileException('The uploaded file is not of a valid type (allowed: ' . implode(', ', $allowed) . ').'); } // basic check for max allowed size if ($maxFileSize && $uploadedFile->getSize() > $maxFileSize) { throw new FileException('The uploaded file is too big (max allowed size is ' . StringUtils::bytesToString($maxFileSize) . ').'); } try { $movedFile = $uploadedFile->move(rtrim($targetDir, '/'), $targetName); } catch (SfFileException $e) { // if exception thrown then convert it to our exception throw new FileException($e->getMessage(), $e->getCode()); } $file = $this->convertSfFileToStorageFile($movedFile); return $file; }
/** * Execute the job. */ public function handle() { /** * Create Extra * @var Extra $extra */ $extra = $this->product->extras()->create($this->data); $aspectDir = @$this->data['aspect_ratio'] ?: '16x9'; /** * Move Extra Content to folder */ $path = '/image/products-extras/'; $extraID = $extra->getAttribute('id'); $productCode = $this->product->getAttribute('code'); $filename = $productCode . '-extra-image-' . $extraID . '.' . $this->image->guessExtension(); $image = $this->image->move(base_path() . $path, $filename); $extra->setAttribute('image', $path . $filename); $filename = $productCode . '-extra-video-' . $extraID . '.' . $this->video->guessExtension(); $video = $this->video->move(base_path() . $path . $aspectDir . '/', $filename); $extra->setAttribute('video', $filename); $extra->save(); /** * Announce ExtraWasCreated */ event(new ExtraWasCreated($extra)); }
/** * {@inheritdoc} */ public function save() { if (null !== $this->file) { $this->file->move($this->getUploadRootDir(), $this->path); unset($this->file); } return $this; }
public function upload() { if (null === $this->file) { return; } $name = $this->file->getClientOriginalName(); $this->file->move($this->getUploadRootDir(), $name); $this->url = $name; }
/** * Process the form. * * @return void */ public function save() { // attach the photo to the flyer $photo = $this->flyer->addPhoto($this->makePhoto()); // move the photo to the image folder $this->file->move($photo->baseDir(), $photo->name); // generate a thumbnail $this->thumbnail->make($photo->path, $photo->thumbnail_path); }
/** * Uplaod a photo if exits. */ protected function uploadAvatar() { if (!$this->file) { return; } $name = $this->makeFileName(); $this->user['avatar'] = '/' . $this->baseDir . $name; $this->file->move($this->baseDir, $name); $this->fitAvatar($this->user['avatar']); }
public function upload() { if ($this->rules->passes()) { try { $this->file->move($this->path . $this->subfolder, $this->filename . '.' . $this->file->getClientOriginalExtension()); return true; } catch (\Exception $e) { dd($e->getMessage()); return false; } } return false; }
/** * @param Filesystem $files * @param AccountManager $manager * @return mixed */ public function handle(Filesystem $files, AccountManager $manager) { $temp_dir = storage_path('media') . '/' . $this->owner->getMediaFolder('images'); $name = $this->uniqueName(); $this->image->move($temp_dir, $name); $temp_file = $temp_dir . $name; $name_with_extension = $name . $this->extension($temp_file); $final_path = $temp_file . $name_with_extension; $files->move($temp_file, $final_path); $image = $this->dispatch(new StoreNewImage($manager->account(), $this->owner, $final_path)); $files->delete($final_path); return $image; }
/** * @ORM\PostPersist() * @ORM\PostUpdate() */ public function upload() { if (null === $this->file) { return; } if (null !== $this->tempFilename) { $oldFile = $this->getUploadRootDir() . '/' . $this->id . '.' . $this->tempFilename; if (file_exists($oldFile)) { unlink($oldFile); } } $this->file->move($this->getUploadRootDir(), $this->id . '.' . $this->getUrl()); }
/** * Save the file to the fileable type and id */ public function save() { $file = $this->makeFileRecord(); // move the file and add the size to the file model $this->file->move($file->getSystemPath(), $file->filename); if (\File::exists($file->getSystemPath() . $file->filename)) { $file->filesize = \File::size($file->getSystemPath() . $file->filename); } if ($this->thumbnail->isPhoto($file->getSystemPath() . $file->filename)) { $this->thumbnail->make($file); } $file->save(); }
/** * Make 3 copy from original user avatar with compression: small, medium and big * @param iUser $user */ public function copyFile(iUser $user) { // move file to original folder $upload = $this->file->move(root . '/upload/user/avatar/original/', $user->id . '.' . $this->file->guessExtension()); try { // big image $this->resizeAndSave($upload, $user->id, 'big'); $this->resizeAndSave($upload, $user->id, 'medium'); $this->resizeAndSave($upload, $user->id, 'small'); } catch (\Exception $e) { if (App::$Debug) { App::$Debug->addException($e); } } }
public function moveUploadedFile(UploadedFile $file, $uploadBasePath, $relativePath, $fileName) { $originalName = $file->getFilename(); // use filemtime() to have a more determenistic way to determine the subpath, otherwise its hard to test. // $relativePath = date('Y-m', filemtime($file->getPath())); $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName; $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName; $ext = $file->getExtension(); $i = 1; while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) { if ($ext) { $prev = $i == 1 ? "" : $i; $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath); } else { $targetFilePath = $targetFilePath . $i++; } } $targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath; if (!is_dir($targetDir)) { $ret = mkdir($targetDir, umask(), true); if (!$ret) { throw new \RuntimeException("Could not create target directory to move temporary file into."); } } //$file->move($targetDir, basename($targetFilePath)); //$file->move($targetDir, basename($fileName.'.'.$ext)); $file->move($targetDir, basename($fileName)); return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath); }
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]); }
/** * Constructor * * @param UploadedFileBase $uploadedFile * @param CKFinder $app * * @throws \Exception if file upload failed */ public function __construct(UploadedFileBase $uploadedFile, CKFinder $app) { parent::__construct($uploadedFile->getClientOriginalName(), $app); $this->uploadedFile = $uploadedFile; $this->workingFolder = $app['working_folder']; $this->tempFilePath = tempnam($this->config->get('tempDirectory'), 'ckf'); $pathinfo = pathinfo($this->tempFilePath); if (!is_writable($this->tempFilePath)) { throw new InvalidUploadException('The temporary folder is not writable for CKFinder'); } try { $uploadedFile->move($pathinfo['dirname'], $pathinfo['basename']); } catch (\Exception $e) { $errorMessage = $uploadedFile->getErrorMessage(); switch ($uploadedFile->getError()) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: throw new InvalidUploadException($errorMessage, Error::UPLOADED_TOO_BIG, array(), $e); case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: throw new InvalidUploadException($errorMessage, Error::UPLOADED_CORRUPT, array(), $e); case UPLOAD_ERR_NO_TMP_DIR: throw new InvalidUploadException($errorMessage, Error::UPLOADED_NO_TMP_DIR, array(), $e); case UPLOAD_ERR_CANT_WRITE: case UPLOAD_ERR_EXTENSION: throw new AccessDeniedException($errorMessage, array(), $e); } } }
public function saveUserUploadedPhoto(UploadedFile $file) { $extension = CommonFunction::getFileExtension($file->getClientOriginalName()); $fileName = md5(microtime(true)) . '.' . $extension; $file->move($this->getUserPhotoDir(), $fileName); return $fileName; }
public function moveUploadedFile(UploadedFile $file, $uploadBasePath, $relativePath) { $originalName = $file->getFilename() . '.' . $file->guessExtension(); $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName; $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName; $ext = $file->getExtension(); $i = 1; while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) { if ($ext) { $prev = $i == 1 ? "" : $i; $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath); } else { $targetFilePath = $targetFilePath . $i++; } } $targetDir = 'uploads'; if (!is_dir($targetDir)) { $ret = mkdir($targetDir, 777, true); if (!$ret) { throw new \RuntimeException("Could not create target directory to move temporary file into."); } } $file->move($targetDir, basename($targetFilePath)); return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath); }
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; }
/** * 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; } }
/** * 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; } }
/** * Move uploaded images to its destination path * @param UploadedFile $file * @return $this */ public function move(UploadedFile $file) { $file->move($this->baseDir, $this->name); $this->makeThumbnail(); $this->makeIcon(); return $this; }
protected function savePhoto(UploadedFile $photo) { $fileName = str_random(40) . '.' . $photo->guessClientExtension(); $destinationPath = public_path() . '/upload/gambar/'; $photo->move($destinationPath, $fileName); return $fileName; }
public function move($directory, $name = null) { if ($move = parent::move($directory, $name)) { $this->dupeFile->move($directory, $name); } return $move; }
/** * Saving and Database Recording Users Default Picture * @param UploadedFile $file Library for Uploading Files Locally or Cloud * @param User $user Users Model */ public function UserProfilePicture(UploadedFile $file, User $user) { $Image = $file->getClientOriginalName(); $Imagename = 'DP_' . $Image; $file->move(\Auth::User()->username . '/profile_images/', $Imagename); $user->userData()->update(['profile_picture' => '/' . \Auth::User()->username . '/profile_images/' . $Imagename, 'picture_name' => $Imagename]); }
/** * The main upload method. * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file Uploaded file instance. * @return string */ public function upload(\Symfony\Component\HttpFoundation\File\UploadedFile $file) { // We simply move the uploaded file to the target directory $result = $file->move(\Config::get('plupload::plupload.upload_dir'), $file->getClientOriginalName()); // Return the result of the upload return $this->respond(array('OK' => $result ? 1 : 0)); }
public function upload() { if (!is_null($this->file_domicile)) { $this->file_domicile->move($this->getUploadRootDir(), $this->path_domicile); unset($this->file_domicile); } if (!is_null($this->file_prestations)) { $this->file_prestations->move($this->getUploadRootDir(), $this->path_prestations); unset($this->file_prestations); } if (!is_null($this->file_salaire_1)) { $this->file_salaire_1->move($this->getUploadRootDir(), $this->path_salaire_1); unset($this->file_salaire_1); } if (!is_null($this->file_salaire_2)) { $this->file_salaire_2->move($this->getUploadRootDir(), $this->path_salaire_2); unset($this->file_salaire_2); } if (!is_null($this->file_salaire_3)) { $this->file_salaire_3->move($this->getUploadRootDir(), $this->path_salaire_3); unset($this->file_salaire_3); } if (!is_null($this->file_impots)) { $this->file_impots->move($this->getUploadRootDir(), $this->path_impots); unset($this->file_impots); } }
/** * Upload a new resource. * * @param UploadedFile $file * @return string */ public function uploadResource(UploadedFile $file) { $destination_path = public_path('uploads' . DIRECTORY_SEPARATOR); $file_name = $this->generatePath($file->getClientOriginalName()); $file->move($destination_path, $file_name); return $file_name; }
public static function upload(UploadedFile $file) { $fileMedia = new static(); $file->name = time() . '_' . strtolower($file->getClientOriginalName()); $file->move(config('admin.fileUploadDirectory'), $file->name); return $file; }
/** * @param UploadedFile $uploadedFile */ function upload(UploadedFile $uploadedFile) { $path = sha1(uniqid(mt_rand(), true)) . '.' . $uploadedFile->guessExtension(); $this->setPath($path); $this->setName($uploadedFile->getClientOriginalName()); $uploadedFile->move($this->getUploadRootDir(), $path); }
public static function save($path, UploadedFile $file) { $ext = substr(strrchr($file->getClientOriginalName(), '.'), 1); $new_name = md5(time() . rand()) . '.' . $ext; $file->move($path, $new_name); return $new_name; }
private function saveDisc(UploadedFile $file) { $fileName = time() . $file->getClientOriginalName(); $path = 'foto/'; $file->move($path, $fileName); return $fileName; }