我试图上传图片,但我不知道为什么我总是收到此错误消息。
错误:在非对象
上调用成员函数guessExtension()
这意味着我得到的对象是null,但为什么?
这是我的输入<input type="file" id="images_emp" name="images_emp" >
。
现在在控制器中我这样做了:
$pic = $request->files->get('images_emp');
$imagenom=$nom.$Cin.'.'.$pic->guessExtension();
$pic->move( $this->getParameter('Dossier_images'),imagenom);
$Employe->setImgsrc("/images/".$imagenom);
,这就是实体的样子。
/**
* @ORM\Column(type="string", nullable=true)
*
* @Assert\File(
* maxSize = "1024k",
* mimeTypes={ "application/png" ,"application/jpg","application/jpeg"},
* mimeTypesMessage = "Svp inserer une forme valide (png,jpg,jpeg)"
* )
*/private $imgsrc;
/**
* @return mixed
*/
public function getImgsrc()
{
return $this->imgsrc;
}
/**
* @param mixed $imgsrc
*/
public function setImgsrc($imgsrc)
{
$this->imgsrc = $imgsrc;
return $this;
}
我怎么能解决这个问题。
答案 0 :(得分:0)
我建议你使用实体中的事件非常简单,然后get()
实体图片
<?php
namespace ------------ ;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Images
*
* @ORM\Table(name="images")
* @ORM\Entity(repositoryClass="---------")
* @ORM\HasLifecycleCallbacks
*/
class Slides
{
/**
* @var int
*
* @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;
private $file;
//This attribute is added to store the temporary file name
private $tempFilename;
public function __construct()
{
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set url
*
* @param string $url
*
* @return Images
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
public function getFile()
{
return $this->file;
}
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// We check if we already had a file for this entity
if (null !== $this->url) {
// The file extension is saved to be deleted later
$this->tempFilename = $this->url;
// Reset the values of the url and alt attributes
$this->url = null;
$this->alt = null;
} }
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
// If there is no file (optional field), nothing is done
if (null === $this->file) {
return;
}
// The name of the file is its id, you just have to store its extension
// To make it clean, we should rename this attribute to "extension" instead of "url"
$this->url = $this->file->guessExtension();
// And we generate the alt attribute of the <img> tag, at the file name value on the user's PC
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
//If there is no file (optional field), nothing is done
if (null === $this->file) {
return;
}
//If we had an old file, we delete it
if (null !== $this->tempFilename) {
$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
if (file_exists($oldFile)) {
unlink($oldFile);
}
}
// We move the file sent in the directory of your choice
$this->file->move(
$this->getUploadRootDir(), // Le répertoire de destination
$this->id.'.'.$this->url // Le nom du fichier à créer, ici « id.extension »
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
// We temporarily save the file name because it depends on the id
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
// In PostRemove, we do not have access to the id, we use our saved name
if (file_exists($this->tempFilename)) {
// we delete the file
unlink($this->tempFilename);
}
}
public function getUploadDir()
{
// Returns the relative path to the image for a browser
return 'uploads';
}
protected function getUploadRootDir()
{
// We return the relative path to the image for our PHP code
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getId().'.'.$this->getUrl();
}
/**
* Set description
*
* @param string $alt
*
* @return Images
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt
}
}
在控制器中
$images = new Images();
$form = $this->get('form.factory')->create(ImagesType::class, $images);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($images);
$em->flush();
}
您可以在openclassroom更详细的
中找到此解决方案