What are the potential pitfalls of using move_uploaded_file() to save files to multiple locations in PHP?

Potential pitfalls of using move_uploaded_file() to save files to multiple locations in PHP include potential security vulnerabilities if not properly sanitized, increased server load due to multiple file operations, and potential file corruption if one of the moves fails. To mitigate these risks, it is recommended to validate and sanitize user input, handle errors gracefully, and consider using a more robust file handling solution like a file storage service.

<?php
// Example of saving uploaded file to multiple locations with error handling

$uploadedFile = $_FILES['file'];
$uploadDirectory = 'uploads/';

if ($uploadedFile['error'] !== UPLOAD_ERR_OK) {
    echo 'Error uploading file.';
    exit;
}

$filename = $uploadedFile['name'];
$fileTmpName = $uploadedFile['tmp_name'];

// Save file to first location
if (!move_uploaded_file($fileTmpName, $uploadDirectory . $filename)) {
    echo 'Error moving file to first location.';
    exit;
}

// Save file to second location
if (!copy($uploadDirectory . $filename, $uploadDirectory . 'backup_' . $filename)) {
    echo 'Error copying file to second location.';
    // You may choose to handle this differently based on your requirements
}

echo 'File uploaded and saved to multiple locations successfully.';
?>