AbdulBazith's avatar

how toretreive images from storage folder and display it in blade file

guys iam working with a project online examination.

the question may contain images and the options may also contain images. and the explanation may be images or docs.

while taking a question if there is image for the question it must be stored in storage folder and to be retrieved.

i have a column ques_mage in my table.

and my blade file to upload is

 <input name="ques_image" class="form-control" type="file" accept="image/*" onchange="document.getElementById('ques_image').src = window.URL.createObjectURL(this.files[0])">
                            <br>
                            <img id="ques_image" src=""  class="rounded-circle" width="150" height="150" >

my controller

 if ($request->hasfile('ques_image')) {

            $ques_image = $request->file('ques_image');

         $filename = $request->input('ques_no') . '-' .$ques_image->getClientOriginalName();


    $path = $ques_image->storeAs('QuestionImages', $filename);

             dump($path);
             $addquestion->ques_image = $path;

        }

here the image correctly stored in storage/app/QuestionImages folder. worked correctly.

and then i used php artisan storage:link this created a shortcut storage folder in my public folder.

now i need to display it in blade file.

i did like this

<img src="{{ asset('storage/app/'.$showques->ques_image) }}"> here i didnt mention the QuestionImages folder because the path stored in my table is with this folder name say for example QuestionImages/image.jpg like this

this is not working why???

and i have two doubts

  1. i need to store pdf and document also. if i need to store the documents means what should i do the same process?? and how can i show it in the blade file??

  2. why a storage shortcut created in public folder?

is this process correct?

and can i resize the image and store it in the storage folder?? because when displaying the question with options it looks so awkward to see. how can i make it unique

Kindly some help me pleasee.

0 likes
13 replies
Snapey's avatar

you should store your images in storage/app/public.

My guess is that they are just in the storage/app folder at present

AbdulBazith's avatar

@snapey thank you for your response.

but based on my coding it is storing in app only. how i can move that to public folder.??

and will this work for pdf??

at present this is my filesystem.php

//here what is disks and what is public???and based on this where my images are stored?? where should i change??

    'disks' => [

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

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

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

    ],

];


Snapey's avatar

your public disk is specified here

storage_path('app/public'),

... as I indicated earlier.

When you save the image, save it to the public disk.

You then should have a symlink which makes public/storage have the same content as storage/app/public

Yes PDFs also. But bear in mind these files can be accessed by anyone that knows the URL

AbdulBazith's avatar

@snapey thank you for your response.

you are saying like this But bear in mind these files can be accessed by anyone that knows the URL

then what should i do?? will any security problem will come??

if the image will be stored in both places then memory size is waste..

then where should i store my images?? just i need to work with files (images, documents, pdf)

kindly suggest any link please???

in the file system i understood s3 its amazon s3

but i changed


// this is what??
  'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),  // this line i changed first it is like app i changed to app/public
        ],




// this is what??

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


whats this disk implies and public implies???


if possible kindly share any link please

is this all correct way?? in my last project i used image intervention. that will store the images in public folder.

which is correct where to store images and docs, ??

Snapey's avatar

You dont share in both places.only in one place

If you want to include the images in web pages then they must be in a public place

There are thousands of image upload tutorials out there for Laravel. Check some.

AbdulBazith's avatar

@snapey thank you. there exactly the problem i suffer. please kindly respond please

iam not clear about one thing. i referred lots of links and videos in google.

see

$ques_image = $request->file('ques_image');

$filename = $request->input('ques_no') . '-' .$ques_image->getClientOriginalName();

$pathq = $ques_image->storeAs('QuestionImages', $filename);

$addquestion->ques_image = $pathq;

my filesystems.php

'default' => env('FILESYSTEM_DRIVER', 'public'), // i have changed this intially it is "local"




 'disks' => [

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

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

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

    ],

till this ok

i done php artisan storage:link everything is ok

now when i upload an image that is stored in both places

in storage/app/public/QuestionImages and in public/stoage (this is shortcut link)/QuestionImages.

Here is the doubt why the images stored in two places. and when i access it in blade file like this

<img src="{{ asset('storage/'.$showques->ques_image) ?? "-" }}" alt="" class="img-responsive option-img-one">

this showing correctly.

but my objective is the image must be shown in browser.

but all the google videos and links says the same process. but they are saying that those images are stored in storage/app/public. but for me why it is stored in two places?

Kindly please dont hesitate please please. reply for this.

i think i was stuck into small issue.

even i referred you reply in another link : https://laracasts.com/discuss/channels/laravel/show-images-from-storage-folder

still getting confusion

Snapey's avatar
Snapey
Best Answer
Level 122

If you have the file stored in storage/app/public/QuestionImages

then it will also appear to be in public/storage/QuestionImages

Please check this is the case.

Your file is stored once. The symlink makes one directory an alias of the other.

Your webserver can store the images in the storage/app/public folder, and then they appear to the outside world in the public/storage folder.

AbdulBazith's avatar

@snapey thank you thank you soooooo much.

yes thats my case.exactly thats whats hapening. the uploaded images is stored in storage/app/public/QuestionImages

and also i can see those images in public/storage/QuestionImages

ok then my process is correct ???

atlast one doubt? the images are in both places then what about memory?? if i upload a 1 mb image 50 images means it will be in both places then it becomes 50+50=100 mb right?? memory is wasted?? is this right?? or else just forouter workd it is in both places and memory is used once only.

did i misunderstood anything??

the storage folder in public is shortcut am i right?? just it is reference am i right?? originally the images are in storage/app/public/QuestionImages only.

am i correct???

Snapey's avatar

You are still not getting it.

The file is stored once. It appears somewhere else because of the symlink. Search what symlink or symbolic link mean in your native language

AbdulBazith's avatar

@snapey thank you thank youu

ya i understood everything. i referred about symlink and got it. just its a short cut am i right.

because when i change anything or delete the image in storage folder it is reflected in the public folder that shortcut link. so just its a shortcut link.

Ha thank you thank youu soooo much...

can i crop thoses images??? i think i need to use external package. last project i used image intervention.

but i stored in public folder. can i store images in storage folder same the proccess using image intervention??

Snapey's avatar

Yes, you can store the files anywhere which the web server has write access to (the storage folder)

AbdulBazith's avatar

@snapey one doubt everything ok

now i need to do the changes in cpanel how i can do this shortcut storage link in cpanel??

if i need to run artisan comand php artisan storage:link i need ssh.

if i dont have that how can i upload these changes in live server?

AbdulBazith's avatar

@snapey

i did like this and it worked for symlink in live


Route::get('/linkingstorage', function () {
    Artisan::call('storage:link');
    echo '<script>alert("linked")</script>';
});

i wrote this in my route and it worked.

kindly if possible suggest idea for this

Link:

https://laracasts.com/discuss/channels/eloquent/images-are-shown-in-local-system-but-not-in-live-laravel

https://laracasts.com/discuss/channels/laravel/how-to-differentiate-teacher-and-admin-in-a-school-project-to-enter-the-data-laravel

https://laracasts.com/discuss/channels/laravel/confusion-in-db-structure-for-fees-in-school-project-laravel

Please or to participate in this conversation.