How to Upload Files to a Server Using PHP
Last updated: 21.02.2026
Views: 265
Uploading files to a website is a common task. Consider uploading files to a PHP server using the POST method. This will require a form with the “file” field type and an enctype attribute with a multipart/form-data value. We will not use AJAX in this example, but will do a regular HTML form submission.
HTML
<form method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="2097152" /> <input type="file" name="uploaded_file"> <input type="submit" name="submit" value="send"> </form>
The field with name=”MAX_FILE_SIZE” must be placed above the input type=”file”, the value is set in bytes. The field is optional and the check must be still on the server. After sending the data of the uploaded file get into the $_FILES array
PHP
if (isset($_FILES["uploaded_file"])) { // field´s name type="file"
$maxsize = 1024 * 1024* 2; // limited the allowed file size to 2 MB
$errors = [];
// valid file extensions
$acceptable = [
'application/pdf',
'image/jpeg',
'image/jpg',
'image/gif',
'image/png'
];
if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {
if (($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
}
if (!in_array($_FILES['uploaded_file']['type'], $acceptable) && (!empty($_FILES["uploaded_file"]["type"]))){
$errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
}
if (!count($errors)) {
$uploaddir = __DIR__ . '/upload/'; // upload folder
$uploadfile = $uploaddir . $_FILES['uploaded_file']['name'];
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadfile);
} else {
foreach($errors as $error) {
echo $error . '<br />';
}
}
}
}
You can upload multiple files at once. PHP supports the ability to transfer an array of HTML including files.
HTML
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
<input type="file" name="pictures[]">
<input type="file" name="pictures[]">
<input type="file" name="pictures[]">
<input type="submit" name="submit" value="send">
</form>
PHP
if (isset($_FILES["pictures"])) { // field´s name type="file"
$maxsize = 1024 * 1024* 2; // limited the allowed file size to 2 MB
$errors = [];
// valid file extensions
$acceptable = [
'application/pdf',
'image/jpeg',
'image/jpg',
'image/gif',
'image/png'
];
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
if (is_uploaded_file($_FILES['pictures']['tmp_name'][$key])) {
if (($_FILES['pictures']['size'][$key] >= $maxsize) || ($_FILES["pictures"]["size"][$key] == 0)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
}
if (!in_array($_FILES['pictures']['type'][$key], $acceptable) && (!empty($_FILES["pictures"]["type"][$key]))){
$errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
}
if (!count($errors)) {
$uploaddir = __DIR__ . '/upload/'; // upload folder
$uploadfile = $uploaddir . $_FILES['pictures']['name'][$key];
move_uploaded_file($_FILES['pictures']['tmp_name'][$key], $uploadfile);
} else {
foreach($errors as $error) {
echo $error . '<br />';
}
}
}
}
}
}
Similar posts:
-
Caching Data to a File Using PHP
Sometimes it becomes necessary to limit the number of requests to an external data source, especially when the data does not change frequently. For example, this can be u...
-
How to Send HTML Form Data to Email Using PHP
Sending a form to email is an important and common way of communicating with a web resource user. Let's write a simple form for sending data to email using the PHP mail()...
-
How to Remove the jQuery Migrate Script from WordPress
If your WordPress project uses jQuery, then by default, WordPress also loads the jQuery Migrate script along with it. In 99% of cases, you don’t actually need this script...
Leave a Reply