I want to return the files names of a dir called public/uploads. I used Storage::allFiles and Storage::files, but only return an empty array.
Storage works only for storage directory. If you want to use it, you'll need to create a symbolic link.
Use File facade instead:
File::files(public_path('uploads'));
Related
I have two folder in view file. like:
template1
template2
I want to see all the file list from specific folder. and check there is any specific blade file have or not. So i dont know how to do this.
thanks.
If you want to list all views, you could use the File facade to get an array of all the files included in your views folder. This will not show vendor views unless they have been published.
use Illuminate\Support\Facades\File;
$views = collect(File::allFiles(resource_path('views')))
->map->getPathName()
// ->filter(fn ($pathName) => ... ) // additional filtering
;
If you want to check if a specific view exists or not, the View facade has an exists method.
use Illuminate\Support\Facades\View;
View::exists('template1.some-view'); // returns true if template1/some-view.blade.php exists.
I solved my problem from here.
https://laravel.com/docs/9.x/views#determining-if-a-view-exists
I have a Laravel website and I have several routes that load the contents of images from Storage. I do this using the following code:
public function show_image($name) {
echo Storage::disk('images')->get($name);
}
I want to prevent users being able to set name to something like ../../../error.log. So I don't want users to escape the Storage directory. I have a few ideas on how to accomplish this however I want to know is there a best practice?
If you need just file name, not location, disallow them from inputting folder of any kind. Just cut the string on /.
end(preg_split("#/#", $name));
When you need to allow some folders and all of the contents, check the folder name, subfolder name, etc.
You could either keep a registry/index of the uploaded images, and only allow the user to show a image from that registry (e.g. an images database table).
Or you could do a scan of the directory, that you are allowing files from, and make sure, that the requested file is in that list.
public function show_image($name) {
$files = Storage::disk('images')->files();
if (! in_array($name, $files)) {
throw new \Exception('Requested file not found');
}
echo Storage::disk('images')->get($name);
}
(code untested)
In my application users can upload files. In my controller I have the following:
//...
$request->file->storeAs('/public/user_files',$fileName);
I don't like how I have to hard code the path /public/user_files, what is the proper way to manage file paths? I could simply create a variable but is there any better ways for maximum maintainability?
From my understanding I have a few options:
Create a variable
Create a new file in config directory that manages the path
Create a disk in filesystem.php and use storeAs with the custom disk
What is the best way to handle paths?
In most cases, you should use filesystem disks as you specified in your last option, it allows a centralized flexibility.
In this case, you may use Laravel helpers such as public_path or storage_path, for example:
$request->file->storeAs(
storage_path('app/public'), $fileName
);
Combined to a symbolic link, it allows flexibility about public assets.
https://laravel.com/docs/7.x/filesystem#the-public-disk
you can use helpper public_path()
$request->file->storeAs( public_path('/user_files') ,$fileName);
doc: https://laravel.com/docs/7.x/helpers#method-public-path
public function deletePublication($publication_id=null){
if(!empty($publication_id)){
Publication::where(['publication_id'=>$publication_id])->delete();
return redirect()->back()->with('flash_message_success','Publication Deleted Successfully..');
}
}
by this code I'm unable to delete files from by folder which is inside public/files but files are deleted from database.Can You help me how can I delete files from both?
very simple try this
use Illuminate\Support\Facades\Storage;
public function deletePublication($publication_id=null)
{
if(!empty ($publication_id))
{
$publication_path = Db::table('publications')
->where('publication_id','=',$publication_id)
->value('publication_file');
Storage::delete($publication_path);
DB::table('publications')->where('publication_id', '=', $publication_id)- >delete();
return redirect()
->back()
->with('flash_message_success','Publication Deleted');
}
}
path it must be different in your table.
To know more about visit File system Laravel
What is your code to create a publication? What you did to create a publication (including the uploading of file), you also have to do the reverse (which is delete the file).
Laravel has a good documentation, you can refer to this link on how to delete files/directory: https://laravel.com/docs/5.6/filesystem.
You just added the code to remove a file from Database. Need to remove the file from a folder, try below code
$filePath = 'public/files';
unlink($filePath);
Please read details of unlink() function
http://php.net/manual/en/function.unlink.php
You can also try from laravel to remove the file, please review the link from laravel documentation
https://laravel.com/docs/5.6/filesystem#deleting-files
I'm using storage_path() for storing uploaded images, but when I use it is pointing wrong on my page.
I use it like this {{ $data->thumbnail }} where $data came from the database and thumbnail comes as the string which used storage_path
Let us take a look at the default L4 application structure:
app // contains restricted server-side application data
app/storage // a writeable directory used by L4 and custom functions to store data ( i.e. log files, ... )
public // this directory is accessible for clients
If I were you, I would upload the file to the public directory directly:
Store image here: public_path() . 'img/filename.jpg'
Save the 'img/filename.jpg' in database
Generate the image URL with url('img/filename.jpg') => http://www.your-domain.com/img/filename.jpg
Hope this helps.
The storage_path function returns the path to the storage folder, which is inside the app folder --and outside the public folder-- so it's not directly accessible from the client, that's why your images are not being displayed. You can move them to the public folder path, or you could use a custom controller to handle the image requests, read the image from the storage folder and return the value.