File upload is quite easy and compact in PHP. We can upload a single or multiple files in PHP with just a few lines of code.
PHP can handle uploaded files in POST requests with below constants.
Read Also: PHP Multiple File Upload Using AJAX
Index
In this step, we create a form to upload a file in PHP.
<!DOCTYPE html>
<html>
<body>
<form action="upload_handle.php" method="post" enctype="multipart/form-data">
<label>Select File to Upload:</label>
<input type="file" name="file"/>
<input type="submit" name="submit"/>
</form>
</body>
</html> NOTE: enctype=”multipart/form-data” is required to upload files in PHP via POST request in forms.
In this step, we create a new PHP file named upload_handle.php to handle the uploading process via PHP. Please read our article on $_FILES to understand the uploading process.
<?php
$target_path = "YOUR_PATH";
$target_path = $target_path.basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "File uploaded successfully...";
} else{
echo "Error, file not uploaded...";
}
?> Read Also: move_uploaded_file() function in PHP
In the above article, we learned the file upload process in PHP. We can upload any file via the above code to our server.
Enjoy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…