lundi 24 décembre 2018

Laravel file corrupted when dowloaded in private file system

I need to create private file system for my website. Each user should be able to see its own file. To do so, I created a class GetFile as follow

<?php


namespace App\Http\Tasks;


class GetFile
{
    private static $localFilePath = "attached_files";

    public static function getFile($filename)
    {
        return response()->download(storage_path(self::$localFilePath ."/".$filename, null, [], null));
    }
}

Then I created a GetFileController controller as follow

namespace App\Http\Controllers;

use App\Http\Tasks\GetFile;

class GetFileController extends Controller
{
    public function get_file($file_name)
    {
        return GetFile::getFile($file_name);
    }
}

and then a route

Route::group(['middleware' => ['web','auth'], 'namespace' => 'App\Http\Controllers'], function () {
    Route::get('get_file/{file_name}', 'GetFileController@get_file')->name('file.get_file');
});

Now the

route('file.get_file', $filename)

is a link to the $filename. When someone tries to see a file, I can control the access with some logic in get_file function of controller.

The problem is when I download a file (an image) by this method, The Windows OS says the file is corrupted or too long and it can't open the file. I am really confused, I already created private file system this way and every thing was right, but this time some thing went wring. I check the files on the server, they have no problem, so I guess something should be wrong in download function.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire