How to change the storage path of files? - laravel

I want to change the storage path of my data (files) from the default storage/app/public to public/files.
Controller
public function store(Request $request)
{
request()->validate([
'filename' => 'required',
]);
$files = $request->file('filename');
foreach ($files as $file) {
File::create([
'filename' => $file->getClientOriginalName(),
]);
}
return redirect('/file')->with('success', 'File Uploaded Successfully');
}

go to config folder -> filesystems.php
'public' => [
'driver' => 'local',
'root' => public_path(),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
public function upload(Request $request)
{
$uploadedFile = $request->file('filename');
$filename = time().$uploadedFile->getClientOriginalName();
Storage::disk('local')->putFileAs(
'files/'.$filename,
$uploadedFile,
$filename
);
$upload = new Upload;
$upload->filename = $filename;
$upload->save();
}

$fileFullName=$request->file('cover_image')->getClientOriginalName();
$fileName=pathinfo($fileFullName,PATHINFO_FILENAME);
$ext=$request->file('cover_image')->getClientOriginalExtension();
$fileToSave=$fileName.'_'.time().'.'.$ext;
$store=$request->file('cover_image')->storeAs('/public/cover_images',$fileToSave);

in your config/config/filesystems.php you can do this like:
'disks' => [
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
]
and then store like this:
Storage::disk('uploads')->put('filename', $file_content);

Related

How to prevent the file access from public in Laravel?

I still a newbie in Laravel. Please help me to solve my issue. I stuck on to prevent the file from the public. I trying to make the file access from public to private in Laravel 7.
I put all of the links in the auth it made all of the links required access before using it. I was tried to access some links to create a record. I can't do it and the system required the auth but when I use the URL http://127.0.0.1:8000/storage/actions/filename.something without log-in to access the image file. I still can see the file.
This is my route
Route::group(['prefix' => 'admin', 'middleware'=> ['auth', 'administrator']], function () {
//Action
Route::get('/addAction', 'admin\ActionController#create');
Route::get('/deleteAction/{id}', 'admin\ActionController#delete');
Route::get('editAction/{id}', 'admin\ActionController#edit');
Route::post('/addAction', 'admin\ActionController#store');
Route::post('/updateAction/{id}', 'admin\ActionController#update');
Route::get('/allAction', 'admin\ActionController#index');
Route::get('/delete/actionImage/{id}', 'admin\ActionController#deleteimages');
});
This is the controller that I use to store the data to the database and the image to the file path.
public function index(){
return view('admin.allAction')
->with('actions',Action::all())
->with('challenges',Challenge::all())
->with('users',User::all())
->with('status',Status::all())
->with('images',Images::all());
}
public function create(){
$users = DB::table('users')
->where('role','2')
->get();
return view('admin.addAction')
->with('users', $users)
->with('status',Status::all());
public function store(Request $request){
$request->validate([
'name' => 'required|string|max:188',
'objective' => 'string|max:888',
'images' => 'required',
'images.*' => 'file|image|mimes:jpeg,png,jpg|max:8000',
]);
// Insert Data to Table
$action=new Action();
$action->name=$request->name;
$action->objective=$request->objective;
$action->status_id=$request->status_id;
$action->owner_id=$request->owner_id;
$action->challenge_id=$request->challenge_id;
/*dd($action);*/
$action->save();
if ($request->hasfile('images')) {
$images = $request->file('images');
foreach($images as $image) {
$name = time().'-'.$image->getClientOriginalName();
$name = str_replace(' ','-',$name);
/*$path =*/ $image->storeAs('actions', $name, 'public');
Images::insert([ /*OrUpdate*/
'name' => $name,
'action_id' => $action->id,
]);
}
}
Session()->flash("success", "Success!");
return redirect('/admin/addAction');
My filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
'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'),
],
],
I never do private file before I don't know how to do it. I tried to search how to on the internet but I still can't make it possible.
Thank you for all of the comments in advance and sorry for my English.

cant store the file using storeAs in laravel

iam using a laravel site and in my file i have a directory in path
/var/www/site/admin.site.com/public/storage/salescall
i need to upload a file to this salescall folder i have used the following code.
$filenameWithExt = $request->file('sales_call')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('sales_call')->getClientOriginalExtension();
$filenameToStore = $filename . '-' . time() . '.' . $extension;
$request->file('sales_call')
->storeAs('', $filenameToStore, 'sales');
and my filesystems.php is as follows
'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_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
'sales' => [
'driver' => 'local',
'root' => public_path('salescall'),
'visibility' => 'public',
],
],
file is not uploaded to folder.please help me to solve this. Thank you in advance.
You can easily do this:
$request->file('sales_call')-> storeAs('salescall/',$filenameToStore);
FOR BETTER UNDERSTANDING, SEE BELOW
The configuration of your filesystem seems to be incorrect.
Use the below sample as a guide and I hope it helps you.
If you use this code, it will upload your file in storage/app folder
Storage::disk('local')->put('file.txt', 'Contents');
Now, if you need to change the directory and store into the storage folder directly, you need to change something in the filesystems.php file.
Go to config/filesystems.php file and change the following code-
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
Here, this line of code 'root' => storage_path('app'), responsible to define where to store. You just adjust according to your demands.
Alternatively, In your controller, You could do something like this:
<?php
namespace App\Http\Controllers\Api;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
class UserAvatarController extends Controller
{
/**
* Handle the incoming request.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$request->validate([
'avatar'=> ['required','max:200']
]);
$user = User::findOrFail(auth()->user()->id);
// Handle file Upload
if($request->hasFile('avatar')){
//Storage::delete('/public/avatars/'.$user->avatar);
// Get filename with the extension
$filenameWithExt = $request->file('avatar')->getClientOriginalName();
//Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('avatar')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('avatar')->storeAs('public/avatars',$fileNameToStore);
$user->avatar = $fileNameToStore ;
$user->save();
}
return $user;
}
}
But in your own case, I believe:
In your config, set the root key to storage_path('salescall')
Yours should be $path = $request->file('sales_call')->storeAs('public/salescall',$fileNameToStore);
Just let me know if this works.. Cheers!
You have to use like this
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
$request->file('sales_call')-> storeAs('salescall/',$filenameToStore);

Disk [google-drive] does not have a configured driver

Hi i am trying to link google drive api to the laravel system for what i have used google drive api in laravel and configured the filesystem and env setting. But when executed it returns DISK not configured error.
Tried solution of clearing config cache and dump-autoload still the isuue persist same.
filesystem.php
'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'),
],
'google-drive' => [
'driver' => 'google',
'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
],
],
googledriveserviceprovider.php
public function boot()
{
Storage::extend('google-drive', function($app, $config) {
$client = new \Google_Client();
$client->setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client->refreshToken($config['refreshToken']);
$client->fetchAccessTokenWithRefreshToken($config['refreshToken']);
$service = new \Google_Service_Drive($client);
$adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);
return new \League\Flysystem\Filesystem($adapter);
});
}
Route to put file.
Route::get('put', function() {
Storage::disk('google-drive')->put('test.txt', 'Hello World');
return 'File was saved to Google Drive';
});
Any help is deeply appreciated.
I think this line:
Storage::extend('google-drive', function($app, $config) {
should just be
Storage::extend('google', function($app, $config) {
I think this useful for you
filesystem.php
'disks' => [
'google' => [
'driver' => 'google',
'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
],
],
googledriveserviceprovider.php
public function boot()
{
Storage::extend('google', function($app, $config) {
$client = new \Google_Client();
$client->setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client->refreshToken($config['refreshToken']);
$client->fetchAccessTokenWithRefreshToken($config['refreshToken']);
$service = new \Google_Service_Drive($client);
$adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);
return new \League\Flysystem\Filesystem($adapter);
});
}
Route file
Route::get('put', function() {
Storage::disk('google')->put('test.txt', 'Hello World');
return 'File was saved to Google Drive';
});

Intervention Image can't write to storage_path

I want to store uploaded image to storage_path (storage/app/images/users) and not public_path (public/images/users) in my Laravel application.
My filesystem configuration is as follows:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'users'=> [
'driver' => 'local',
'root' => storage_path('app/images/users'),
'url' => env('APP_URL').'/storage'
],
'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'),
],
],
I have also created the necessary folders in the desired path.
My upload script is as follows:
public function upload_avatar($data)
{
$users_image_location=storage_path('images/users');
$users_image_thumb_location_1=storage_path('images/users/thumbs-100');
$users_image_thumb_location_2=storage_path('images/users/thumbs-50');
$users_image_thumb_location_3=storage_path('images/users/thumbs-30');
$image = $data['photo'];
$filename = User::create_random_name(32) . '.' . $image->extension();
$image_file = Image::make($image->getRealPath());
list($width, $height) = getimagesize($image);
if ($width > 700)
{
//Resize the image
$image_file->resize(600, 600, function ($constraint) {
$constraint->aspectRatio();
});
}
//save the image file
$image_file->save($users_image_location.'/' . $filename);
//create and store a 100px thumbnail of the image
$image_file->resize(100, 100, function($constraint) {
$constraint->aspectRatio();
})->save($users_image_thumb_location_1.'/'.$filename);
$image_file->resize(50, 50, function($constraint) {
$constraint->aspectRatio();
})->save($users_image_thumb_location_2.'/'.$filename);
$image_file->resize(30, 30, function($constraint) {
$constraint->aspectRatio();
})->save($users_image_thumb_location_3.'/'.$filename);
//return the filename of the image
return $filename;
}
But when I run it, I get the following Exception indicating that the system could not write to the path.
Can't write image data to path (C:\xampp\htdocs\NelpritadConcept\storage\images/users/NITA5VYtiWGSbWLmy2M6mtTpbW6XyupA.jpeg)
And when I change the paths public_path() as follows:
$users_image_location=public_path('images/users');
$users_image_thumb_location_1=public_path('images/users/thumbs-100');
$users_image_thumb_location_2=public_path('images/users/thumbs-50');
$users_image_thumb_location_3=public_path('images/users/thumbs-30');
it works as expected.
I am running Laravel 5.7, PHP 7.2 on Windows 10 OS, Apache
I wish to know how I can store my images to storage folder instead of the public path.
I will appreciate any guide to getting this done
Regards

Store Image to the Public Folder

hello team help me I want to upload image to the "my-Project_folder/public/" but now uploaded to the "my-Project_folder/storage/app/" here is my config/filesystem
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
and my controller
{
$filename = $image->getClientOriginalName();
if ($request->hasFile('image'))
{
$request->file('image')->storeAs('images', $filename);
}
$creator = new Content_form([
'media_id' => $request->get('media_name'),
'content_title' => $request->get('content_title'),
'content_description' => $request->get('content_description'),
'date_occured' =>\Carbon\Carbon::now()->format('Y-m-d'),
'page_number' => $request->get('page_number'),
'image' => $filename,
]);
$creator->save();
if ($request->hasFile('image')){
$file = $request->file('image');
$extension = $file->getClientOriginalExtension(); // you can also use file name
$fileName = time().'.'.$extension;
$path = public_path().'/yourfolder';
$uplaod = $file->move($path,$fileName);
return $fileName;
}
You are storing in Laravel's default storage path by doing this:
$request->file('image')->storeAs('images', $filename);
You have to specify the storage path or disk:
Storage::disk('public')->put('image.jpg', $request->file('image'));

Resources