Symfony上传PDF文件后无法打开或删除的权限问题求助
Symfony 6.4.3 PDF上传后无管理员权限无法访问/删除问题
在Symfony 6.4.3应用中上传PDF文件时,通过UploadedFile可成功将PDF存储到指定目录public/pdf/,但无管理员权限的用户无法打开或删除这些PDF文件。应用中其他上传.json或.txt文件的目录不存在此问题。
代码片段
控制器代码(src/Controller/AwsExtractController.php)
<?php //src/Controller/AwsExtractController.php namespace App\Controller; use App\Entity\AwsPdfDocument; use App\Form\AwsPdfDocumentFormType; use App\Service\FileUploader; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Doctrine\ORM\EntityManagerInterface; class AwsExtractController extends AbstractController { public function __construct() {} /** * @Route("/documents/scan", name="scan_documents") * Fonction permettant de transférer les documents dans le * bucket S3, puis d'analyser ces documents avec AWS Textract. * * 3 Requêtes AWS * * @param Request $request * @param EntityManagerInterface $entityManager * * @return Response */ public function index(Request $request, EntityManagerInterface $entityManager, FileUploader $fileUploader) { $form = $this->createForm(AwsPdfDocumentFormType::class); $form->handleRequest($request); $analysedPdfs = $entityManager->getRepository(AwsPdfDocument::class)->findAll(); if ($form->isSubmitted() && $form->isValid()) { $files = $form->get('files')->getData(); if ($files) { foreach ($files as $file) { //PROBLEM $fileName = $fileUploader->upload($file); dd("end"); } } } return $this->render( 'aws/aws_scan_document/index.html.twig', [ 'form' => $form->createView(), ] ); } }
文件上传服务代码(src/Service/FileUploader.php)
<?php // src/Service/FileUploader.php namespace App\Service; use Symfony\Component\HttpFoundation\File\Exception\FileException; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\String\Slugger\SluggerInterface; class FileUploader { public function __construct( private string $targetDirectory, private SluggerInterface $slugger, ) { } public function upload(UploadedFile $file): string { $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $safeFilename = $this->slugger->slug($originalFilename); $fileName = $safeFilename . '-' . uniqid() . '.' . $file->guessExtension(); try { $file->move($this->getTargetDirectory(), $fileName); } catch (FileException $e) { // ... handle exception if something happens during file upload } return $fileName; } public function getTargetDirectory(): string { return $this->targetDirectory; } }
问题现象描述
- 打开PDF时:提示无权限访问该文件
- 删除PDF时:提示需要管理员权限才能执行此操作
- 将PDF复制到“我的文档”后:可正常删除该文件
运行环境
- PHP 8.1.27
- Composer 2.6.4
- Symfony web server
- Windows 10 Professional
已尝试的解决方法
- 更改PDF保存目录,无效
- 手动修改目录和文件权限,无效
- 改用纯PHP代码上传,无效
- 在Windows 10虚拟机部署应用后可正常运行,原因不明
内容的提问来源于stack exchange,提问作者Retr01
相关产品推荐
相关产品推荐

