PHP allows you to upload files or images to the server with minimum code. In this article, we'll guide you how to upload files in PHP and create a PHP script to upload files to a server directory.
You can upload any sorts of files, including photos, to the server with PHP using this sample PHP file upload script. This might be good for student project assignment for very basic beginner.
These following steps to make upload file possible.
First, you have to create a form that allow user browse files from their computer into browsers (Chrome, Safari, Firefox...). Basically, form to upload file make sure there are following required attributes.
method="post"
enctype="multipart/form-data"
Before go further make sure yourself understand basic HTML and PHP. Here is the code that is important point in whole HTML page.
<form action="upload.php" method="post" enctype="multipart/form-data">
Browse File to Upload:
<input type="file" name="files[]" multiple>
</br>
<input type="submit" name="upload" value="Upload">
</form>
HTML element type="file"
help browser to translated as button browse file in computer, while files[]
is variable to store temporally value of multiple file in HTML element. multiple
attribute is to allow user select multiple files at the same time.
This form will be submitted to the server-side script file name upload.php for uploading the selected files to the server. The below HTML code is full snippet code.
Create file index.html
<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
</head>
<body>
<h1 class="title">Upload File - CamboTutorial.com </h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
Browse File to Upload:
<input type="file" name="files[]" multiple>
</br>
<input type="submit" name="upload" value="Upload">
</form>
</body>
</html>
This step we will create a PHP file to write upload file script, as well known server side to handle the file upload process using raw PHP. The following functions are important working for upload files.
is_dir()
- to check whether is directory or notmkdir()
- to create directorymove_uploaded_file()
- to move file upload from temporally file to actually directory upload (upload file).Now below script to upload multiple file with server code.
Create file upload.php
<?php
if(isset($_FILES))
{
// Count total files
$countfiles = count($_FILES['files']['name']);
$dir = "upload/";
// if there no directory upload then create once
if(!is_dir($dir))mkdir($dir);
// Looping all files
for($i=0; $i< $countfiles; $i++)
{
// Get each file by index
$filename = $_FILES['files']['name'][$i];
// Upload file
move_uploaded_file($_FILES['files']['tmp_name'][$i],$dir.$filename);
}
echo "Uploaded $countfiles file(s)!";
}
else
{
echo "No file to upload";
}
?>
If you found this tutorial helpful then don't forget to share. Please have a nice day!
You might also like..
PHP
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him