1

序章

私は使っている:

  • ウィンドウズ 10 プロ
  • XAMPP と PHP v7.0.9
  • symfony v3.1.6
  • ドクトリン v2.5.4
  • Tree 構造を管理するためのStofDoctrineExtensionsBundle [1] 。
  • ファイルをアップロードするためのOneupUploaderBundle [2]
  • ファイルシステムの抽象化のためのOneupFlysystemBundle [3]

設定中

ツリー構造 (ディレクトリとファイルを表す) をセットアップしOneupUploaderBundleFlysystemエンドポイントを使用してファイルをアップロードしました。

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

  • File not found at path: C:/DEV/tree_and_upload/data/Project_9999/1_Viens/test1.txt 500 Internal Server Error - FileNotFoundException

しかし、違いはありませんでした-アクセスエラーが発生しました。dataおそらく、Symfony はプロジェクトのルートにあるフォルダーへのアクセスを積極的に制限しています...

質問

dataプロジェクトのルート フォルダーにあるフォルダーからファイルを提供し、アダプターFlysystemでファイルシステム抽象化レイヤーを使用するにはどうすればよいですか?Local

結論

私は何が欠けていますか?

お知らせ下さい。

お時間と知識をありがとうございました。

4

1 に答える 1

1

同様Flysystemに、local adapterダウンロード可能なファイルパスを取得する方法はありません! publicフォルダーにファイルを移動または保存し、アセットと同じ方法でパスを取得することをお勧めします。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 に答える