Welcome to our PHP tutorial on the $_FILES reference! In this lesson, we'll dive deep into understanding how to handle file uploads in PHP. This knowledge is essential for building dynamic web applications, from simple form submissions to complex content management systems. Let's get started! π
$_FILES πThe $_FILES is a superglobal array in PHP that is automatically populated when a user submits a form with file(s) attached. Each file uploaded is represented as an associative array within $_FILES. π‘ Remember: $_FILES is only populated when a form is submitted using the POST method.
$_FILES array π―To access the $_FILES array, simply use the variable name in your PHP script. Let's create a simple HTML form for file upload and a PHP script to handle the upload.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload Form</title>
</head>
<body>
<h1>File Upload Form</h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="my_file" id="my_file">
<br><br>
<input type="submit" value="Upload File" name="submit">
</form>
</body>
</html><?php
// Check if file is uploaded
if(isset($_FILES['my_file'])) {
// Get file details
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_tmp = $_FILES['my_file']['tmp_name'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
// File extension
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
// List of allowed file extensions
$allowed_exts = array("jpg", "jpeg", "png", "gif");
// Check for errors
if(!empty($file_error)) {
switch($file_error) {
case 1:
echo "The uploaded file is too large.";
break;
case 2:
echo "The uploaded file is too small.";
break;
case 3:
echo "Only part of the file was uploaded.";
break;
case 4:
echo "No file was uploaded.";
break;
default:
echo "An error occurred during the upload process.";
}
}
// Check file extension
if(in_array($file_ext, $allowed_exts)) {
// Move the uploaded file to the desired directory
$target_dir = "uploads/";
$target_file = $target_dir . basename($file_name);
if(move_uploaded_file($file_tmp, $target_file)) {
echo "The file " . basename($file_name) . " has been uploaded.";
} else {
echo "There was an error uploading the file.";
}
} else {
echo "Only JPG, JPEG, PNG, and GIF files are allowed.";
}
}
?>Which PHP variable holds the file uploaded by the user in a form?
π Note: In the example above, we have allowed only jpg, jpeg, png, and gif file types for uploads. You can adjust the allowed file types according to your application's requirements.
$file_tmp variable to store the uploaded file temporarily before moving it to the desired location.move_uploaded_file() to move the uploaded file to the desired location.In this lesson, we've covered the basics of handling file uploads in PHP using the $_FILES superglobal array. By understanding the concepts discussed here, you'll be well-equipped to build dynamic web applications that require file upload functionality. Keep practicing, and happy coding! π