How to download private files in Windows?

Asked

Viewed 348 times

-1

The idea is to upload confidential documents, so I can’t put them in the folder /public. The goal is that in the views I can access these private files.

Local:

 'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

Controller Upload:

  public function uploadDocumentos(RequestDocumentosDevedors $request, $idDevedor)
    {

        $data = $request->all();

        $random = Str::random(150);

        if ($request->hasFile('file') && $request->file('file')->isValid()) {

            $name = kebab_case($request->descricao);
            $extension = $request->file->extension();


            $nameFile = "{$idDevedor}-{$random}.{$extension}";

            $data['file'] = $nameFile;

            $upload = $request->file->storeAs('documentos/devedor/' . $idDevedor, $nameFile);

            $id = $request->devedor_id;
            DocumentosDevedors::create($data);
        }
        return redirect()->route('admin.devedors.documentos', compact('id'));
    }

It worked that way:

Route::get(
    'teste/{id}/{arquivo}',
    function ($id, $arquivo) {

        $file =  storage_path() . '/app' . '/documentos/devedor/' . $id . '/' . $arquivo;
        return response()->download($file);
    }
)->middleware('auth');
  • The ideal is to configure in apache not to have public access in the folders of /public.

  • In the public/ folder is public access. What I need is to store documents that only the specific user has access to download.

  • 1

    I don’t know if it would work, but you can try using the storage_path() storage_path() . 'documentos/devedor/' . $idDevedor

  • Doing a test slightly I think it will work. However as I am in localhost the bars are reversed.

  • return E: project Storage/documents/debtor/16/16.pdf

1 answer

1


I usually save in Storage, for example this my method:

public function store(StoreManagedFileFormRequest $request)
{
    if ($request->hasFile('file')) {
        $file              = $request->file('file');
        $original_filename = $file->getClientOriginalName();
        $mime              = $file->getMimeType();
        $extention         = $file->getExtension();
        $size              = $file->getClientSize();
        $stored_filename   = md5($original_filename);
        $file_path         = storage_path('app/files/');

        $person_id = isset($request->person_id) ? $request->person_id : null;

        $subject    = $request->only('subject', 'description');
        $user_id    = auth()->user()->id;
        $file_moved = $file->move($file_path, $stored_filename);
        if (Storage::disk('local')->exists('files/'.$stored_filename)) {

            $result = ManagedFile::create($subject + [
                    'user_id' => $user_id,
                    'person_id' => $person_id,
                    'mime_type' => $mime,
                    'extention' => $extention,
                    'stored_filename' => $stored_filename,
                    'original_filename' => $original_filename,
                    'size' => $size
            ]);
        }
        if ($result) {
            return redirect()
                    ->route('managed_files.index')
                    ->withSuccess('Item cadastrado com êxito');
        }
    }
    return back()
            ->withErrors(['Falhou ao cadastrar item'])
            ->withInput($request->input());
}

This method is in a controller of an application that manages private files. I administer the file details in a database. Note that I created a directory "files" in the directory "app" inside the directory "Storage".

Then to download it, I did it this way:

public function download($id)
{
    $file_info = $this->model->findOrFail($id);
    if (Storage::disk('local')->exists('files/'.$file_info->stored_filename)) {

        return response()->download(storage_path('app/files/'.$file_info->stored_filename),
                $file_info->original_filename,
                ['Content-Type' => $file_info->mime_type]
        );
    }
}

Also, see that until the file extension I saved in database, to manage there the mime type, extension etc. Then save the file as having a name as just a hash. In this case I used md5().

The Migration of this table looked like this:

    Schema::create('managed_files',
        function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id')->nullable();
        $table->unsignedBigInteger('person_id')->nullable();
        $table->string('original_filename', 128);
        $table->string('stored_filename', 128);
        $table->string('extention', 4);
        $table->string('size')->nullable();
        $table->string('mime_type', 40)->nullable();
        $table->string('subject', 128)->nullable();
        $table->text('description')->nullable();
        $table->timestamps();
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.