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" >
Related
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
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;
}
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'));
I am working with laravel 5.6 for upload a file using HTML form but I am having a problem while retrieving a file in a controller.
When I retrieve it using this way it returns ok return $request; it is retrieving in controller but not in hasFile() function or
getClientOriginalExtension()
I have searched many website but problem is still staying here. All I have matched but not working.
Error: "Call to a member function getClientOriginalExtension() on null"
Html Form view:
<form action="{{url('/changeProductImage')}}" enctype="multipart/form-data" method="post">
{{csrf_field()}}
<input type="file" name="photo" id="photo" class="btn btn-secondary">
<hr>
<button type="submit" value="upload" class="form-control btn btn-primary btn-fill btn-lg">Upload Image</button>
</form>
Route:
Route::post('/changeProductImage','ProductsController#upload');
Controller:
function upload(Request $request)
{
$image = $request->file('photo');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return back()->with('success', 'Image Uploaded Successfully');
}
It's really hard to say what's the problem looking at this piece of code. Assuming you really send this like this (you don't use AJAX somewhere for example), it should work.
What I would do, I would make sure that in your controller you have:
use Illuminate\Http\Request;
line just to make sure this request you have in your controller is really this one.
Also keep in mind, you should probably have some validation to make sure that file is really type you want or at least you should have some additional logic for example:
function upload(Request $request)
{
if (!$request->hasFile('photo') {
return back()->with('error', 'Missing image!');
}
$image = $request->file('photo');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return back()->with('success', 'Image Uploaded Successfully');
}
May be you are using this statement
use Illuminate\Support\Facades\Request;
insted of
use Illuminate\Http\Request;
View
<form method="post" enctype='multipart/form-data' action="<?php base_url();?>edituserpic" name="form_editpic" id="form_editpic" class="avatar">
<div class="slim"
data-label="Drop your avatar here"
data-size="240,240"
data-ratio="1:1">
<input type="file" name="avatar" required />
</div>
<button type="submit">Upload now!</button>
</form>
Controller
public function edit_user_pic() {
$data['baseurl'] = $this->config->item('base_url');
// Pass Slim's getImages the name of your file input, and since we only care about one image, postfix it with the first array key
$image = $this->slim->getImages('avatar')[0];
*/
// Grab the ouput data (data modified after Slim has done its thing)
if ( isset($image['output']['data']) )
{
// Original file name
$name = $image['output']['name'];
// Base64 of the image
$data = $image['output']['data'];
}
}
I'm just stuck in server side how to save to get output image and save to database.
I am getting $name undefined. This mean output is empty.
If anyone used it and able to help that would be great.
If you've setup to use a different name than "slim", pass it along.
$images = Slim::getImages('avatar');
$image = $images[0];
It works for me.