Laravel does not show images on production server - php

my production server is ubuntu Ubuntu 16.04.3 LTS and I have laravel 5.5
In the route:
/var/www/forumb
my public folder is in the directory:
/var/www/forumb/public
users upload images and these are saved in:
/var/www/forumb/public/uploads/images
the images folder has 777 permissions
in the images folder there are images that use the cover
and these are shown normal from the views
the problem is that the images that upload the users are not seen from the views and these are in the same folder images
the cover images I can see directly from the browser like this:
http://forumb.com/uploads/images/banner1.jpeg
the user images
http://forumb.com/uploads/images/5fa7385e1542a31ab9c34419b4d914fe81a11449.jpeg
but these can not be seen, laravel returns 404 error
the only images that can be seen from the views are the ones that came up together with the framework and that are in the images folder
all the others can not be seen until I uploaded an image from filezilla to the server but can not see either
this only happens on the production server, on my localhost everything works normal
this is mi code in the view
<img class="card-img-top border-bot" src="{{ !is_null(user()->image) ? $profile_image : '/uploads/images/facebook-default-no-profile-pic.jpg' }}" class="img-fluid rounded mx-auto d-block" alt="{!! user()->nombre_empresa !!}">
this my controller
public function index()
{
$perPage = 6;
$page = input('page', 1);
$baseUrl = config('app.url') . '/news-and-events';
$items = News::whereHas('photos')->with('photos')->active()->orderBy('active_from', 'DESC')->select('news.*')->get();
$profile_image = profile_image();
$total = $items->count();
// paginator
$paginator = new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(),
$perPage, $page, ['path' => $baseUrl, 'originalEntries' => $total]);
// if pagination ajax
if (request()->ajax()) {
return response()->json(view('website.news_events.pagination')
->with('paginator', $paginator)
->render());
}
return $this->view('news_events.news_events')->with('paginator', $paginator)->with('profile_image', $profile_image);
}
profile_image() function
function profile_image()
{
$image = user()->image;
$gender = user()->gender;
if ($image && strlen($image) > 5) {
return '/uploads/images/' . $image;
}
else {
return "/images/admin/$gender.png";
}
}
the images are uploaded normally but not shown

When you save an uploaded file to disk, Laravel puts the file in storage/app/public and not in public. So, you need to create a symbolic link to have access to these files from the web.
the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
https://laravel.com/docs/5.5/filesystem#the-public-disk

The reason you are not seeing images in production is well explained by #Alexey Mezenin, in reply to your question here,
in short in laravel you have to create link to your storage folder in public folder using command php artisan storage:link then you can see images,
But the problem in production is we can not run commands directly, but we can run it programmatically,
So edit your routes/web.php file and add below lines:
Route::get('/any-route', function () {
Artisan::call('storage:link');
});
imp note: keep in mind that if you have ran that command locally, then the storage folder is already there in public folder, so first you have to delete the public/storage folder,
and then you can run that specified route /any-route stated as in above route file.

I suffer from this issue too. On my localhost I could upload and view images. On the server, i could upload images but can't view images on my browsers though i can see the image in public/storage. I later discover i was pointing to the image wrongly. I hope what worked for me will work for you. These are what i did.
Upload project to server. In my case it was
|-www
|--laravelProject
Run $ php artisan storage:link or run this script
$exitCode = Artisan::call('storage:link', [] );
echo $exitCode; // 0 exit code for no errors.
I upload my image
$file = Storage::putFile('images', $request->file('my_image'));
$baseUrl = url('/');
$imagePath = $baseUrl.'/laravelProject/public/storage/'.$file;
$input['image_path'] = $imagePath;
$input['image_root'] = $file; //used when deleting
MobileSlider::create($input);
I confirm that my image is at
www/laravelProject/public/storage/images
yours could be
public_html/laravelProject/public/storage/images
To access the image on my browser
http://yoursite.com/laravelProject/public/storage/images/dfdbfdbfdfdhfkhdfhnduok.png
I am not sure this is the best way, but it worked for me.

Related

laravel missing file after image upload

I want to upload a image to a sub directory (public/uploads). after submit it returns successful but it doesn't save the image
public function storeMedia(Request $request)
{
$file = $request->file('productImage');
$name = trim($file->getClientOriginalName());
$folder = uniqid() . '_' . now()->timestamp;
$file->storeAs('uploads/'.$folder, $name, ['disk' => 'public']);
return $folder;
}
it returns 60dc27eb0eb92_1625040875 which is want I need but I can't find the uploaded file
By default laravel stores file in storage folder. If you want your image file need to be accessible publicly then you need to create symbolic link.
refer this Laravel public disk
put the folder link path in config/filesystems.php
public_path('storage/uploads') => storage_path('app/public/uploads')
Run artisan command to create symbolic link
php artisan storage:link
Here you can refer my another detailed answer for symbolic link uploading file publicly accessible
On your config/filesystems.php file, search for public_uploads and update the root to public_path() . '/uploads' and that must do the trick

Storage Linking Issue in Laravel 8 on localhost

Laravel Version: 8.35.1
PHP Version: 8.0.0
Description:
I'm uploading an image with laravel using this code:
$product_image = $request->file('product_image');
$product_image_extension = $product_image->extension();
$product_image_name = time() . '.' . $product_image_extension;
$product_image->storeAs('/media/product_images', $product_image_name);
$model->product_image = $product_image_name;
It works fine, the file is uploaded to storage/app/media/product_images/.
Then, I run the command
php artisan storage: link
to create the symlink in public.
The Command Execute Like this:
Local NTFS volumes are required to complete the operation.
The [E:\Complete Programming Bootcamp\laravel Work\ecom-project\cyber_shopping\public\storage] link
has been connected to [E:\Complete Programming Bootcamp\laravel Work\ecom-
project\cyber_shopping\storage\app/public].
The links have been created.
I am Using This Code To Display Image:
{{asset('storage/media/product_images/' . $list->product_image)}}
But Image is not displaying on the frontend.
Also, The Storage Folder Is not created in the public folder.
PLz, Help Me.
Thanks
Step 1:: Store Image
$path = ‘’;
if( $request->has('product_image') ) {
$path = $request->file('product_image')->store('media/product_images');
}
$model->product_image = $path;
Step 2:: Check Store File Path
The File Will Be Store In Path::
————————————————————————————————
Storage/app/public/media/product_images/
Step 3:: Link Storage In Public Folder
Run The Storage Link Command and remove storage link folder from the public if already exist
php artisan storage:link
Step 4:: Create Global Function To Access Images Main Controller.php File Create Global Storage Image Getting Function Like This
public static function getUrl($path)
{
$url = "";
if( !empty($path) && Storage::disk('public')->exists($path) )
$url = Storage::url($path);
return $url;
}
Step 5:: Use Function In Assest To Display Image
<img src="{{ getUrl($list->product_image) }}" />
According to https://github.com/photoncms/cms/issues/8, you are trying to symlink on fat32 drive on which it does not work. Try to test it on NTFS drive

How to store multiple uploaded files with Laravel's Storage Facade

Currently I have a project that is running Laravel 5.8 and I am trying to use a form that allows user's to upload their own images to the site and store it in the public folder. When trying to use the Storage facade with the 'put' method, a path gets returned but the images do not actually get stored.
if ($request->hasfile('images')) {
foreach ($request->file('images') as $file) {
$path = Storage::disk('public')->put('images', $file);
echo $path;
}
}
This is the code I am trying to use. The path gets echo'd as it should and I have not made any changes to the filesystem config. In the form, the input field images allows for submitting multiple files and the form does hasenctype="multipart/form-data". The $file variable also contains an instance of Illuminate\Http\UploadedFile.
The previous code I used which did work was:
if ($request->hasfile('images')) {
foreach ($request->file('images') as $file) {
$file->move(public_path('images'), $file->getClientOriginalName());
}
}
I would be okay with using my previous code if Laravel can give it a unique file name on upload but what would be causing my code with the Storage facade to not save the images properly? Does the put function just not work like that or is there something I am overlooking?
Edit:
So I realise the images were in fact saving correctly as they should be. I was looking inside the public folder rather than the storage folder which is why my second code example 'worked' but not the first. I realise when using the Storage facade I need to make use of Symbolic linking if I want to access these files on the web.
You need to also make sure you have created the symbolic link on your Ubuntu server or Windows development machine to the storage folder.
Windows you can use : mklink /j /path/to/laravel/public/youfolder /path/to/laravel/storage/youfolder
Ubuntu: ln -s /path/to/laravel/public/youfolder /path/to/laravel/storage/youfolder
To check & set you can also use use php artisan storage:link
Hope this helps
if ($request->hasfile('images')) {
foreach ($request->file('images') as $file) {
$path = Storage::disk('public')->put('images', $file);
$new_file_name = time() . "_" . uniqid() . "_" . $file->getClientOriginalName();
$path = Storage::disk('public')->put($new_file_name, file_get_contents($file));
echo $path;
}
}

Laravel: Images and Videos are not updating

I am using Laravel 5.5, and I have problems to update images and videos.
The thing here is that I have an index of my videos, these videos are being uploades to a folder (\storage\app\public\videos) this path has been linked whith the command:
php artisan storage:link
I have no problem storing any file, I use the following code on my controller:
$videoName = "".$request->user."_".$request->propiedad."_".$videoNumber.".mp4";
$request->video->storeAs('public/videos', $videoName);
As I said everything works fine, but if I try to update this file, I save it with the same name using the same code:
$request->video->storeAs('public/videos', $videoName);
And it saves when i check the folder, but the view displays the old video instead of the new one, i am not sure what is going on I have tried to clear cache but is not working.
Check if the file exist. if yes, then delete it and upload the new one.
$videoName = "".$request->user."_".$request->propiedad."_".$videoNumber.".mp4";
if(Storage::exists('public/videos' . $videoName)) {
Storage::delete('public/videos' . $videoName);
}
// Save the video
$request->video->storeAs('public/videos', $videoName);
Deleting a file
Checking if the file exists

creating image inside a folder results in creating an image with name folder/image.jpg

I have this piece of code:
$file = $faker->image($dir = public_path().'/tmp', $width = 800, $height = 600, '', true);
$hash = str_random(7);
$thumbnailName = $hash . '.jpg';
$thumbnailImage = ImgResizer::make($file)->fit(180, 180);
$thumbnailImage->save( public_path() . '\\thumb\\'. $thumbnailName);
rename($file, 'public/'.$hash.'.jpg');
As you can see I am using faker to populate the database. Faker supports images as well and it's getting images from lorempixel.com. I save this image directly in public folder and that works. I also create a thumbnail of the image using InternventionImage and save it to public/thumb folder as you can see in the code.
When I run the db:seed, no errors were produced, it all went well. However when I looked inside thumb folder there were no files there. There were bunch of images created inside public folder but not inside thumb. I logged in the server and cd into default folder and typed ls and I got this output:
As you can see on the image there are bunch of public/thumb/hashcode.jpg images there. How is this possible, they are not inside thumb folder, why is it listing them there? Did the script instead create file with filename public/thumb/hashcode.jpg? How do I put them inside thumb folder? This code works on my local machine windows 10 under apache2 and php7.
Try change this:
public_path() . '\\thumb\\'. $thumbnailName
To this:
public_path().DIRECTORY_SEPARATOR.'thumb'.DIRECTORY_SEPARATOR.$thumbnailName
Also, create thumb directory and set correct permissions:
chmod -R 755 /public/thumb

Categories