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;
}
}
我沒有錯誤但是我的上傳文件夾中沒有視頻文件。
謝謝。
你有沒有在你的模板任何形式的錯誤? – jahller
嗨,Jahller,沒有,我沒有任何錯誤 –