How can developers optimize their PHP scripts to efficiently handle 50 MB file uploads without impacting server performance?
When handling large file uploads in PHP, developers can optimize their scripts by adjusting the PHP configuration settings to allow for larger file uploads and adjusting the server settings to handle the increased load efficiently. Additionally, developers can use techniques like streaming file uploads, chunked uploads, and asynchronous processing to minimize the impact on server performance.
// Increase PHP settings for handling large file uploads
ini_set('upload_max_filesize', '50M');
ini_set('post_max_size', '50M');
// Process the file upload using streaming or chunked uploads
// Example code for handling file upload using move_uploaded_file function
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$file = $_FILES['file'];
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($file['name']);
if (move_uploaded_file($file['tmp_name'], $uploadFile)) {
echo 'File uploaded successfully!';
} else {
echo 'Failed to upload file.';
}
}