我正在使用:
我确实设置了树结构(代表目录和文件)以及使用OneupUploaderBundle
和Flysystem
端点进行文件上传。
组织文件上传以上传到项目根文件夹中存在的名为data
的文件夹。上传工作正常,甚至包含自定义子目录的路径也适用于不同的上传工作(例如data/project_001/test1.txt
和data/project_002/test2.txt
)!
我需要提供用户之前上传的文件。
目前
我收到错误(控制器结束1)
The file "Project_9999/2_Divi/bbb.pdf" does not exist
500 Internal Server Error - FileNotFoundException
和错误(控制器结束2)
Catchable Fatal Error: Object of class League\Flysystem\File could not be converted to string
500 Internal Server Error - ContextErrorException
请注意$exists
返回true
- 所以文件绝对存在!
config.yml的相关部分
oneup_uploader:
mappings:
gallery:
storage:
type: flysystem
filesystem: oneup_flysystem.gallery_filesystem
frontend: blueimp
enable_progress: true
namer: app.upload_unique_namer
allowed_mimetypes: [ image/png, image/jpg, image/jpeg, image/gif ]
max_size: 10485760s
oneup_flysystem:
adapters:
my_adapter:
local:
directory: "%kernel.root_dir%/../data"
filesystems:
gallery:
adapter: my_adapter
控制器操作
<?php
public function projectDownloadFileAction($file_id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('AppBundle:FileTree');
$ultra = $this->get('app.ultra_helpers');
$selected_file = $repo->getFileTreeNodeByIdArray($file_id);
$item_name = $selected_file[0]['item_name'];
$item_extension = $selected_file[0]['item_extension'];
$activeProject = $this->get('session')->get('active_project');
$activeProjectSelectedNodePath = $this->get('session')->get('active_project_selected_node_path');
$item_file_name = $item_name .'.'. $item_extension;
$complete_file_path = $activeProject['secret_path'] . $activeProjectSelectedNodePath .'/'. $item_file_name;
dump($complete_file_path);
ENDING -1-
ENDING -2-
}
结束1
$downloadable_file = new \SplFileInfo($complete_file_path);
dump($downloadable_file);
$response = new BinaryFileResponse($downloadable_file);
// Set headers
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-Type', mime_content_type($downloadable_file));
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$downloadable_file->getFilename()
));
return $response;
结束2
$filesystem = $this->get('oneup_flysystem.gallery_filesystem');
$exists = $filesystem->has($complete_file_path);
if (!$exists)
{
throw $this->createNotFoundException();
}
dump($exists);
$downloadable_file = $filesystem->get($complete_file_path);
dump($downloadable_file);
$response = new BinaryFileResponse($complete_file_path);
$response->trustXSendfileTypeHeader();
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $response;
我试图提供Ending 1
变量`$ complete_file_path的绝对路径,但只得到了错误。例如:
File not found at path: C:\DEV\tree_and_upload\data\Project_9999\1_Viens\test1.txt
500 Internal Server Error - FileNotFoundException
File not found at path: C:/DEV/tree_and_upload/data/Project_9999/1_Viens/test1.txt
500 Internal Server Error - FileNotFoundException
但它没有任何差异 - 我得到了访问错误。也许Symfony主动限制访问项目根目录中的data
文件夹......
如何提供位于项目根文件夹中的data
文件夹中的文件,并使用Flysystem
文件系统抽象层和Local
适配器?
我错过了什么?
请告知。
感谢您的时间和知识。
答案 0 :(得分:1)
Flysystem
local adapter
没有方法可以获取可下载的文件路径! It is suggested将文件移动或存储在公共web
文件夹中,并以与资产相同的方式检索路径。
我最终使用StreamedResponse
[1]代替BinaryFileResponse
。
use Symfony\Component\HttpFoundation\StreamedResponse;
$response = new StreamedResponse();
$response->setCallback(function () {
echo $downloadable_file_stream_contents;
flush();
});
$response->send();
如何获得$ downloadable_file_stream_contents;
$fs = new Filesystem($localAdapter);
$downloadable_file_stream = $fs->readStream($public_path);
$downloadable_file_stream_contents = stream_get_contents($downloadable_file_stream);