我正在使用捆绑包上传任何类型的文件 为此,我使用以下文件创建了一个新的包 uploadBundle :
<?php
namespace SisEvo\UploadBundle\Entity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Archivo {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $idTipo;
/**
* @ORM\Column(type="integer")
*/
private $idAgregado;
/**
* @var File
*
* @Assert\File(
* maxSize = "50M",
* mimeTypes = {"application/pdf", "application/x-pdf, application/x-rar-compressed, application/octet-stream, application/csv, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/msword, application/vnd.ms-excel"},
* maxSizeMessage = "El arxiu es massa gran",
* mimeTypesMessage = "El tipus d'arxiu seleccionat no està permitit"
* )
*/
protected $file;
此外,此实体包含get
和set
所需的方法,最后是上传方法,如下所示:
public function upload($file, $path = '%kernel.root_dir%/../web/uploads') {
if (null === $file) {
return;
}
$name = $file->getClientOriginalName();
$file->move(
$path, $name
);
unset($file);
}
<?php
namespace SisEvo\UploadBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
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'
));
}
}
class UploadController extends Controller {
/**
* @Route("/file_upload/" , name = "file_upload")
*/
public function probarAction() {
$request = $this->getRequest();
$file = new Archivo();
$formulario = $this->createForm(new ArchivoType());
$formulario->handleRequest($request);
if ($formulario->isValid()) {
$em = $this->getDoctrine()->getManager();
$data = $formulario->getData();
$fileUploaded = $data->getFile();
$file->upload($fileUploaded);
echo "SUCCESS!";
}
return $this->render("upload/prova.html.twig"
, array("formulario" => $formulario->createView())
);
}
}
如果文件夹/uploads
不存在,则此代码可以正常工作;这是我第一次使用它,但以下时间Symfony显示此错误:
无法创建“%kernel.root_dir%/ .. / web / uploads”目录
错误发生在vendor / symfony / symfony / src / Symfony / Component / HttpFoundation / File / File.php第110行的这段代码中
if (!is_dir($directory)) {
if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
}
} elseif (!is_writable($directory)) {
throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
似乎Symfony找不到uploads
个文件夹。
我试图用chmod 777 -R改变permisions,但问题仍然存在。
有什么想法解决这个问题?
在此先感谢:)
使用更多信息进行编辑
第一
我正在创建一个服务来从我的应用程序的任何部分上传文件,所以我创建了一个包含实体和简单表单的包。上面的控制器帖子只是一个证明,当uploadBundle
完成后,该控制器将被删除,上传方法将用于我需要它的应用程序的每个部分。
此外,尚未开发出将每个上载文件的信息存储在数据库中的代码。
拜托,你能解释一下为什么我的建筑错了吗?谢谢:))
第二
Chmod
从symfony内核获取八进制777; 问题部分中发布的代码来自symfony。
第三
你是对的,代码不能像我预期的那样工作。它在web文件夹中创建一个文件夹,如下所示:web/web/uploads
。我需要在Symfony上获得有关内部文件夹的更多信息。无论如何,@ pablo的回答解决了我的问题。
答案 0 :(得分:3)
你获取$ path(kernel.root_dir)的方式是错误的
尝试这种方法:
* {
box-sizing: border-box;
}
.row {
display: flex;
flex-direction: row;
}
.row span {
flex: 1;
display: block;
}
.row span input {
width: 100%;
margin-left: 5px;
border: none;
border-bottom: 1px solid #000;
background: transparent;
outline: none !important;
}
如果您有一些进展,请告知我们
问候
添加了基于@Isaac评论的文档链接:
获得$ path的更有效方法:
public function probarAction() {
$request = $this->getRequest();
$file = new Archivo();
$formulario = $this->createForm(new ArchivoType());
$formulario->handleRequest($request);
if ($formulario->isValid()) {
$em = $this->getDoctrine()->getManager();
$data = $formulario->getData();
$fileUploaded = $data->getFile();
$path = $this->get('kernel')->getRootDir() . '/../web/uploads';
$file->upload($fileUploaded, $path);
echo "SUCCESS!";
}
return $this->render("upload/prova.html.twig"
, array("formulario" => $formulario->createView())
);
}