What is the correct way to upload a file using PHP?
To upload a file using PHP, you need to create a form with the enctype attribute set to "multipart/form-data" and a file input field. In the PHP script that handles the form submission, you can use the $_FILES superglobal to access the uploaded file and move it to the desired location on the server.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
```
```php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>