0

In my application I have the need to:

  1. upload a file
  2. store information in the db
  3. store the file in a local or remote filesystem
  4. listing all the db rows with a link to download the file
  5. remove the file from the db and from the filesystem

I am trying to develop the 4th but the solutions found here and here don't work for me.

My filesystem.php is:

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

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

    'myftpsite' => [
        'driver'   => 'ftp',
        'host'     => 'myhost',
        'username' => 'ftpuser,
        'password' => 'ftppwd',

        // Optional FTP Settings...
        // 'port'     => 21,
         'root'     => '/WRK/FILE/TEST',
        // 'passive'  => true,
        // 'ssl'      => true,
        // 'timeout'  => 30,
    ],

In the Controller I store the file with:

    ... validation here ...
    $path = $request->uploadfile->storeAs('', $request->uploadfile->getClientOriginalName(), self::STORAGEDISK);
    $file = new TESTFile;
    ... db code here ...
    $file->save();

At this point I would like to retrive the variable to pass to the download methods (url or path of my file). I found 2 ways

  • Storage::url($pspfile->filename) *return* **/storage/** accept.png
  • Storage::disk(self::STORAGEDISK)->getDriver()->getAdapter()->applyPathPrefix($pspfile->filename) *return* C:\xampp\htdocs\myLaravel\ **storage** \app\accept.png

Any help or suggestion to do it in a better way will be very appreciated.

EDIT For the moment I separete local/public from FTP. The download is working if in the Controller I modify

$path = $request->uploadfile->storeAs('',
          $request->uploadfile->getClientOriginalName()
          ,self::STORAGEDISK);
$file->fullpath = $path;

with

$file->fullpath = storage_path('app\\') . $path;

where 'app\' is the storage_path configured as root in filesystem.php Moreover I can avoid to hardcode and use

$file->fullpath = Storage::disk(self::STORAGEDISK) ->getDriver() ->getAdapter() ->getPathPrefix() . $path;

In this way the download method can use

return response()->download($pspfile->fullpath);

I am still looking for a way to retrive a valid scr attribute for an img tag.

In addition I would like the same with remote stored files (maybe with local temp dir and file?)

Community
  • 1
  • 1
AlexMI
  • 604
  • 1
  • 7
  • 28

1 Answers1

0

I made something similar some time ago. Maybe this example code helps you.

class FileController extends Controller
{
    // ... other functions ...

    public function download(File $file)
    {
        if (Storage::disk('public')->exists($file->path)) {
            return response()->download(public_path('storage/' . $file->path), $file->name);
        } else {
            return back();
        }
    }

    public function upload()
    {
        $this->validate(request(), [
            'file-upload' => 'required|file',
        ]);

        $path = request()->file('file-upload')->store('uploads', 'public');
        $file = new File;
        $file->name = request()->file('file-upload')->getClientOriginalName();
        $file->path = $path;
        $file->save();

        return back();
    }
}
mnme
  • 557
  • 6
  • 16
  • **upload** instead of using store I use storeAs. Instead of **public** i switch (just for basic testing) from local to FTP. As you can see I leave blank the _directory name_ in order to store in remote dir configured in filesystem.php or for local disk in storing file in storage/app. In both ways the file is stored as expected. The path stored in the db is the same as the file name. Also the _get_ methods of _Storage_ facade works but I don't understand if I need it to download or if I need only path () and If the same path can be used as src attrib in img tag. – AlexMI Apr 11 '17 at 14:43