Symfony 4.2 上传文件并重命名
Symfony 4.2 Uploaded Files and Renaming them
我正在构建的这个 Symfony 网站 api 需要您的一些帮助。长话短说,它使用 Symfony 之上的 API-Platform。根据文档,我添加了 post 文件的功能,但是我 运行 遇到的问题是冲突。我需要一种重命名文件的好方法。这些文件作为 UploadedFile 类.
那是说我知道移动功能并且我已经尝试过这个但是对于我当前的设置来说这不是一个好的选择。
控制器代码示例:
public function __invoke(Request $request): Images
{
$uploadedFile = $request->files->get('file');
if (!$uploadedFile) {
throw new BadRequestHttpException('"file" is required');
}
$mediaObject = new Images();
$mediaObject->file = $uploadedFile;
...
$this->validate($mediaObject, $request);
$em = $this->managerRegistry->getManager();
$em->persist($mediaObject);
$em->flush();
return $mediaObject;
}
图片实体:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use App\Controller\CreateMediaObjectAction;
/**
* @ApiResource(
* iri="http://schema.org/Images",
* collectionOperations={
* "post"={
* "controller"=CreateMediaObjectAction::class,
* "defaults"={
* "_api_receive"=false,
* },
* "validation_groups"={"Default", "media_object_create"},
* "swagger_context"={
* "consumes"={
* "multipart/form-data",
* },
* "parameters"={
* {
* "in"="formData",
* "name"="file",
* "type"="file",
* "description"="The file to upload",
* },
* },
* },
* },
* "get",
* },
* itemOperations={
* "get",
* },
* )
* @Vich\Uploadable
* @ORM\Entity(repositoryClass="App\Repository\ImagesRepository")
*/
class Images
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $dealer_id;
/**
* @var File|null
*
* @Assert\NotNull(groups={"media_object_create"})
* @Vich\UploadableField(mapping="images", fileNameProperty="image_path")
*/
public $file;
/**
* @ORM\Column(type="string", length=255)
*/
private $image_path;
/**
* @ORM\Column(type="datetime")
*/
private $created_on;
/**
* @ORM\Column(type="string", length=255)
*/
private $unique_id;
public function getFile()
{
return $this->file;
}
/**
* @param string $file
*
* @return Images
* @throws \Exception
*/
public function setFile(string $file): self
{
$this->file = $file;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getDealerId(): ?int
{
return $this->dealer_id;
}
public function setDealerId(int $dealer_id): self
{
$this->dealer_id = $dealer_id;
return $this;
}
public function getImagePath(): ?string
{
return $this->image_path;
}
public function setImagePath(string $image_path): self
{
$this->image_path = $image_path;
return $this;
}
public function getCreatedOn(): ?\DateTimeInterface
{
return $this->created_on;
}
public function setCreatedOn(\DateTimeInterface $created_on): self
{
$this->created_on = $created_on;
return $this;
}
public function getUniqueId(): ?string
{
return $this->unique_id;
}
public function setUniqueId(string $unique_id): self
{
$this->unique_id = $unique_id;
return $this;
}
}
所以问题是有没有一种好方法可以在不使用移动功能的情况下将文件 posted 重命名为 Symfony 应用程序?
你可以尝试rename()函数,但通常使用mov()。
此致!
找到适合我的解决方案:
https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/namers.md#file-namer
VichUp 具有自动重命名文件以避免冲突和目录规划的内置服务。
希望能帮到别人。
我正在构建的这个 Symfony 网站 api 需要您的一些帮助。长话短说,它使用 Symfony 之上的 API-Platform。根据文档,我添加了 post 文件的功能,但是我 运行 遇到的问题是冲突。我需要一种重命名文件的好方法。这些文件作为 UploadedFile 类.
那是说我知道移动功能并且我已经尝试过这个但是对于我当前的设置来说这不是一个好的选择。
控制器代码示例:
public function __invoke(Request $request): Images
{
$uploadedFile = $request->files->get('file');
if (!$uploadedFile) {
throw new BadRequestHttpException('"file" is required');
}
$mediaObject = new Images();
$mediaObject->file = $uploadedFile;
...
$this->validate($mediaObject, $request);
$em = $this->managerRegistry->getManager();
$em->persist($mediaObject);
$em->flush();
return $mediaObject;
}
图片实体:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use App\Controller\CreateMediaObjectAction;
/**
* @ApiResource(
* iri="http://schema.org/Images",
* collectionOperations={
* "post"={
* "controller"=CreateMediaObjectAction::class,
* "defaults"={
* "_api_receive"=false,
* },
* "validation_groups"={"Default", "media_object_create"},
* "swagger_context"={
* "consumes"={
* "multipart/form-data",
* },
* "parameters"={
* {
* "in"="formData",
* "name"="file",
* "type"="file",
* "description"="The file to upload",
* },
* },
* },
* },
* "get",
* },
* itemOperations={
* "get",
* },
* )
* @Vich\Uploadable
* @ORM\Entity(repositoryClass="App\Repository\ImagesRepository")
*/
class Images
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $dealer_id;
/**
* @var File|null
*
* @Assert\NotNull(groups={"media_object_create"})
* @Vich\UploadableField(mapping="images", fileNameProperty="image_path")
*/
public $file;
/**
* @ORM\Column(type="string", length=255)
*/
private $image_path;
/**
* @ORM\Column(type="datetime")
*/
private $created_on;
/**
* @ORM\Column(type="string", length=255)
*/
private $unique_id;
public function getFile()
{
return $this->file;
}
/**
* @param string $file
*
* @return Images
* @throws \Exception
*/
public function setFile(string $file): self
{
$this->file = $file;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getDealerId(): ?int
{
return $this->dealer_id;
}
public function setDealerId(int $dealer_id): self
{
$this->dealer_id = $dealer_id;
return $this;
}
public function getImagePath(): ?string
{
return $this->image_path;
}
public function setImagePath(string $image_path): self
{
$this->image_path = $image_path;
return $this;
}
public function getCreatedOn(): ?\DateTimeInterface
{
return $this->created_on;
}
public function setCreatedOn(\DateTimeInterface $created_on): self
{
$this->created_on = $created_on;
return $this;
}
public function getUniqueId(): ?string
{
return $this->unique_id;
}
public function setUniqueId(string $unique_id): self
{
$this->unique_id = $unique_id;
return $this;
}
}
所以问题是有没有一种好方法可以在不使用移动功能的情况下将文件 posted 重命名为 Symfony 应用程序?
你可以尝试rename()函数,但通常使用mov()。
此致!
找到适合我的解决方案: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/namers.md#file-namer
VichUp 具有自动重命名文件以避免冲突和目录规划的内置服务。
希望能帮到别人。