2015-10-14 29 views
2

我試圖與文件字段形式上傳的視頻和音頻文件上傳視頻。 我可以上傳音頻文件,但不能上傳視頻。 當我上傳MP3文件的擴展名是.mpga而不是.mp3。不能在symfony2.7

這裏是我的代碼,(我autorized所有類型的文件,以避免與MIME類型problemes):

 $product = new Bfile(); 
     $form_upload = $this->createForm(new BfileType(), $product); 
     $form_upload->handleRequest($request); 

     if ($form_upload->isValid()) { 
      // $file stores the uploaded PDF file 
      /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ 

      $file = $product->getBrochure(); 

      // Generate a unique name for the file before saving it 
      $fileName = md5(uniqid()).'.'.$file->guessExtension(); 

      // Move the file to the directory where brochures are stored 
      $brochuresDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads'; 
      $file->move($brochuresDir, $fileName); 

      // instead of its contents 
      $product->setBrochure($fileName); 

      // ... persist the $product variable or any other work 

      return $this->render(...); 
     } 

我的表格:

<?php 

namespace RS\VideosBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class BfileType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('brochure', 'file') 
     ; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'RS\VideosBundle\Entity\Bfile' 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'rs_videosbundle_bfile'; 
    } 
} 

我的實體:

<?php 

namespace RS\VideosBundle\Entity; 

use Symfony\Component\Validator\Constraints as Assert; 

/** 
* Bfile 
*/ 
class Bfile 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @var string 
    * @Assert\File(maxSize ="1000M") 
    */ 
    private $brochure; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set brochure 
    * 
    * @param string $brochure 
    * 
    * @return Bfile 
    */ 
    public function setBrochure($brochure) 
    { 
     $this->brochure = $brochure; 

     return $this; 
    } 

    /** 
    * Get brochure 
    * 
    * @return string 
    */ 
    public function getBrochure() 
    { 
     return $this->brochure; 
    } 
} 

我沒有錯誤但是我的上傳文件夾中沒有視頻文件。

謝謝。

+0

你有沒有在你的模板任何形式的錯誤? – jahller

+0

嗨,Jahller,沒有,我沒有任何錯誤 –

回答

5

我會說,有多個事情看起來很奇怪:

1 - 對PHP/web服務器

  • 文件上傳限制爲您在php.ini中看看自己的極限爲 upload_max_filesize = xxMpost_max_size = xxM
  • 檢查你的Web服務器配置阿帕奇/ nginx的 限制nginx的其client_max_body_size 8m; 阿帕奇應該使用的php.ini值。

2 - 文件擴展名問題

  • 這souldn't是問題的主要原因,但
  • 與擴展的問題很可能是因爲extensionGuesser例如。這條線$fileName = md5(uniqid()).'.'.$file->guessExtension();這將通過它的MimeType與擴展名的文件重命名 - 這不要緊,你的文件名是什麼,喜歡的test.txt - 當它的MIME類型是MPEG三層,將其更改爲MP3。

從猜測器類:

/** 
* Returns the extension based on the mime type. 
* 
* If the mime type is unknown, returns null. 
* 
* This method uses the mime type as guessed by getMimeType() 
* to guess the file extension. 
* 
* @return string|null The guessed extension or null if it cannot be guessed 
* 
* @api 
* 
* @see ExtensionGuesser 
* @see getMimeType() 
*/ 
public function guessExtension() 
{ 
    $type = $this->getMimeType(); 
    $guesser = ExtensionGuesser::getInstance(); 

    return $guesser->guess($type); 
} 

3 - 嘗試刪除驗證

  • 只是嘗試了調試運行的緣故。
+0

嗨Nakashu ,,在php.ini很好配置,我沒有probleme大小爲<= 1M,但還是有probleme大尺寸的視頻和音頻,,,當我嘗試上傳大小大於500M的視頻時,我的電腦被阻止,我不明白爲什麼。 –