Where can I find a concrete sequence or example of how to handle file uploads in PHP?
Handling file uploads in PHP involves using the $_FILES superglobal array to access the uploaded file information and move the file from the temporary directory to a permanent location on the server. This process typically involves validating the file type and size, checking for errors during the upload, and setting the appropriate permissions on the uploaded file.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$file = $_FILES["file"];
$uploadDir = "uploads/";
$uploadFile = $uploadDir . basename($file["name"]);
if (move_uploaded_file($file["tmp_name"], $uploadFile)) {
echo "File uploaded successfully.";
} else {
echo "Error uploading file.";
}
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>