Storage Linking Issue in Laravel 8 on localhost - php

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

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

Laravel 6 Storage results in a 404 error when trying to fetch files

I have tried to setup an upload script in Laravel and have followed the instructions in the docs.
I created a Symlink using the Laravel script and it looks like the following
storage -> /Users/username/Sites/switch/storage/app/public
The problem arrives when I go to upload the image and then get result of the image url in return. As you can see to match the symlink I set the folder to be public below.
$path = $request->file('manufacturer_image_name')->store('public');
echo asset($path);
and this returns
http://127.0.0.1:8000/public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
the problem is this doesn't work and throws a 404 but if I manually change the url from "public" to "storage" it will find the image.
http://127.0.0.1:8000/storage/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
Shouldn't
echo asset($path);
be returning a url containing storage instead of public?
assett($path) is for generating a URL for assets that are just in the public folder, things like the Mix generated CSS and JS files. If you user Laravel Storage to save the file, you also have to use Laravel storage to generate the file URL.
Storage::url('file.jpg');
Well, there are a lot of ways to do that, pick anyone which fits you best.
// using storage_path helper
storage_path('public/' . $filename);
// you could make a double-check with File::exist() method
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
// using asset helper
asset('storage/your_folder/image.png');
// using url helper
url('storage/your_folder/image.png');
// using Storage facade
Storage::url($photoLink)
Here is the simplest and exact thing for your issue
if(!empty($request->file('manufacturer_image_name'))){
$path = storage_path('public/image/');
$image_path = Storage::disk('public')->put('manufacturer_image_name', $request->file('manufacturer_image_name'));
//Assuming you have a model called Manufacturer and created $manufacturer = new Manufacturer()
$manufacturer->manufacturer_image_name = isset($image_path) ? "storage/".$image_path : "";
}
Thanks for the help, I discovered this answer the fits nearly perfectly what I am after. Laravel: Storage not putting file inside public folder
This was what I ended up with.
if($request->file('manufacturer_image_name')){
$path = Storage::disk('public')->put('logo', $request->file('manufacturer_image_name'));
echo $path;
}
$path now returns "logo/filename.ext" instead of "public/ or storage/" so I can store this directly in the db.

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 does not show images on production server

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.

Symfony copy files like upload files

I have a form that can upload an image to my website.
I have thousand images to upload in a folder (test images). I create a symfony Command to import and copy my images to the destination folder.
$url = '/tmp/testimg.jpg';
$photo = new Photo();
$photo->setName('test name');
$photo->setFile(new UploadedFile($url, basename($url)));
$photo->upload();
When i execute the commande i have:
[Symfony\Component\HttpFoundation\File\Exception\FileException]
The file "test name" was not uploaded due to an unknown error.
Function Photo::upload:
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move(
"/home/julien/work/mysite/src/MyProject/PhotoBundle/Entity/../../../../web/uploads/photos",
$this->name
);
$this->file = null;
}
This looks like the move method crashes. Using absolute paths might be a problem here. You could try this as well:
__DIR__ . '/../../../../web/uploads/photos'
Also make sure that you have proper access to that folder.
I'd also attach a debugger to this just to be sure. You can easily set up a debug session using XDebug and step through your lines. It's always good to step into each function of your code and see where it breaks.
If your code still fails I'd suggest you implement this example and see if it works:
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

Categories