Are there any best practices for handling multipart/form-data uploads in PHP without using cURL?
When handling multipart/form-data uploads in PHP without using cURL, you can use the $_FILES superglobal array to access the uploaded file data. You can move the uploaded file to a specified directory using the move_uploaded_file() function. Make sure to validate the file type, size, and handle any errors that may occur during the upload process.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_FILES['file'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$upload_dir = 'uploads/';
if (move_uploaded_file($file_tmp, $upload_dir . $file_name)) {
echo 'File uploaded successfully.';
} else {
echo 'Error uploading file.';
}
} else {
echo 'No file uploaded.';
}
}
?>