1
我創建了一個實體與屬性附加傷害file
這樣的:如何上傳RAR,CSV,DOC,DOCX,XLS和XLSX文件到Symfony2的應用
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Archivo {
/**
* @var File
*
* @Assert\File(
* maxSize = "6M",
* mimeTypes = {"application/pdf", "application/x-pdf",
* "application/x-rar-compressed","application/octet-stream", "application/zip"},
* maxSizeMessage = "Symfony: File too big!",
* mimeTypesMessage = "Symfony: Invalid file type!"
*)
*/
protected $file;
}
我用這個方法來存儲文件:
public function upload($file, $path) {
if (null === $file) {
return;
}
$name = $file->getClientOriginalName();
$file->move(
$path, $name
);
unset($file);
}
這是我表:
class ArchivoType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('file', 'file')
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'SisEvo\UploadBundle\Entity\Archivo'
));
}
}
當我用RAR文件提交表單時,會顯示警告Symfony: Invalid file type!
。我該如何上傳RAR,CSV,DOC,DOCX,XLS和XLSX文件?
預先感謝您!
你確定你的RAR文件有 '應用程序/ x-RAR壓縮' MIME類型?它也可以是一些過時的老MIME類型,如「vnd.rar」 – Alsatian
也許這可以幫助你:http://stackoverflow.com/questions/1681424/how-to-create-a-csv-file-using-php -and-upload-it?rq = 1 –
對不起@MarcGiroux,我有不同的問題。不管怎樣,謝謝你 :) –