How can the $_FILES array be properly accessed and utilized in PHP when uploading files?
When uploading files in PHP, the $_FILES array is used to access information about the uploaded file, such as its name, type, size, and temporary location. To properly utilize this array, you need to ensure that the form containing the file upload field has the attribute enctype="multipart/form-data" set. Once the form is submitted, you can access the uploaded file information from the $_FILES array and move the file to its desired location on the server.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload">
<input type="submit" value="Upload File">
</form>
```
```php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$file_name = $_FILES["fileToUpload"]["name"];
$file_type = $_FILES["fileToUpload"]["type"];
$file_size = $_FILES["fileToUpload"]["size"];
$file_tmp = $_FILES["fileToUpload"]["tmp_name"];
// Move the uploaded file to a desired location
move_uploaded_file($file_tmp, "uploads/" . $file_name);
echo "File uploaded successfully!";
}
?>