Php 上载图像时出错:调用成员函数扩展名
我有一个实体代理与实体照片Php 上载图像时出错:调用成员函数扩展名,php,symfony,image-uploading,Php,Symfony,Image Uploading,我有一个实体代理与实体照片 <?php namespace Project\DashboardBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * Photo * * @ORM\Table() * @ORM\Entity(repositoryClass="Project\DashboardBundle\Entity\Pho
<?php
namespace Project\DashboardBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Photo
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Project\DashboardBundle\Entity\PhotoRepository")
* @ORM\HasLifecycleCallbacks
*/
class Photo
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
/**
* @Assert\File(maxSize="1M")
*/
public $file;
public function setFile($file)
{
$this->file = $file;
if (null !== $this->url) {
$this->tempFilename = $this->url;
$this->url = null;
$this->alt = null;
}
}
public function getFile()
{
return $this->file;
}
private $tempFilename;
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null === $this->file) {
return;
}
$this->url = $this->file->guessExtension();
$this->alt = $this->file->getClientOriginalName();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
if (null !== $this->tempFilename) {
$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
if (file_exists($oldFile)) {
unlink($oldFile);
}
}
$this->file->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->url
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->tempFilename)) {
unlink($this->tempFilename);
}
}
public function getUploadDir()
{
return 'uploads/img';
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getId().'.'.$this->getUrl();
}
代理机构
class Agence
{
/**
* @ORM\OneToOne(targetEntity="Project\DashboardBundle\Entity\Photo", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $photo;
这是实体照片的简介
<?php
namespace Project\DashboardBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Photo
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Project\DashboardBundle\Entity\PhotoRepository")
* @ORM\HasLifecycleCallbacks
*/
class Photo
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
/**
* @Assert\File(maxSize="1M")
*/
public $file;
public function setFile($file)
{
$this->file = $file;
if (null !== $this->url) {
$this->tempFilename = $this->url;
$this->url = null;
$this->alt = null;
}
}
public function getFile()
{
return $this->file;
}
private $tempFilename;
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null === $this->file) {
return;
}
$this->url = $this->file->guessExtension();
$this->alt = $this->file->getClientOriginalName();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
if (null !== $this->tempFilename) {
$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
if (file_exists($oldFile)) {
unlink($oldFile);
}
}
$this->file->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->url
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->tempFilename)) {
unlink($this->tempFilename);
}
}
public function getUploadDir()
{
return 'uploads/img';
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getId().'.'.$this->getUrl();
}
您只需
像那样插入到你的表单中
{{form_enctype(form)}
$this->file
不是null,而是其他内容,不是对象。添加使用Symfony\Component\HttpFoundation\File\UploadedFile
并生成公共函数setFile(UploadedFile$file)
以查看是否调用了setter并上载了参数文件。我得到以下错误:可捕获的致命错误:传递给Project\DashboardBundle\Entity\Photo::setFile()的参数1必须是Symfony\Component\HttpFoundation\file\UploadedFile的实例,给定字符串,在第438行的D:\wamp\www\agence\vendor\symfony\symfony\src\symfony\Component\PropertyAccess\PropertyAccessor.php中调用,并在D:\wamp\www\agence\src\Project\DashboardBundle\Entity\Photo.php第103行中定义。第103行是:公共函数setFile(UploadedFile$file)不可能,我忘了添加{{form_enctype(form)}}。谢谢,现在可以用了