Level 8
Check the storage app public folder. If it is in there you have to do a php artisan storage:link this will create a copy inside your /public folder and you will access it like this /public/storage/images/myimage.png.
The below code will just store the images in a file called images in the public directory. So you will access it /public/images.
public function safetyStore(Request $request)
{
// Validation rules
$validatedData = $request->validate([
'file' => 'required|file|mimes:jpeg,png,jpg,gif|max:2048', // Example rules - adjust as needed
]);
if ($request->hasFile('file')) {
$file = $request->file('file');
// Generate a random name for the file
$fileName = Str::random(25) . '_' . date('Ymd') . '.' . $file->getClientOriginalExtension();
// Move the uploaded file to the /images directory
$file->move(public_path('images'), $fileName);
// You can also store file details in the database or perform other operations
return "File has been uploaded successfully!";
}
return "No file uploaded.";
}
1 like