【发布时间】:2017-04-06 23:03:04
【问题描述】:
简介
我正在使用:
- Windows 10 专业版
- XAMPP 与 PHP v7.0.9
- Symfony v3.1.6
- 学说 v2.5.4
- StofDoctrineExtensionsBundle [1] 以便 管理树结构。
- OneupUploaderBundle [2] 用于上传文件
- OneupFlysystemBundle [3] 用于文件系统抽象
设置
我确实设置了树结构(表示目录和文件)并使用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;
更新 1
我尝试提供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 - FileNotFoundExceptionFile 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 适配器?
结论
我错过了什么?
请指教。
感谢您的时间和知识。
【问题讨论】:
-
$complete_file_path真的包含文件的绝对路径还是相对于%kernel.root_dir%/../data? -
@xabbuh,是的,
$complete_file_path与%kernel.root_dir%/../data相关。 -
在构造
BinaryFileResponse时,您必须在$complete_file_path前面加上%kernel.root_dir%/../data,或者检查getAbsolutePath()对象中是否有类似getAbsolutePath()的方法。 -
@xabbuh,事实证明,没有任何方法可以使用
local适配器获取Flysystem中文件的绝对路径。建议将文件移动到web目录的某个位置,以使其可访问或使用流。确实 - 流允许从项目根目录中存在的data文件夹中提供文件。所以我最终使用了 Symfony 的StreamedResponse,它运行良好。感谢您的帮助/
标签: php download symfony flysystem