Reputation: 199
I have some question.
I need to save my file to storage/public/apps
with original name and not with name FAJFJAKJ@$!@@!#
- look's like this.
What I missing there??
Controller
public function upload(Request $request)
{
$request->file('file')->save('public/apps');
return redirect()->back()->with('message', 'File Upload Successfully!');
}
web.php
Route::prefix('admin')->middleware('auth')->group(function(){
Route::get('/', 'Admin\IndexController@index');
Route::get('dashboard', 'Admin\IndexController@index')->name('dashboard');
Route::get('upload-files', 'Admin\UploadController@index');
Route::post('upload-files', 'Admin\UploadController@upload');
Route::get('links', 'Admin\LinkController@index')->name('links');
Route::get('links/create', 'Admin\LinkController@createUlr')->name('links.create');
Route::post('links/create', 'Admin\LinkController@store')->name('links.store');
Route::get('links/{id}', 'Admin\LinkController@linkById')->name('link.chose');
Route::post('links/{id}', 'Admin\LinkController@update')->name('link.delete');
Route::delete('links/{id}', 'Admin\LinkController@delete')->name('link.delete');
});
Form
<form class="col-lg-push-6" action="/admin/upload-files" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">New File</label>
<br>
<br>
<br>
<input type="file" class="form-control" size="100" name="file">
</div>
<button class="btn btn-primary" type="submit">Create</button>
</form>
Upvotes: 0
Views: 3363
Reputation: 106
Here you can see below:
<?php
use Illuminate\Http\Request;
//importing user model
use App\User;
class UploadImage
{
function avatarUpload(Request $request)
{
$file = $request->file('avatar_img');
//getting original name of file
$fileName = $file->getClientOriginalName();
$post = [
'avatar_img'=> $fileName,
'id'=> $request->user()->id
];
//saving into database
$create = User::create($post);
if($create)
{
//store file, it will store in avatars folder inside app folder
$store = $file->storeAs('avatars',$fileName);
return $store ? 'Avatar image uploaded' : 'Avatar image store failed';
}
return 'Avatar image upload failed';
}
}
?>
Upvotes: 0
Reputation: 12835
Whenever handling file uploads in Laravel Controller, below things can be helpful
public function store(Request $request)
{
//Validate request data if there are other fields as well
// do not include image fields in validation rules
$validatedData = $request->validate($rules, $request->except('image'));
//Check if $request has uploaded file and whether it's a valid file
if($request->hasFile('image') && $request->file('image')->isValid()) {
//Custom name pattern as per application preference
$filename = time() . '.' . $request->file('image')->extension();
//Or - Get the original name of the uploaded file
$filename = $request->file('image')->getClientOriginalName();
//Store the file in desired directory and assign the path to the image field in validated data
$validatedData['image'] = $request->file('image')->storeAs('images', $filename);
}
//Fill the values from validatedData and save the record
$model->fill($validatedData);
$model->save();
}
Upvotes: 2
Reputation: 1266
If you wish to upload the file with the original name you can use getClientOriginalName()
function which will return the original name of file , here is some code which will help to upload your file to your desired location and i would highly recommend to wrapping your paths in Variables.
public function upload(Request $request)
{
$imageName = "{$request->file->getClientOriginalName()}.{$request->file->getClientOriginalExtension()}";
$imagePath = "public/apps/{$imageName}";
$request->file->move(public_path("public/apps"), $imageName);
}
Upvotes: 0