What potential pitfalls can arise when using file_get_contents & file_put_contents for file uploads in PHP?

Using file_get_contents & file_put_contents for file uploads in PHP can potentially lead to security vulnerabilities such as allowing arbitrary file uploads or overwriting existing files. To mitigate these risks, it is recommended to use functions like move_uploaded_file() which is specifically designed for handling file uploads securely.

// Example of securely handling file uploads using move_uploaded_file()

// Check if a file was uploaded
if(isset($_FILES['file'])){
    $file = $_FILES['file'];

    // Check for errors during upload
    if($file['error'] === UPLOAD_ERR_OK){
        $uploadDir = 'uploads/';
        $uploadPath = $uploadDir . basename($file['name']);

        // Move the uploaded file to the uploads directory
        if(move_uploaded_file($file['tmp_name'], $uploadPath)){
            echo 'File uploaded successfully!';
        } else {
            echo 'Failed to upload file.';
        }
    } else {
        echo 'Error during file upload.';
    }
}