1

介绍

我在用:

  • 视窗 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.txtdata/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
    

请注意,$existsreturn true- so 文件肯定存在!

代码

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 - 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

结论

我错过了什么?

请指教。

感谢您的时间和知识。

4

1 回答 1

1

由于Flysystem没有local adapter获取可下载文件路径的方法!建议将文件移动或存储在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);
于 2016-11-19T13:15:17.720 回答