What are some best practices for storing avatars in a PHP application?

When storing avatars in a PHP application, it is best practice to save the avatars in a separate directory outside of the web root to prevent direct access. You should generate a unique filename for each avatar to avoid conflicts and store the file path in the database for retrieval.

// Define the directory to store avatars
$avatarDirectory = '/path/to/avatar/directory/';

// Generate a unique filename for the avatar
$avatarFilename = uniqid() . '_' . $_FILES['avatar']['name'];

// Move the uploaded avatar to the avatar directory
move_uploaded_file($_FILES['avatar']['tmp_name'], $avatarDirectory . $avatarFilename);

// Store the file path in the database for retrieval
$avatarPath = $avatarDirectory . $avatarFilename;
// Save $avatarPath in the database