Php 在Symfony 4中上载图像
我正在尝试使用本教程在我的Symfony项目中上载文件: Symfony没有显示错误,但是当我上传文件时,什么也没有发生,在我的数据库中,我没有使用Php 在Symfony 4中上载图像,php,image,symfony,upload,symfony4,Php,Image,Symfony,Upload,Symfony4,我正在尝试使用本教程在我的Symfony项目中上载文件: Symfony没有显示错误,但是当我上传文件时,什么也没有发生,在我的数据库中,我没有使用filename.jpg而是使用了这行C:\wamp64\tmp\php9294.tmp 这是我的实体: namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony
filename.jpg
而是使用了这行C:\wamp64\tmp\php9294.tmp
这是我的实体:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Entity(repositoryClass="App\Repository\ItemRepository")
*/
class Item
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank(message="Upload your image")
* @Assert\File(mimeTypes={ "image/png", "image/jpeg" })
*/
private $image;
public function getImage()
{
return $this->image;
}
public function setImage($image)
{
$this->image = $image;
return $this;
}
我的表格:
namespace App\Form;
use App\Entity\Item;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
class ItemType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('image',FileType::class,array('data_class'=> null, 'label' => 'Image'))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Item::class,
));
}
}
我的控制器:
namespace App\Controller;
use App\Entity\Item;
use App\Form\ItemType;
use App\Repository\ItemRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/item")
*/
class ItemController extends Controller
{
/**
* @Route("/", name="item_index", methods="GET")
*/
/**
* @Route("/new", name="item_new", methods="GET|POST")
*/
public function new(Request $request): Response
{
$item = new Item();
$form = $this->createForm(ItemType::class, $item);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $item->getImage();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
// moves the file to the directory where brochures are stored
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
$item->setImage($fileName);
return $this->redirectToRoute('item_index');
}
return $this->render('item/new.html.twig', [
'item' => $item,
'form' => $form->createView(),
]);
}
/**
* @return string
*/
private function generateUniqueFileName()
{
// md5() reduces the similarity of the file names generated by
// uniqid(), which is based on timestamps
return md5(uniqid());
}
}
我完全按照教程中所述的步骤操作,但图像似乎没有上载到我的文件夹中我建议您使用StofOctrineExtensionsBundle: 可上传的扩展名是为你,是帮助你上传文件与一些服务 首先,需要它:
composer需要stof/doctrine扩展包
现在,在文件
config/packages/stof_doctor_extensions.yml
中对其进行如下配置(考虑到public/uploads文件夹是通过读/写权限创建的):
现在,创建实体/File.php
以供使用:
<?php
namespace App\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Overblog\GraphQLBundle\Annotation\GraphQLColumn;
/**
* @ORM\Entity
* @Gedmo\Uploadable(filenameGenerator="SHA1", allowOverwrite=true, appendNumber=true)
*/
class File
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\Column(name="path", type="string")
* @Gedmo\UploadableFilePath
*/
protected $path;
/**
* @ORM\Column(name="name", type="string")
* @Gedmo\UploadableFileName
*/
protected $name;
/**
* @ORM\Column(name="mime_type", type="string")
* @Gedmo\UploadableFileMimeType
*/
protected $mimeType;
/**
* @ORM\Column(name="size", type="decimal")
* @Gedmo\UploadableFileSize
*/
protected $size;
/**
* @GraphQLColumn(type="String")
*/
protected $publicPath;
/**
* @return string
*/
public function getPublicPath()
{
return '/uploads/'.$this->name;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* @param mixed $path
*
* @return File
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*
* @return File
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return mixed
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* @param mixed $mimeType
*
* @return File
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* @return mixed
*/
public function getSize()
{
return $this->size;
}
/**
* @param mixed $size
*
* @return File
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
}
我也遇到了同样的问题,我发现这是一个权限问题,如果其他人卡在部件中,请授予文件夹所有权限,然后重试
希望能有所帮助。您是否在services.yaml?@DirkJ.Faber中设置了参数brockhures\u directory
,是的,在这里,我这样设置:brockhures\u directory:'%kernel.project\u dir%/img'
可能与您的img
文件夹的权限有关?如果您想访问该图像,我还认为您希望将其放入文件夹public
。该表单有效吗?@DirkJ.Faber我更改了文件夹并更新了权限,现在可以使用了,谢谢!
<?php
namespace App\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Overblog\GraphQLBundle\Annotation\GraphQLColumn;
/**
* @ORM\Entity
* @Gedmo\Uploadable(filenameGenerator="SHA1", allowOverwrite=true, appendNumber=true)
*/
class File
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\Column(name="path", type="string")
* @Gedmo\UploadableFilePath
*/
protected $path;
/**
* @ORM\Column(name="name", type="string")
* @Gedmo\UploadableFileName
*/
protected $name;
/**
* @ORM\Column(name="mime_type", type="string")
* @Gedmo\UploadableFileMimeType
*/
protected $mimeType;
/**
* @ORM\Column(name="size", type="decimal")
* @Gedmo\UploadableFileSize
*/
protected $size;
/**
* @GraphQLColumn(type="String")
*/
protected $publicPath;
/**
* @return string
*/
public function getPublicPath()
{
return '/uploads/'.$this->name;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* @param mixed $path
*
* @return File
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*
* @return File
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return mixed
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* @param mixed $mimeType
*
* @return File
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* @return mixed
*/
public function getSize()
{
return $this->size;
}
/**
* @param mixed $size
*
* @return File
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
}
<?php
public function uploadFile(\Stof\DoctrineExtensionsBundle\Uploadable\UploadableManager $uploadableManager)
{
$uploadableManager->markEntityToUpload($file, $fileInfo);
}