Reputation: 85
I want save uploaded file with original name.What i must add to this code?
Below's my code
public function store(Request $request)
{
if($request->hasFile('image'))
{
$file = $request->file('image');
$originalname = $file->getClientOriginalName();
$filename =$originalname;
$file->move('public/', $filename);
}
Upvotes: 4
Views: 12605
Reputation: 3455
Upload files with original fileName.
if ($request->hasFile('image')) {
$file_path = $request->file('image')->storeAs('dir_name', request()->file('image')->getClientOriginalName(), 'optional_disk_name');
}
// pass the disk name as the third argument to the storeAs method: ex. local,s3,etc
Upvotes: 1
Reputation: 1003
You may use the storeAs
method, which receives the path, the file name, and the (optional) disk as its arguments:
public function store(Request $request)
{
if($request->hasFile('image'))
{
$file = $request->file('image');
$originalname = $file->getClientOriginalName();
$path = $file->storeAs('public/', $originalname);
}
}
Upvotes: 7
Reputation:
Try this:
if($request->hasFile('image'))
{
$file = $request->file('image');
$originalname = $file->getClientOriginalName();
$img = Image::make($file->getRealPath());
$img->stream();
Storage::disk('local')->put('images/'.$originalname, $img, 'public');
}
Upvotes: 0