2

I'm saving files locally in Laravel, however I'm having issues getting the right URL and accessing the files.

I've setup a symlink with Artisan:

php artisan storage:link 

When saving, I add public/ to the name, so the files are placed in the /storage/app/public/ directory, which works.

if ($request->hasFile('files')) {
    $files = array();

    foreach ($request->file('files') as $file) {
        if ($file->isValid()) {
            $name = time() . str_random(5) . '.' . $file->getClientOriginalExtension();
            Storage::disk('public')->put($name, $file);
            $files[] = $name;
        }
    }

    if (count($files) > 0) {
        $response->assets = json_encode($files);
    }
}

The name is stored in the database

["1524042807kdvws.pdf"]

Then the assets are returned as part of a JSON object via my API for Vue

if (count($response->assets) > 0) {
    $assets = array();
    foreach (json_decode($response->assets, true) as $asset) {
        $assets[] = asset($asset);
    }
    $responses[$key]->assets = $assets;
}

Which returns http://127.0.0.1:8000/1524042807kdvws.pdf but that 404s. I've gotten myself a little confused I think, so any pointers or help would be appreciated.

Jam3sn
  • 1,077
  • 4
  • 17
  • 35

2 Answers2

5

So I found my answer on another post

I needed to wrap the $file in file_get_contents();

Storage::disk('public')->put($name, $file);

and instead of asset() I used:

Storage::disk('public')->url($asset['file']);
Jam3sn
  • 1,077
  • 4
  • 17
  • 35
1

Check this docs. https://laravel.com/docs/5.5/filesystem#the-public-disk

As it shows there instead of

$name = 'public/' . time() . str_random(5) . '.' . $file->getClientOriginalExtension();

you can just have

$name = 'time() . str_random(5) . '.' . $file->getClientOriginalExtension();
Storage::disk("public")->put($name, $file); // assuming you have the public disk that comes by default

Then when you want to get an url, you can use the asset function

asset('storage/foo.txt');
Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32
  • Thanks, yeah I've gone through the docs. So i've updated my question to use `disk('public')` which is saving it in the correct place. I was already using `asset()` to retrieve the file but still 404ing – Jam3sn Apr 18 '18 at 09:16
  • the file is stored in the correct path in the system? if it's under 'public' `asset('foo.txt');` should get it.. – Sérgio Reis Apr 18 '18 at 09:28
  • can you access any file by directly using the url in the browser ? – Sérgio Reis Apr 18 '18 at 09:28
  • Yeah so it's in `/storage/app/public/1524042807kdvws.pdf` but like I say the `asset()` function returns `http://127.0.0.1:8000/1524042807kdvws.pdf`which just goes to my 404. – Jam3sn Apr 18 '18 at 09:33
  • does `http://127.0.0.1:8000/storage/1524042807kdvws.pdf` return the file? – Sérgio Reis Apr 18 '18 at 09:37
  • That shows a different 404 (Purple header) with `The requested resource /storage/1524042807kdvws.pdf was not found on this server.` – Jam3sn Apr 18 '18 at 09:41
  • mucha gracias, el codigo me evita si existe dos archivos con el mismo nombre – marcos alberto saavedra sanabr Jun 22 '18 at 00:37