afoysal's avatar

Upload image

I am using below Trait.

public function safetyStore(Request $request ) {        
        $images = [];
        $files = $request->file('image');
        if($request->hasFile('image'))
        {
            foreach ($files as $file) {
                $file->store('users/helllo/messages');
                $images[] = $file->hashName();
            }
        }
}

Where is file uploading ? Where can I find uploaded images ?

0 likes
1 reply
Randy_Johnson's avatar

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

Please or to participate in this conversation.