How to upload font file in Laravel - php

I have files like this font.ttf I need to upload them
HTML code:
<form action="{{ route('admin.settings.pdf.update') }}" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="file" accept=".ttf" required>
<button type="submit" class="button green">upload</button>
</form>
Controller:
public function updatePdfFont(Request $request)
{
$request->validate([
'file' => 'required|file',
]);
// dd($request->all());
// rename($request->file, "pdf_font.ttf");
$request->file->store('fonts', 'public');
}
// public disk is a custom one that goes files to public folder
The problem that I'm facing now when uploading font it uploaded with no extension like this wmRey4YaOLaldchlFV1l6GQylbZArc4xmyy2tXnL
The second thing I need is how can I rename the file before uploading it? like I want to rename file to pdf_font.ttf

before this line:
$request->file->store('fonts', 'public');
get the file extension like this:
$file_name = $file->'pdf_font'.getClientOriginalExtension();
Now store the file with the $file_name

In web applications, one of the most common use-cases for storing files is storing user uploaded files such as photos and documents. Laravel makes it very easy to store uploaded files using the store method on an uploaded file instance. Call the store method with the path at which you wish to store the uploaded file:
public function update(Request $request)
{
$path = $request->file('avatar')->store('avatars');
return $path;
}
https://laravel.com/docs/8.x/filesystem

Related

Image upload on laravel doesn't work online : Laravel

The project works fine on my localhost but has issues on a live shared server.
I have tried adding this code to my index.php
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
I have tried adding this code in a new sym.php file in my public folder
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';
?>
I have tried adding this on my web.php and then running site/linkstorage
Route::get('/linkstorage', function () {
Artisan::call('storage:link');
});
None of these solutions works
here is a snippet of my Controllers code:
public function storeBrand(Request $request){
$this->validate($request, ['brand_name'=> 'required',
'brand_url'=> 'required',
'brand_image'=>'image|nullable|max:1999']);
if($request->hasFile('brand_image')){
//1 : get filename with ext
$fileNameWithExt = $request->file('brand_image')->getClientOriginalName();
//2 : get just file name
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
//3 : get just extension
$extension = $request->file('brand_image')->getClientOriginalExtension();
//4 : file name to store
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
//upload image
$path =$request->file('brand_image')->storeAs('public/BrandImages', $fileNameToStore);
}
else{
$fileNameToStore ='noimage.jpg';
}
$brand=new Brand();
$brand->brand_name =$request->input('brand_name');
$brand->brand_url =$request->input('brand_url');
$brand->brand_image =$fileNameToStore;
$brand->save();
return redirect('/create_brand')->with('status', 'The '.$brand->brand_name.' Brand has been saved successfully. Create another one.');
Note
When an image is uploaded the path can be traced, but the image is not found, returns an empty image.
Thank you for your time and assistance.
I have got the solution:
I Wrote down this code in my web.php route file:
Route::get('/linkstorage', function () { $targetFolder = base_path().'/storage/app/public'; $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage'; symlink($targetFolder, $linkFolder); });
After that I navigated to my url/linkstorage
It worked!!
Looking at the code in your controller, it seems correct. Perhaps the error is within your form in the Blade file associated with this method. Based on experience I tend to forget to write this and this could probably sort out your error.
Write this on the form tag.
<form action="{{ insert the route here }}" method="POST" enctype="multipart/form-data">
// insert form input fields here...
</form>

Dropzone.js not sending file to laravel controller on submit

Hi I am trying to integrate Dropzone.js in my app and using Laravel Framework. I have a form with below code,
<form method="post" action="{{url('/example/fileupload')}}"
enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
#csrf
<input type="submit">
</form>
The laravel controller attached with this form has below code in which I am just trying to get the name if the image which is dropped in dropzone area,
public function fileupload(Request $request)
{
$file = $request->file('file');
$filename = $file->getClientOriginalName();
echo $filename;
}
After clicking submit button it shows me below error,
Call to a member function getClientOriginalName() on null
Dont know what I am doing wrong here, because when I try to run the same code with simple
<input type="file" name="file">
it shows me the name of the uploaded image file which I want. Any suggestion or fix? Thanks
Try changing the "Input" class for "Request" ... I believe it will work. The error is being reported because there is no method on the object you are calling.
public function fileupload(Request $request)
{
$file= $request->file('file');
$fileName = $image->getClientOriginalName();
echo $fileName;
}

getClientOriginalName() on null and $request->hasFile('image') not working

I'm working on uploading some text and image to database. I'm getting an error :
Call to a member function getClientOriginalName() on null
when below code is used:
$file = $request->file('image')->getClientOriginalName(); // 'image' is name in html form.
But $file = $request->input('image'); gets image name.
(also if($request->hasFile('image')) is not work (it returns FALSE).)
In my html,
<form method="post" action="/postupload" enctype="multipart/form-data">
<input type="text" name="title">
<input type="file" name="image">
</form>
In my controller,
public function upload(Request $request) {
$title = $request->input('title');
if($request->hasFile('image')) {
$file = $request->file('image')->getClientOriginalName();
$image->move(public_path('images'), $file);
$post = new Post();
$is_success = $post->addPost($title, $file);
}
}
When you access the uploaded file using the $request->file() method, it returns an instance of php's native SplFileInfo class. By looking at the php manual, you can see which methods are available on that class, check it out here: https://www.php.net/manual/en/class.splfileinfo.php
Also, take a look at the Laravel's docs: https://laravel.com/docs/5.8/requests#files
To further extend the answer: Should you need to do something with your file that the SplFileInfo cannot do, you can retrieve the newly uploaded file via the Storage Facade, which is built on top of the Flysystem package that will give you a ton of options for working with files. Start by reading up on the File Storage Larave'ls docs here: https://laravel.com/docs/5.8/filesystem#retrieving-files
Use instead of that
$request->file('upload_file');
var_dump($request->file('upload_file'));

Laravel 5 file and image upload

View:
<input type="file" name="photo">
<button class="btn">Send</button>
<input type="submit" value="send"> // I tested by input and button but it also does not work
Controller:
$f = $request->file("photo");
if($f->isValid()){
echo "upload"
$f->move("/public/upload/file.png");
}else{
echo 'not upload';
}
Error:
Fatal error: Call to a member function isValid() on null
I do not know why but I can not do upload images to the site. I've tried many ways but nothing works. Send files by request and get an error, I tried to use a Request::file('photo') or Input::file('photo')->isValid() but it not work too.
var_dump($request->file('prod_photo'));
If you will use such code in response gets NULL
EDIT:
$f = $request->file('photo');
if ($f->isValid()) {
$ext = $f->getClientOriginalExtension();
$org_name = $f->getClientOriginalName();
$r = rand(11111,999999);
$f->move("public/uploads", $r.$org_name);
//
}
How can I change the image size and check if the file is certainly the picture?
Add enctype="multipart/form-data" to your form
EDIT:
You can easily check it with validator, like so:
$this->validate($request, [
'photo' => 'required|image' //works for jpeg, png, bmp, gif, or svg
]);
Unfortunely u cant change image size with native php (as i know), you need to use either ImageMagick, GD or Glide (which needs previous one)

Laravel 5 : the move() function doesn't save the uploaded image file

What I'm trying is to set an image uploader with Laravel 5.
Here is the form.
<form action="/edit/{{$id}}" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="hidden" name="_method" value="PUT">
//There are other tags (including other input) here
<input type="file" name="pic" accept="image/*">
<input type="submit" value="Apply Changes">
</form>
Here is the controller function which will be run from the form above.
public function update($id)
{
$this->model->find($id)->fill($this->request->all())->save();
if ($this->request->hasFile('pic')) {
$file = $this->request->file('pic');
$name = $file->getClientOriginalName();
$file->move('assets/serviceimages/', $name);
}
return redirect('/blahblah');
}
As you might have noticed, this doesn't store the uploaded file under the assets/serviceimages/ directory, which is what I've been trying.
Basically I've been following this tutorial, but apparently I'm missing something and totally clueless about what it is, even after I had a look at the API documentation.
Inside of the if statement of the controller function, it was confirmed that the $file and $name had what I expected.
Therefore, I suspect that the problem lies in the move() function.
Any small piece of advice will be appreciated, since this is my first time that I've tried to set an uploader.
Permissions if you are on Linux. If that doesn't work try it this way:
$image = $request->file('pic');
$destinationPathImg = '/your path/images/';
if (!$image->move($destinationPathImg, $image->getClientOriginalName())) {
return 'Error saving the file.';
}
Also, this need to be added in ur ()
public function update(Request $request, $id)
I found having to use the Storage adapter to use the full file path worked for me.
$storageAdapter = Storage::disk('v1')->getDriver()->getAdapter()
$full_path_before = $storageAdapter->applyPathPrefix($oldPath);
$full_path_after = $storageAdapter->applyPathPrefix($newPath);
if (!File::exists($full_path_before)){
throw new \Exception("Error moving folders");
}
$move_files = $full_path_after !== $full_path_before;
if ($move_files){
File::move($full_path_before, $full_path_after);
}
Use This Method This Will Work 100%
if($request->hasFile('filename')){
$file = $request->filename;
$file_new_name = $file->move("upload/posts/", $file->getClientOriginalName());
$post->filename = $file_new_names;
}
Remember filename is your < img name="filename" >

Categories