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


PHP File::getClientOriginalExtension方法代码示例

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


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

示例1: upload

 /**
  * $current_uri String actual uri of the file
  * $dest_folder String future uri of the file starting from web/upload folder
  * $lifetime DateTime lifetime of the file. If time goes over this limit, the file will be deleted.
  **/
 public function upload(File $file, $dest_folder = '', \DateTime $lifetime = null)
 {
     if ($file instanceof UploadedFile) {
         if ($file->getError() !== null && $file->getError() !== 0) {
             throw new UploadException($file->getErrorMessage());
         }
     }
     //preparing dir name
     $dest_folder = date('Ymd') . '/' . date('G') . '/' . $dest_folder;
     //checking mimetypes
     $mimeTypePassed = false;
     foreach ($this->allowedMimetypes as $mimeType) {
         if (preg_match('@' . $mimeType . '@', $file->getMimeType())) {
             $mimeTypePassed = true;
         }
     }
     if (!$mimeTypePassed) {
         throw new InvalidMimeTypeException('Only following filetypes are allowed : ' . implode(', ', $this->allowedMimetypes));
     }
     $fs = new Filesystem();
     if (!$fs->exists($this->uploadDir . $dest_folder)) {
         $fs->mkdir($this->uploadDir . $dest_folder);
     }
     $em = $this->entityManager;
     $media = new Media();
     $media->setMime($file->getMimeType());
     // If there's one, we try to generate a new name
     $extension = $file->getExtension();
     // Sanitizing the filename
     $slugify = new Slugify();
     if ($file instanceof UploadedFile) {
         if (empty($extension)) {
             $extension = $file->getClientOriginalExtension();
             if (empty($extension)) {
                 $extension = $file->guessClientExtension();
             }
         }
         $filename = $slugify->slugify(basename($file->getClientOriginalName(), $extension)) . '.' . $extension;
     } else {
         if (empty($extension)) {
             $extension = $file->guessClientExtension();
         }
         $filename = $slugify->slugify(basename($file->getFilename(), $extension)) . '.' . $extension;
     }
     // A media can have a lifetime and will be deleted with the cleanup function
     if (!empty($lifetime)) {
         $media->setLifetime($lifetime);
     }
     // Checking for a media with the same name
     $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($dest_folder . $filename);
     $mediaExists = count($mediaExists) > 0;
     if ($mediaExists === false) {
         $mediaExists = $fs->exists($this->uploadDir . $dest_folder . $filename);
     }
     if ($mediaExists === true) {
         $filename = basename($filename, '.' . $extension);
         $i = 1;
         do {
             $media->setName($filename . '-' . $i++ . '.' . $extension);
             $media->setUri($dest_folder . $media->getName());
             $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri());
             $mediaExists = count($mediaExists) > 0;
             if ($mediaExists === false) {
                 $mediaExists = $fs->exists($this->uploadDir . $dest_folder . $filename);
             }
         } while ($mediaExists === true);
     } else {
         $media->setName($filename);
         $media->setUri($dest_folder . $media->getName());
     }
     $file->move($this->uploadDir . $dest_folder, $media->getName());
     chmod($this->uploadDir . $dest_folder . $media->getName(), 0664);
     // Getting the salt defined in parameters.yml
     $secret = $this->container->getParameter('secret');
     $media->setSecretKey(hash('sha256', $secret . $media->getName() . $media->getUri()));
     $em->persist($media);
     $em->flush();
     return $media;
 }
开发者ID:alpixel,项目名称:AlpixelMediaBundle,代码行数:84,代码来源:MediaManager.php

示例2: getFileExtension

 public static function getFileExtension(File $file)
 {
     return $file instanceof UploadedFile ? $file->getClientOriginalExtension() : $file->getExtension();
 }
开发者ID:liugang19890719,项目名称:www.zesow.com,代码行数:4,代码来源:FileToolkit.php

示例3: uploadFile

 /**
  * @param File $file
  * @param User $user
  */
 private function uploadFile(File $file, User $user)
 {
     $filename = 'emergya-' . $user->getNick() . '.' . $file->getClientOriginalExtension();
     $file->move($this->getParameter('uploads_directory'), $filename);
     $user->setAvatar($filename);
 }
开发者ID:JuanLuisGarciaBorrego,项目名称:emergya,代码行数:10,代码来源:TablonController.php

示例4: createImage

 /**
  * Given a single File, assuming is an image, create a new
  * Image object containing all needed information.
  *
  * This method also persists and flush created entity
  *
  * @param File $file File where to get the image
  *
  * @return ImageInterface Image created
  *
  * @throws InvalidImageException File is not an image
  */
 public function createImage(File $file)
 {
     $fileMime = $file->getMimeType();
     if ('application/octet-stream' === $fileMime) {
         $imageSizeData = getimagesize($file->getPathname());
         $fileMime = $imageSizeData['mime'];
     }
     if (strpos($fileMime, 'image/') !== 0) {
         throw new InvalidImageException();
     }
     $extension = $file->getExtension();
     if (!$extension && $file instanceof UploadedFile) {
         $extension = $file->getClientOriginalExtension();
     }
     /**
      * @var ImageInterface $image
      */
     $image = $this->imageFactory->create();
     if (!isset($imageSizeData)) {
         $imageSizeData = getimagesize($file->getPathname());
     }
     $name = $file->getFilename();
     $image->setWidth($imageSizeData[0])->setHeight($imageSizeData[1])->setContentType($fileMime)->setSize($file->getSize())->setExtension($extension)->setName($name);
     return $image;
 }
开发者ID:pramoddas,项目名称:elcodi,代码行数:37,代码来源:ImageManager.php

示例5: extractDataFromFileInfo

 /**
  * @param FileInfo $info
  *
  * @return array
  */
 protected function extractDataFromFileInfo(FileInfo $info)
 {
     $data = [];
     if ($info instanceof UploadedFile) {
         $data['original_name'] = $info->getClientOriginalName();
         $data['original_extension'] = $info->getClientOriginalExtension();
     }
     return $data;
 }
开发者ID:nordsoftware,项目名称:lumen-file-manager,代码行数:14,代码来源:FileManager.php

示例6: upload

 /**
  * Upload a file, storing it in the temporary
  *
  * @param File  $file
  * @param Media $media
  *
  * @throws Exception\MediaFileDeniedException
  * @return Media
  */
 public function upload(File $file, Media $media = null)
 {
     if (!$media instanceof Media) {
         $media = new $this->class();
     }
     if ($file instanceof UploadedFile) {
         $extension = $file->getClientOriginalExtension();
         $oldName = $file->getClientOriginalName();
     } else {
         $extension = $file->getExtension();
         $oldName = $file->getFilename();
     }
     $fileMime = $file->getMimeType();
     if (array_key_exists($fileMime, $this->accept) && in_array(strtolower($extension), $this->accept[$fileMime])) {
         $newFileName = null;
         // if the media name is set, use it over a hashed one
         if ($media->getName()) {
             if ($media->getPath() != $file->getPathname()) {
                 $newFileName = $media->getName();
             }
         } else {
             $newFileName = md5(time() . rand()) . "." . $extension;
         }
         if ($newFileName) {
             $file = $file->move($this->storePath, $newFileName);
         }
     } else {
         throw new MediaFileDeniedException($fileMime);
     }
     if ($file instanceof File) {
         /** @var Media $media */
         if (!$media->getTitle()) {
             $media->setTitle($oldName);
         }
         $oldPath = pathinfo($oldName);
         if (!$media->getNameOriginal()) {
             $media->setNameOriginal($oldName);
         }
         if (!$media->getName()) {
             $media->setName($file->getFilename());
         }
         if (!$media->getAlt()) {
             $media->setAlt($oldPath['filename']);
         }
         if (!$media->getCredits()) {
             $media->setCredits($this->user->getUsername());
         }
         $media->setSrc($this->storeDirectory . $file->getFilename());
         $media->setPath($this->storePath . $file->getFilename());
         $this->optimiser->optimise($media);
         $media->setHash(sha1_file($file->getPathname()));
     }
     return $media;
 }
开发者ID:linestorm,项目名称:media-bundle,代码行数:63,代码来源:LocalStorageMediaProvider.php


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