Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tallaljamshed's avatar

to upload

$file = Storage::disk('public')->putFileAs($folder,$request->file('file_input'),$filename);

to download

$headers = array(
            'Content-Type:'.$file->mime_type,
        );
$path = storage_path('app/public'.'/'.$folder.'/'.$filename);

// $path is the link to the file use it in <img> src or download the file like below:

return response()->download($path, $filename, $headers);

NOT CLAIMING TO BE THE PERFECT OR SECURE SOLUTION BUT THIS WORKED FOR ME FOR NOW. WILL FIGURE OUT SOMETHING BETTER LATER

PS: Maybe try

$path =Storage::url($folder.'/'.$filename);

to get the URL for the file instead of above method if it works tell me :)

1 like
vainway 's avatar

@jlrdw no its only the admin and there is no need for bob to view it again

and let me tell you a bit on my project : it is for freelancing so ex: bob give a request as boss hiring a job may be he want us to create a logo of his business so he give us a small description on how he want it and also he has a picture like a suggestion to be look like the one he gave us, so the image can help us to give the image of the logo he wanted to be done. thats the use of the image he gave us

jlrdw's avatar

You could try something: Make a folder in in your laravel webroot folder. I do not know your folder structure.

But if your webroot folder was mysite. (That is just example) make a folder called upload.

So you have

public_html  
----mysite   
--------upload   // make sure it has write permissions

Now place an image in that folder name it test.jpg.

Now try to view the image in blade like:

<img src="{{ asset('upload/test.jpg') }}" alt="">

If you can view it you should be good to go. Just change your uploaded images to that folder, or whatever you want to name it. Perhaps images.

2 likes
Snapey's avatar

no its only the admin and there is no need for bob to view it again

If the admin needs to see the image through the web interface then there is no difference between bob and the admin. The image needs to be stored in a public place if you want to see it through the admin interface.

show how you save these images and I'll show you how to adapt it

2 likes
azeame's avatar

@niyo You can actually use a Fallback Route to replace the functionality of the storage symlink.

In your routes/web.php add a fallback route like so:

Route::fallback('FallbackController');

And then create the controller which handles the fallback route:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

class FallbackController extends Controller
{
    public function __invoke($path)
    {
        if (substr($path, 0, 8) === 'storage/')
        {
            $resource = substr($path, 8);
            if (Storage::disk('public')->exists($resource))
            {   
                // You can add any necessary authorization checks here.
     
                $content_type = (new UploadedFile(storage_path('app/public/' . $resource), $resource))->getMimeType();
                return response()->file(storage_path('app/public/' . $resource),
                    [
                        'content/type' => $content_type,
                    ]);
            }
        }
        return abort(404);
    }
}

This should be able to handle a number of situations:

  • if the file resource exists it will provide the correct header content/mime type
  • you can implement any form of authorization or gate
  • it can serve files from directories nested in the storage/app/public folder
  • if the resource doesn't exist it will respond with a 404 not found error

Let me know what you think.

2 likes
Snapey's avatar

Why would you go to all that trouble when you can just save the file to public/images or similar

2 likes
vainway 's avatar

@snapey i wonded if i can use other way without struggling with storage and save different way

just save the file to public/images or similar

and try to show me according to my code below

public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|min:3',
            'time' => 'required|min:3',
            'description' => 'required|min:10',
            'file' => 'required|file|max:2048'
        ]);

        $upload = $request->file('file');
        $path = $upload->store('public/storage/posts');

        $post = Post::create([
            'name' => $request->name,
            'time' => $request->time,
            'description' => $request->description,
            'file_title' => $upload->getClientOriginalName(),
            'file' => $path,
            'user_id' => Auth::user()->id
        ]);

        return redirect()->route('givePrice', $post->id);
        }
Snapey's avatar

go into the config file filesystems.php and create a new entry below public

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        'upload' => [
            'driver' => 'local',
            'root' => public_path('upload'),
            'url' => env('APP_URL').'/upload',
            'visibility' => 'public',
        ],

Now you have a file system disk called upload which refers to this folder within the public folder (not to be confused with the public disk)

change your controller;

    {
        $this->validate($request, [
            'name' => 'required|min:3',
            'time' => 'required|min:3',
            'description' => 'required|min:10',
            'file' => 'required|file|max:2048'
        ]);

        $path = $request->file('file')->store('/','upload');

        $post = Post::create([
            'name' => $request->name,
            'time' => $request->time,
            'description' => $request->description,
            'file_title' => $upload->getClientOriginalName(),
            'file' => $path,
            'user_id' => Auth::user()->id
        ]);

        return redirect()->route('givePrice', $post->id);
        }

So, now, in your database, you will just have the filename and no path attached. But you know it is in the upload disk.

<img src="{{ asset('upload/' . $post->file }}" >
2 likes
vainway 's avatar

@snapey the problem now i have is am seeing them in public/uploads as they are surposed to be but when try to view them they show as there is no image inside i don't know why

may be is bcz i use '/home/username/laravelpro/public/upload' to get them not in public_htm folder

jlrdw's avatar

@niyo can't you just adjust the asset helper to display the images.

Just use a little trial and error on this

<img src="{{ asset('upload/' . $post->file }}" >

Echo out various asset helper lines until it's correct.

2 likes
vainway 's avatar

@jlrdw i used all the steps @snapey gave me

<img src="{{ asset('upload/' . $post->file }}" >

its the one am using

just i think cause am using this

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../laravelpro/vendor/autoload.php';
//require __DIR__.'/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../laravelpro/bootstrap/app.php';
//$app = require_once __DIR__.'/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

may be cause all folders are not in public_htm

jlrdw's avatar

But his example showed you how to upload to the public and display from public, just experiment around with the asset helper.

A few answers above I also gave you an example of using the asset helper, it's always worked fine for me in development and production.

Use a little trial and error.

Always clear your cache after each program change. (Code change).

2 likes
vainway 's avatar

@jlrdw try to show me how i can use asset helper. i don't know how it works

Snapey's avatar

so after all this, you NOW say that you are not using the public folder !!

Change the upload in filesystem.php TO WHATEVER YOUR PUBLIC FOLDER IS CALLED

3 likes
jlrdw's avatar

try without the asset helper.

Also look it up in the documentation under helpers.

An example was given above.

Snapey's avatar

asset helper is going to be of no use if the public path is not /public

2 likes
jlrdw's avatar

@niyo do you have them in upload or uploads.

Make sure you use right URL.

the problem now i have is am seeing them in public/uploads

2 likes
vainway 's avatar

@jlrdw am keeping them in upload folder and i found that it wasn't also the problem of "username/laravelpro/ ....../etc." its also steal the same

am also steal using public/upload

jlrdw's avatar

@niyo in one of your replies you stated:

the problem now i have is am seeing them in public/uploads as they are surposed to be but when try to view them they show as there is no image inside i don't know why

@snapey example was upload which you used in the asset helper.

Verify exactly where these images are. What folder are they in, and is that folder underneath public?

Public is a generic term whatever folder you call public.

See this folder structure here

In the example laravel54 is public.

So what is your public folder in production. And remember in one of the above replies I showed you how to serve an image from any folder.

People are really trying to help you but you have to start understanding paths and folder structures and url's.

EDIT:

In my image above you see assets. I have dog images in

laravel54\assets\upload\imgdogs

To view those images I do

<img src="{{ asset('assets/upload/imgdogs') . '/' . $row->dogpic }}" alt="" class="image">

So when using the asset helper, I don't include laravel54 in example.

I also have a folder for cats named imgcats.

So to serve cats, it's

<img src="{{ asset('assets/upload/imgcats') . '/' . $row->catpic }}" alt="" class="image">

Just figure where (for sure) your images are so you can serve them. I don't know how to explain this any better.

2 likes
azeame's avatar

@snapey This is why I went through all of that trouble writing a Fallback Route/Controller Handler, because it should have alleviated the issue of having separate application folders and public folders their permission issues in a shared hosting environment.

2 likes
vainway 's avatar

@jlrdw i was may be mistake to write

the problem now i have is am seeing them in public/uploads

this but am using both same thing you gave and @snapey too but the problem is steal the same and there is no problem with asset helper cause is the one am using same as ur example

jlrdw's avatar
  • Put here the url that you use to bring up your web site
  • Also the complete folder to the site
  • and show your basic folder structure

Is your folder structure

A

mysite   
--------upload

OR

B

Some_root_folder (    www   public_html    htdocs     )
----mysite   
--------upload

Also maybe you could put this on GitHub temporarily so some of us can have a look and try to help.

Without having the data and code it is hard to see what's going on.

2 likes
Previous

Please or to participate in this conversation.