How can PHP sessions help in identifying uploaded files by different users?
When multiple users are uploading files to a website, it can be challenging to identify which files belong to which user. PHP sessions can help in this scenario by assigning a unique session ID to each user when they log in or visit the website. This session ID can be used to track the user's uploaded files and associate them with the correct user.
<?php
// Start the session
session_start();
// Check if user is logged in
if(isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
// Save uploaded file with user's ID in the filename
$file_name = $user_id . '_' . $_FILES['file']['name'];
// Move uploaded file to desired directory
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $file_name);
echo 'File uploaded successfully!';
} else {
echo 'User not logged in. Please log in to upload files.';
}
?>