When i try select an image to upload it will thow this error.
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
The file "/tmp/phpLznimY" does not exist
It does not matter if i want to preview the image or not. I heave checked the storage/app/livewire-tmp directory and the file is getting uploaded in this directory. It will work on local machine but not on production server.
"laravel/framework": "^8.0",
"livewire/livewire": "^2.10",
I've checked the livewire config and filesystem config file and these look fine.
'temporary_file_upload' => [
'disk' => 'local', // Example: 'local', 's3' Default: 'default'
'rules' => null, // Example: ['file', 'mimes:png,jpg'] Default: ['required', 'file', 'max:12288'] (12MB)
'directory' => 'livewire-tmp', // Example: 'tmp' Default 'livewire-tmp'
'middleware' => null, // Example: 'throttle:5,1' Default: 'throttle:60,1'
'preview_mimes' => [ // Supported file types for temporary pre-signed file URLs.
'png', 'gif', 'bmp', 'svg', 'wav', 'mp4',
'mov', 'avi', 'wmv', 'mp3', 'm4a',
'jpg', 'jpeg', 'mpga', 'webp', 'wma',
],
'max_upload_time' => 5, // Max duration (in minutes) before an upload gets invalidated.
],
filesystems
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
I've tried both local and public but the same error still keeps showing up.
the upload_max_filesize is 2GB so that should not be a problem.
<form wire:submit.prevent="saveCustomer" enctype="multipart/form-data">
...........
<div class="form-group row">
<div class="col-sm-1"></div>
<label class="col-sm-3 col-form-label" for="logo">Logo:</label>
<div class="col-sm-3">
<input wire:model="logo" type="file" name="logo" class="form-control-file btn btn-primary"/>
#error('logo') <span class="text-danger mt-1">{{ $message }}</span> #enderror
</div>
<div class="col-sm-2">
#if($logo)<img src="{{ $logo->temporaryUrl() }}">#endif
</div>
</div>
............
</form>
this is the validation of the logo
'logo' => 'nullable|image|mimes:png,png,jpeg,gif|max:10000000',
And i'm not uploading it to the server yet. First i wanted to see the preview. But as i said the tmp file is being uploaded to the livewire-tmp folder just fine. When i check the folder i see that it is being uploaded to the folder.
But somehow it seems like it want's to get the file from the tmp folder. And the filename ("/tmp/phpLznimY") does not come close to what it is actually.
Related
I'm creating logic about uploading avatars to users:
blade.php
<image src="{{ $user->profile->image ? storage_path() . '/ app/public/' . $user->profile->image : '' }}"/>
<input type="file" id="avatar" name="avatar" style="display:none;"/>
<label for="avatar">{{ __('text.select_file') }}</label>
Controller (update method):
public function update(User $user)
{
$this->authorize('update', $user->profile);
$data = request()->validate([
'name' => 'required',
]);
if (request('avatar')) {
$imagePath = request('avatar')->store('uploads/avatars', 'public');
$image = Image::make(storage_path("app/public/{$imagePath}"))->fit(800, 800);
$image->save();
$imageArray = ['image' => $imagePath];
}
auth()->user()->profile->update(array_merge(
$data,
$imageArray ?? []
));
return redirect("/profile/{$user->id}");
}
When I upload a picture it is stored in storage/app/public/uploads/avatars in DB value for avatar is uploads/avatars/name.jpeg
As you can see I have hard code app/public/ in controller and in view files, this is by default, I didn't change any paths, here is filesystem.php:
<?php
return [
'default' => env('FILESYSTEM_DRIVER', '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'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
],
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
So how do I avoid these coded paths? And why Laravel default store folder is in storage and not in public directory ? Should I upload avatars to public directory or to leave them in storage? I'm looking for best and correct practices ?
I'm not well versed in Laravel, but I think you should be storing everything publicly accessible in /public/ with public_path(): Difference between storing files in public directory and in storage in Laravel 5.4
Just calling public_path($user->profile->image) should work, but you'll probably want to keep some path in there to make sure the avatars are in their own directory. Perhaps you could write another helper function:
function avatar_path($filename){
return public_path('avatars/'.$filename);
}
Then use avatar_path() everywhere rather than storage_path()/public_path(). I'm not sure if something as straightforward as that is the "Laravel way" though.
When I add new public directory in Filesystem Disks in filesystems.php like below
'thumbnail' => [
'driver' => 'local',
'root' => storage_path('app/public/relativeContentPath/thumbnail'),
'url' => env('APP_URL').'/storage',
'visibility' => 'thumbnail',
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
And in my Controller I want to save an image into thumbnail directory like below
$fileNameToStore = md5(time()).'.jpg';
$photoThumbnail = ImageResize::make($productContent)
->resize(80, 80, function ($constraint) { $constraint->aspectRatio(); } )
->encode('jpg',80);
Storage::disk('thumbnail')->put( $fileNameToStore, $photoThumbnail);
But it shows me the error Undefined index: thumbnail. I don't know why this error shows me. Any help appreciate
Change 'visibility' => 'thumbnail', to 'visibility' => 'public',
https://laravel.com/docs/8.x/filesystem#file-visibility
it can be public or private other then you can not set anything as per doc
I can't display my images in a tag img in my view. I can save my uploaded image in storage->app->public->books and save the image name in my database as well. But after I run php artisan storage:link to access my file in public->storage, but whatever if I user asset() or url() method, I can't display my images in img tag, just alt="" atribute, Someone knows what I'm doing wrong?
This is my Controller store().
public function store(Request $request)
{
// Handle File Upload
if($request->hasFile('book_image')){
// Get filename with the extension
$filenameWithExt = $request->file('book_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('book_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('book_image')->storeAs('books', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.png';
}
//save in database
$itens = \App\Book::create([
'book_name' => mb_strtolower($request->book_name),
'author_name' => mb_strtolower($request->author_name),
'publishing_name' => mb_strtolower($request->publishing_name),
'description' => $request->description,
'release_date' => $request->release_date,
'category' => mb_strtolower($request->category),
'book_image' => $fileNameToStore
]);
flash('Livro criado com sucesso !')->success();
return redirect()->route('admin.books.index');
}
And this is my html where I'm trying to display.
#foreach($books as $book)
<tr>
<td><input type="checkbox" class="checkthis" /></td>
<td>
<img style="width: 80px;height: 80px" src="{{ url('books/{$book->book_image}') }}" alt="{{$book->book_image}}">
</td>
<td>{{$book->book_name}}</td>
<td>{{$book->author_name}}</td>
<td>{{$book->publishing_name}}</td>
<td>{{$book->release_date}}</td>
<td>
</tr>
And my config->filesystems.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'),
'endpoint' => env('AWS_ENDPOINT'),
],
],
You need to pass src as below.
src="{{ url('books/'.$book->book_image) }}"
as storage folder
src="{{ asset('storage/books/'.$book->book_image) }}"
I'm very new to php web development with yii2 framework. I am working on a web application and I have few variables in my return of main.php file (config folder).
return [
'components' => [
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'facebook' => [
//some fields
],
'google' => [
'class' => 'yii\authclient\clients\GoogleOAuth',
'clientId' => 'somerandomnumber.apps.googleusercontent.com',
'clientSecret' => '6f7g8h9i10j',
],
],
],
]
I have to use google's clientId in Model folder's php file.
<a href="https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2FCURRENT-DOMAIN%2Fgoogle%2Dlogin&response_type=permission&scope=email+profile+openid&client_id=GET THE CLIENT ID HERE">
<div class="gplus_seperator">
<div class="social_logo">
<span class="icon-font">p</span>
</div>
<div class="social_text">Sign in with Google+</div>
</div>
</a>
Can some one help me. TIA
You can access components using:
Yii::$app->components['authClientCollection']['clients']['google']['clientId'];
I'm trying to upload some files to a specific "disk" in Laravel and then to retrieve the URL from these files.
Here is my config/filesystem.php file:
'disks' => [
...
'thumbnails' => [
'driver' => 'local',
'root' => storage_path('app/public/pictures/thumbnails'),
'visibility' => 'public',
],
...
],
Here is the way I handle an uploaded file (very basic, just for testing purposes):
Route::post('upload', function(Request $request) {
$file = $request->file('file');
$path = $file->storeAs('/', 'myFile.'.$file->getClientOriginalExtension(), 'thumbnails');
return dd($path);
});
The file /storage/app/public/pictures/thumbnails/myFile.jpg is created, which is fine, but $path equals "myFile.jpg" instead of the full path. First strange thing.
Then if I use:
$url = Storage::disk('thumbnails')->url('myFile.jpg');
$url equals "/storage/myFile.jpg" instead of "/storage/app/public/pictures/thumbnails/myFile.jpg".
Am I missing something ?
By default the disk url method just prepends /storage as stated in the docs.
To customize the url add a url to your config:
'thumbnails' => [
'driver' => 'local',
'root' => storage_path('app/public/pictures/thumbnails'),
'visibility' => 'public',
'url' => 'the url',
],