What best practices should be followed when implementing file upload functionality in PHP for mobile users?

When implementing file upload functionality in PHP for mobile users, it is important to ensure that the file upload form is mobile-friendly and easy to use on smaller screens. Additionally, it is recommended to validate the file type and size before allowing the upload to prevent any security risks. Finally, consider using AJAX to handle the file upload process asynchronously to improve the user experience.

// HTML form for file upload
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="Upload">
</form>

// PHP code to handle file upload
if(isset($_FILES['file'])) {
    $file = $_FILES['file'];
    
    // Validate file type
    $allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
    if(!in_array($file['type'], $allowedTypes)) {
        die('Invalid file type. Only JPEG, PNG, and GIF files are allowed.');
    }
    
    // Validate file size
    if($file['size'] > 5242880) {
        die('File is too large. Maximum file size is 5MB.');
    }
    
    // Move uploaded file to desired directory
    move_uploaded_file($file['tmp_name'], 'uploads/' . $file['name']);
    
    echo 'File uploaded successfully.';
}