I want to upload file in my app.
This is the blade file .
<form action="/fileUploader " files="true" method="post" role="form" name="file" id="chan" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="image">
<button type="submit" name ="Upload_File">Upload File</button>
</div>
</form>
This is my controller file
public function viewFile()
{
return View::make('/fileUploader');
}
public function showfileupload(Request $request)
{
$file = $request -> file('image');
// show the file name
echo 'File Name : '.$file->getClientOriginalName();
echo '<br>';
// show file extensions
echo 'File Extensions : '.$file->getClientOriginalExtension();
echo '<br>';
// show file path
echo 'File Path : '.$file->getRealPath();
echo '<br>';
// show file size
echo 'File Size : '.$file->getSize();
echo '<br>';
// show file mime type
echo 'File Mime Type : '.$file->getMimeType();
echo '<br>';
// move uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
This is the web.php file
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('/fileUploader', 'channelController#showfileupload');
I'm getting an error called FatalThrowableError in channelController.php line 48:
Call to a member function getClientOriginalName() on null.
How can I solve this problem
Most likely, you are trying to call a method - getClientOriginalName() - on a object that doesn't exist, so it's null. That jives with the error message you are seeing.
I'm not sure why, but we can start working backwards. Let's use an if statement with the hasFile() method to verify that a file is actually present on the request before attempting to move() it.
if ($request->hasFile('image')) {
$file->move($destinationPath,$file->getClientOriginalName());
}
If you implement the above, does the error still exist?
Here are the Laravel 5.3 Docs on file uploads. It may give you some more ideas.
If you're finding that users are posting files and hasFile() is still returning boolean FALSE, then you may want to go digging into the php.ini file and take a look at the Post_max_size or upload_max_size values to make sure that we aren't blocking large uploads.
VIEW
{!! Form::open(['route'=>'fileUploader', 'id'=>'chan', 'files' => true] )!!}
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="file" name="image">
<button type="submit">Upload File</button>
</div>
{!! Form::close()!!}
ROUTES
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('fileUploader', array(
'as' => 'fileUploader',
'uses' => 'channelController#showfileupload',
));
CONTROLLER
public function showfileupload(Request $request){
$file = $request -> file('image');
dd($file); // This work well for me and return information about the image
}
Do copy and past! i hope it work, let me know any error and result!
Related
When I am uploading an image to the form and returning it from the controller, the image name and extention are changing.I am a beggar, so if I make a mistake while asking a question, I would like to apologize.
This is my form:
<form action="{{ route('admin.slider.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-12">
<label class="control-label">Image</label>
<input type="file" name="image">
</div>
</div
<button type="submit" class="btn btn-success">Save</button>
</form>
This is my controller:
public function store(Request $request)
{
$this->validate($request, [
'image' => 'required|mimes:jpeg,bmp,png,jpg',
]);
$image = $request->file('image');
return $image;
}
My image file name is :demo.jpg
Controller return result is like that:
C:\xampp\tmp\php5E86.tmp
This is the same result when I give another picture, only the last four characters are changing.
C:\xampp\tmp\phpF239.tmp
It is very helpful to know why I am getting .tmp file return.
use getClientOriginalName to get orginal file name
$request->image->getClientOriginalName()
To get file extension
$request->image->extension();
or
$name = $request->file('image')->getClientOriginalName();
$extension = $request->file('image')->extension();
Ref:https://laravel.com/docs/8.x/filesystem#other-uploaded-file-information
i want to insert link of an image into a field in the database like annonces\August2020\image.jpg in storage, but it gives me error:The POST method is not supported for this route. Supported methods: GET, HEAD.
file.blade.php
<form action="/file" method="post" enctype="multipart/form-data">
#csrf
<div class="form-group">
<input type="file" class="form-control-file" name="file" id="file" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">Please upload a valid image file. Size of image should not be more than 2MB.</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
web.php
Route::get('file', 'fileController#index');
Route::get('save', 'fileController#save');
FileController.php
public function save()
{
request()->validate(['file' => 'required|mimes:doc,docx,pdf,txt|max:2048',]);
if ($files = $request->file('fileUpload')) {
$destinationPath = 'public/file/'; // upload path
$profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profilefile);
$insert['file'] = "$profilefile";
}
$check = Document::insertGetId($insert);
return Redirect::to("file")
->withSuccess('Great! file has been successfully uploaded.');
}
as the error said, you need to declare the route as POST
Route::post('save', 'fileController#save');
Also, put the correct route into the form action attribute
<form action="/save" method="post" enctype="multipart/form-data">
you are using post method in the form and then in the web.php you are using get method which is wrong.
in We.php use something like below:
Route::post('save', 'fileController#save');
Also please change the action attribute value to save which is a url in web.php because you are calling save method in the Controller
<form action="/save" method="post" enctype="multipart/form-data">
I was working on a project and was trying to develop a file uploading system for skins.
When I tried to upload my skin, I was given "Call to a member function storeAs() on null"
public function uploadSkin(Request $request)
{
/* $request->validate([
'skins' => 'required|mimes:png|max:1024',
]); */
$storage_dir = storage_path('app/skins');
$request->file('skins')->storeAs($storage_dir, Auth::user->name . '.png');
return route('settings')->with('success', 'skin uploaded :)');
}
Form code:
<form method="post" enctype="multipart/form-data" action="/settings">
#csrf
<br/>
<div class="form-group">
<input type="file" class="form-control-file" id="skins" name="skins" required>
</div>
<button type="submit" class="btn btn-success">Upload</button>
</form>
To store a file like an image or any kind of files you can use a code like this:
public function uploadSkin(Request $request){
$image = $request->file('skins');
if ($image != null) {
$image->move('uploads/skins/', Auth::user()->name . $image->getClientOriginalExtension());
}
return route('settings')->with('success', 'skin uploaded :)');
}
To uppload a file there are various ways in the laravel but for now you can try this to simply move your file to your directory:
if($files= $request->file('skins')){
$files->move('uploads/skins/', Auth::user->name . '.png');
}
I want to upload images with laravel:I created the field in mysql:
imagepath varchar 250
Created the form:
<form method="post" id="formId" action="/ticket" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group ">...other fields
<div class="form-group">
<input type="file" class="form-control-file" name="imageFile" id="imageFile" aria-describedby="fileHelp">
</div>
In my controller:
public function store(Request $request)
{
$fileName = request()->imageFile->getClientOriginalExtension();
dd($fileName);//echos ".jpg"
$tickes = Users::create(['imagePath' => $fileName]);
}
However my db is always null!I want to store it in my public/storage folder in Laravel.
Please help!
you can use this code for upload image to `public/storage/ directory :
if ($request->hasFile('input_img')) {
if($request->file('input_img')->isValid()) {
try {
$file = $request->file('input_img');
$name = rand(11111, 99999) . '.' . $file->getClientOriginalExtension();
# save to DB
$tickes = Users::create(['imagePath' => 'storage/'.$name]);
$request->file('input_img')->move("storage", $name);
} catch (Illuminate\Filesystem\FileNotFoundException $e) {
}
}
}
This code is only saving the file extension, not the image path.
You need to save the file to storage before your database can get a path to it.
Laravel has really good documentation and this link will help you figure this out. https://laravel.com/docs/5.6/filesystem
I'm working on the database restore menu for an application I created, I'm using sqlite for my database, and I want to put my previously backed up database file to the database directory
backupAndRestore.blade.php
<form method="POST" action="/admin/backupAndRestore/restore" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="file-field input-field">
<div class="btn">
<span><i class="material-icons">file_upload</i></span>
<input type="file" name="database" accept=".sqlite">
</div>
<div class="file-path-wrapper">
<input type="text" class="file-path" placeholder="Example: file_name.sqlite" readonly>
</div>
</div>
<button type="submit" class="btn">restore</button>
</form>
web.php
Route::post('/admin/backupAndRestore/restore', 'DatabaseController#restore');
DatabaseController.php
public function restore(Request $request)
{
$database = $request->database;
$file_name = 'database2.sqlite';
$path = database_path($file_name);
file_put_contents($path, $database);
return $path;
}
my code works, the file is stored in the database directory, the problem is when I backup my database file size is 22kb, but when I restore it and I check the database directory, the file is 1kb size and when I open using sublime file contents as shown below
Images
which I expect as shown below
Images
can someone tell where my fault is?
You are using file_put_contents which inserts the contents in the given file and does not move the file.
If you want to store a file in database path..
public function restore(Request $request)
{
$this->validate($request, [
'database' => 'required|file|mimes:sqlite'
]);
$databaseFile = $request->database;
$fileName = 'database2.sqlite';
$databaseFile->move(database_path(), $fileName);
// return success message...
}
For more information uploading files: https://laravel.com/docs/5.4/requests#files.