I have setup up a simple file upload system using laravel. To store the image, I set up my controller as
public function store(Request $request)
{
$menu = new Menu;
$menu->title = $request->get('menuTitle');
$menu->user_id = Auth::id();
if($request->hasFile('menuImage')){
$filenameWithExt = $request->file('menuImage')->getClientOriginalName();
$fileName = pathinfo($filenameWithExt,PATHINFO_FILENAME);
$ext = $request->file('menuImage')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$ext;
$path = $request->file('menuImage')->storeAs('public/menu',$fileNameToStore);
}
else{
$fileNameToStore = 'blank.jpg';
}
$menu->image_name = $fileNameToStore;
$menu->save();
return redirect(route('menu.create'))->with('success',$menu->title." Added successfully");
}
In the view I set it up as
<a href="#">
<img class="img-fluid rounded mb-3 mb-md-0" src= alt="">
</a>
In the browser the link is
<img class="img-fluid rounded mb-3 mb-md-0" src="http://127.0.0.1:8000/storage/menu/Screenshot_20190517_023111_1558916284.png" alt="">
While developing, I was working on ubuntu and everything works fine. However, I transferred my files to my mac and using xampp I was able to get everything working. However the images do not load anymore. I get a 404 error when I check the console. However, the images are able to upload to the right folder.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire