當前位置: 首頁>>代碼示例>>PHP>>正文


PHP File::guessClientExtension方法代碼示例

本文整理匯總了PHP中Symfony\Component\HttpFoundation\File\File::guessClientExtension方法的典型用法代碼示例。如果您正苦於以下問題:PHP File::guessClientExtension方法的具體用法?PHP File::guessClientExtension怎麽用?PHP File::guessClientExtension使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\Component\HttpFoundation\File\File的用法示例。


在下文中一共展示了File::guessClientExtension方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的Symfony\Component\HttpFoundation\File\File::guessClientExtension方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。