How can PHP developers handle file uploads securely in a shared hosting environment where root access is restricted?

In a shared hosting environment where root access is restricted, PHP developers can handle file uploads securely by setting proper file permissions, validating file types and sizes, and storing uploaded files outside of the web root directory to prevent direct access. Additionally, implementing measures such as renaming uploaded files and using secure upload libraries can help mitigate security risks.

// Example PHP code snippet for handling secure file uploads in a shared hosting environment

$uploadDir = '/path/to/uploads/';
$allowedTypes = ['image/jpeg', 'image/png'];
$maxFileSize = 1048576; // 1MB

if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    $fileType = $_FILES['file']['type'];
    $fileSize = $_FILES['file']['size'];

    if (in_array($fileType, $allowedTypes) && $fileSize <= $maxFileSize) {
        $fileName = uniqid() . '_' . $_FILES['file']['name'];
        $uploadPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadPath)) {
            echo 'File uploaded successfully.';
        } else {
            echo 'Error uploading file.';
        }
    } else {
        echo 'Invalid file type or size.';
    }
} else {
    echo 'Error uploading file.';
}