Upload file via PHP
Uploading a file in PHP is an easy task and the user customization on websites needs to be allowed as this has become very important these days.
Follow the steps:
<html lang="en">
<body>
<div class="container">
<h2>File Upload Example</h2>
<form role="form" action="index.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="fileToUpload">Select File</label>
<input class="form-control" type="file" name="fileToUpload" id="fileToUpload" required data-validation-required-message="Please define image path.">
</div>
<div id="success">
<div class = "<?php echo $resultClass;?>"><?php echo $result;?></div>
</div>
<button type="submit" name="submit" class="btn btn-success">Upload</button>
</form>
</div>
</body>
</html>
- Configure The "php.ini" File
file_uploads = On
- Create 'index.php' file
Paste the following code in the file
<html lang="en">
<body>
<div class="container">
<h2>File Upload Example</h2>
<form role="form" action="index.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="fileToUpload">Select File</label>
<input class="form-control" type="file" name="fileToUpload" id="fileToUpload" required data-validation-required-message="Please define image path.">
</div>
<div id="success">
<div class = "<?php echo $resultClass;?>"><?php echo $result;?></div>
</div>
<button type="submit" name="submit" class="btn btn-success">Upload</button>
</form>
</div>
</body>
</html>
- Ensure that the method="post" is used by the form.
- enctype="multipart/form-data" is an attribute which determines how the form is encoded. Ensure the usage of this attribute in the form before submitting.
- type="file" will be used to select file from the computer.
- Create a new folder with the name ‘files’ in the same place where index.php resides in:
- Now place the following script at the top of the <!DOCTYPE html> tag.
<?php
if
( !empty($_POST)){
$dir
= "files/";
$file_name
= basename($_FILES['fileToUpload']['name']);
$file_path
= $dir . $file_name;
$uploadOk
= 1;
if
($_FILES["fileToUpload"]["size"] > 500000) {
$uploadOk
= 0;
}
if
($uploadOk == 0) {
$resultClass=
'alert-danger alert';
$result=
'Sorry, your file was not uploaded. Please try later';
}
}
?>
- $dir = "files/" : It specifies the directory of the file.
- $file_path : It specifies the path of the file.
- Check if File Already Exists:
if (file_exists($file_path)) {
$resultClass=
'alert-danger alert';
$result=
'File exist already. File uploading ignored.';
}
- Limit File Size:
A limit can be put on the size of the file to be uploaded:
if ($_FILES["fileToUpload"]["size"] > 500000) {
$uploadOk
= 0;
}
if ($uploadOk
== 0) {
$resultClass=
'alert-danger alert';
$result=
'Sorry, your file was not uploaded. Please try later';
}
- Upload File:
Now Upload the file to the desired path.
else if
(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$file_path)) {
$resultClass=
'alert-success alert';
$result=
'File uploaded successfully';
}
else
{
$resultClass=
'alert-danger alert';
$result=
'Sorry, your file was not uploaded. Please try later';
}
And You are done :)
Complete Upload File PHP
Script:
This is how the complete Upload File PHP Script looks like:
if ( !empty($_POST)){
$dir
= "files/";
$file_name
= basename($_FILES['fileToUpload']['name']);
$file_path
= $dir . $file_name;
$uploadOk
= 1;
if
($_FILES["fileToUpload"]["size"] > 500000) {
$uploadOk
= 0;
}
if
($uploadOk == 0) {
$resultClass=
'alert-danger alert';
$result=
'Sorry, your file was not uploaded. Please try later';
}
else
{
if
(file_exists($file_path)) {
$resultClass=
'alert-danger alert';
$result=
'File exist alredy. File uploading ignored.';
}
else
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$file_path)) {
$resultClass=
'alert-success alert';
$result=
'File uploaded successfully';
}
else
{
$resultClass=
'alert-danger alert';
$result=
'Sorry, your file was not uploaded. Please try later';
}
}
0 comments