当前位置: 首页>>代码示例>>PHP>>正文


PHP File\UploadedFile类代码示例

本文整理汇总了PHP中Symfony\Component\HttpFoundation\File\UploadedFile的典型用法代码示例。如果您正苦于以下问题:PHP UploadedFile类的具体用法?PHP UploadedFile怎么用?PHP UploadedFile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了UploadedFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: handleUploadedFile

 /**
  * 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;
 }
开发者ID:splot,项目名称:framework-extra-module,代码行数:44,代码来源:SimpleStorage.php

示例2: convertFieldValueFromForm

 /**
  * {@inheritdoc}
  *
  * @param null|\Symfony\Component\HttpFoundation\File\UploadedFile $data
  */
 public function convertFieldValueFromForm($data)
 {
     if ($data === null) {
         return null;
     }
     return array("inputUri" => $data->getRealPath(), "fileName" => $data->getClientOriginalName(), "fileSize" => $data->getSize());
 }
开发者ID:eab-dev,项目名称:NetgenEzFormsBundle,代码行数:12,代码来源:Image.php

示例3: saveFile

 /**
  * {@inheritdoc}
  */
 public function saveFile(UploadedFile $uploadedFile, $fileName)
 {
     $stream = fopen($uploadedFile->getRealPath(), 'r+');
     $result = $this->filesystem->writeStream($this->getMediaBasePath() . '/' . $fileName . '.' . $uploadedFile->guessClientExtension(), $stream);
     fclose($stream);
     return $result;
 }
开发者ID:superdesk,项目名称:web-publisher,代码行数:10,代码来源:MediaManager.php

示例4: putUploadedFile

 /**
  * 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;
     }
 }
开发者ID:arooth,项目名称:FileStorage,代码行数:31,代码来源:FileStorage.php

示例5: saveUserUploadedPhoto

 public function saveUserUploadedPhoto(UploadedFile $file)
 {
     $extension = CommonFunction::getFileExtension($file->getClientOriginalName());
     $fileName = md5(microtime(true)) . '.' . $extension;
     $file->move($this->getUserPhotoDir(), $fileName);
     return $fileName;
 }
开发者ID:tesmen,项目名称:velo.local,代码行数:7,代码来源:FileWorker.php

示例6: updateUserProfileImage

 /**
  * 회원의 프로필 이미지를 등록한다.
  *
  * @param UserInterface $user        프로필 이미지를 등록할 회원
  * @param UploadedFile  $profileFile 프로필 이미지 파일
  *
  * @return string 등록한 프로필이미지 ID
  */
 public function updateUserProfileImage(UserInterface $user, UploadedFile $profileFile)
 {
     $disk = array_get($this->profileImgConfig, 'storage.disk');
     $path = array_get($this->profileImgConfig, 'storage.path');
     $size = array_get($this->profileImgConfig, 'size');
     // make fitted image
     /** @var ImageManager $imageManager */
     $imageManager = call_user_func($this->imageManagerResolver);
     $image = $imageManager->make($profileFile->getRealPath());
     $image = $image->fit($size['width'], $size['height']);
     // remove old profile image
     if (!empty($user->profileImageId)) {
         $file = File::find($user->profileImageId);
         if ($file !== null) {
             try {
                 $this->storage->remove($file);
             } catch (\Exception $e) {
             }
         }
     }
     // save image to storage
     $id = $user->getId();
     $file = $this->storage->create($image->encode()->getEncoded(), $path . "/{$id}", $id, $disk);
     return $file->id;
 }
开发者ID:xpressengine,项目名称:xpressengine,代码行数:33,代码来源:UserImageHandler.php

示例7: moveUploadedFile

 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);
 }
开发者ID:alangalipaud,项目名称:ProjetQCM,代码行数:29,代码来源:UploadFileMover.php

示例8: getFileMd5Name

 public function getFileMd5Name(UploadedFile $file)
 {
     $ext = $file->guessExtension();
     $md5 = md5_file($file->getRealPath());
     $name = $md5 . '.' . $ext;
     return $name;
 }
开发者ID:wucdbm,项目名称:gallery-bundle,代码行数:7,代码来源:ImageManager.php

示例9: upload

 /**
  * @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);
 }
开发者ID:alexseif,项目名称:miniERP,代码行数:10,代码来源:OrderDocuments.php

示例10: isFileUpload

 /**
  * @param string|array|UploadedFile $data
  *
  * @return bool
  */
 protected function isFileUpload($data)
 {
     if ($data instanceof UploadedFile) {
         return $data->isValid() && $data->getClientSize() > 0;
     }
     return is_array($data) && !empty($data['tmp_name']) && !empty($data['size']) && $data['error'] === UPLOAD_ERR_OK;
 }
开发者ID:acp3,项目名称:core,代码行数:12,代码来源:FileUploadValidationRule.php

示例11: saveDisc

 private function saveDisc(UploadedFile $file)
 {
     $fileName = time() . $file->getClientOriginalName();
     $path = 'foto/';
     $file->move($path, $fileName);
     return $fileName;
 }
开发者ID:tiagoheineck,项目名称:puBrick,代码行数:7,代码来源:Foto.php

示例12: upload

 /**
  * 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));
 }
开发者ID:dfdenni,项目名称:plupload,代码行数:13,代码来源:UploadHandler.php

示例13: parseUserFile

 /**
  * 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;
 }
开发者ID:pr0coder,项目名称:Inkstand,代码行数:33,代码来源:UserService.php

示例14: move

 /**
  * 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;
 }
开发者ID:amazingapp,项目名称:rbb.app,代码行数:12,代码来源:Aavatar.php

示例15: savePhoto

 protected function savePhoto(UploadedFile $photo)
 {
     $fileName = str_random(40) . '.' . $photo->guessClientExtension();
     $destinationPath = public_path() . '/upload/gambar/';
     $photo->move($destinationPath, $fileName);
     return $fileName;
 }
开发者ID:cakraamin,项目名称:toko,代码行数:7,代码来源:KamiController.php


注:本文中的Symfony\Component\HttpFoundation\File\UploadedFile类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。