我在控制器中使用了以下代码,以获取上传文件的文件名 我的控制器是
class uploadController extends Controller
{
public function uploadAction(Request $request)
{
$id= $_GET['id'];
$user = new attachments();
$form = $this->createFormBuilder($user)->add('files','file',array("data_class" => null,"attr"=>array("multiple" =>"multiple",)))->getForm();
$formView = $form->createView();
$formView->getChild('files')->set('full_name','files[]');
if ($request->getMethod() == 'POST')
{
$em = $this->getDoctrine()->getManager();
$data = $form["files"]->getData();
}
}
当我打印$ data时,它没有给出上传文件的文件名,而是返回空值
我的实体是:
use Symfony\Component\HttpFoundation\File\UploadedFile;
class attachments
{
private $id;
/**
* @var integer
* @ORM\Column(name="user", type="integer", nullable=false)
* @ORM\ManyToOne(targetEntity="users", inversedBy="annotations")
*/
protected $userId;
/**
* @var string
*
* @Assert\File(maxSize="6000000")
* @ORM\Column(name="files", type="array", length=255, nullable=true)
*/
public $files=array();
public function __construct()
{
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* @param integer $userId
* @return attachments
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Set files
* @param object $files
*
* @return attachments
*/
public function setFiles($files)
{
$this->files = $files;
}
/**
* Get files
*
* @return object
*/
public function getFiles()
{
return $this->files;
}
public function uploadFiles()
{
// the files property can be empty if the field is not required
if (null === $this->files)
{
return;
}
else
{
$this->files->move($this->getUploadRootDir(), $this->files->getClientOriginalName());
}
$this->setFiles($this->files->getClientOriginalName());
}
/**
* Get userId
*
* @return integer
*/
public function getUserId()
{
return $this->userId;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path;
}
protected function getUploadRootDir()
{
return __DIR__ . '/../../../../web/'. $this->getUploadDir();
}
protected function getUploadDir()
{
return 'uploads/';
}
}
答案 0 :(得分:1)
Symfony2中的上传文件类型为Symfony/Component/HttpFoundation/File/UploadedFile。
您可以获取原始客户端名称(php会在将文件放入php_upload_tmp_dir时重命名文件):
$file->getClientOriginalName();
... move将文件发送到新位置:
$file->move('path/to/your_file', 'new_name.jpg');
您不能将断言文件约束用于数组。
* @Assert\File(maxSize="6000000")
*/
protected $files = array();
因此,您需要All约束。
此外,您不能只在数组或集合上调用move方法......您将不得不遍历集合/数组。
$this->files->move('..') // this is never going to work...
使用数组集合并为上传的文件创建属性,如果这就是你想要的。
protected $files;
protected $uploadedFiles;
public function __construct()
{
$this->files = new ArrayCollection;
$this->uploadedFiles = new Array();
}
如果要将UploadedFile实体的Doctrine Collection转换为Array,请执行以下操作:
$collection = $entity->getFiles();
$array = $collection->toArray();
但无论你想做什么......最好使用OOP而不是你在这里尝试的数组。