當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。