gpt4 book ai didi

php - 在 Symfony 4 中上传图片

转载 作者:行者123 更新时间:2023-12-04 13:54:37 24 4
gpt4 key购买 nike

我正在尝试使用本教程在我的 Symfony 项目中上传文件: http://symfony.com/doc/current/controller/upload_file.html

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,
));
}
}

我的 Controller :

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());
}
}

我完全按照教程中的步骤操作,但图像似乎没有上传到我的文件夹中

最佳答案

我有同样的问题,我发现这是权限的问题,如果其他人卡在该部分,请将该文件夹授予所有权限,然后重试。

希望对您有所帮助。

关于php - 在 Symfony 4 中上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com