Create A File Add System On Your Web site with PHP

By | May 6, 2020

With PHP, it’s simple to add information to the server.

Nevertheless, with ease comes hazard, so at all times watch out when permitting file uploads!


Configure The “php.ini” File

First, be sure that PHP is configured to permit file uploads.

In your “php.ini” file, seek for the file_uploads directive, and set it to On:


Create The HTML Type

Subsequent, create an HTML type that enable customers to decide on the picture file they wish to add:

<!DOCTYPE html>
<html>
<physique>
<type motion=”add.php” methodology=”submit” enctype=”multipart/form-data”>
Choose picture to add:
<enter kind=”file” title=”fileToUpload” id=”fileToUpload”>
<enter kind=”submit” worth=”Add Picture” title=”submit”>
</type>
</physique>
</html>

Some guidelines to observe for the HTML type above:

  • Make it possible for the shape makes use of methodology=”submit”
  • The shape additionally wants the next attribute: enctype=”multipart/form-data”. It specifies which content-type to make use of when submitting the shape

With out the necessities above, the file add won’t work.

Different issues to note:

  • The sort=”file” attribute of the <enter> tag exhibits the enter area as a file-select management, with a “Browse” button subsequent to the enter management

The shape above sends information to a file referred to as “add.php”, which we’ll create subsequent.

Create The Add File PHP Script

The “add.php” file incorporates the code for importing a file:

<?php
$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Test if picture file is a precise picture or pretend picture
if(isset($_POST[“submit”])) {
$examine = getimagesize($_FILES[“fileToUpload”][“tmp_name”]);
if($examine !== false) {
echo “File is a picture – ” . $examine[“mime”] . “.”;
$uploadOk = 1;
} else {
echo “File just isn’t a picture.”;
$uploadOk = 0;
}
}
?>

PHP script defined:

  • $target_dir = “uploads/” – specifies the listing the place the file goes to be positioned
  • $target_file specifies the trail of the file to be uploaded
  • $uploadOk=1 just isn’t used but (can be used later)
  • $imageFileType holds the file extension of the file (in decrease case)
  • Subsequent, examine if the picture file is an precise picture or a pretend picture

Observe: You will want to create a brand new listing referred to as “uploads” within the listing the place “add.php” file resides. The uploaded information can be saved there.


Test if File Already Exists

Now we will add some restrictions.

First, we’ll examine if the file already exists within the “uploads” folder. If it does, an error message is displayed, and $uploadOk is ready to 0:

// Test if file already exists
if (file_exists($target_file)) {
echo “Sorry, file already exists.”;
$uploadOk = 0;
}

Restrict File Measurement

The file enter area in our HTML type above is known as “fileToUpload”.

Now, we wish to examine the scale of the file. If the file is bigger than 500KB, an error message is displayed, and $uploadOk is ready to 0:

// Test file measurement
if ($_FILES[“fileToUpload”][“size”] > 500000) {
echo “Sorry, your file is just too massive.”;
$uploadOk = 0;
}

Restrict File Kind

The code beneath solely permits customers to add JPG, JPEG, PNG, and GIF information. All different file varieties provides an error message earlier than setting $uploadOk to 0:

// Permit sure file codecs
if($imageFileType != “jpg” && $imageFileType != “png” && $imageFileType != “jpeg”
&& $imageFileType != “gif” ) {
echo “Sorry, solely JPG, JPEG, PNG & GIF information are allowed.”;
$uploadOk = 0;
}

Full Add File PHP Script

The whole “add.php” file now appears to be like like this:

<?php
$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Test if picture file is a precise picture or pretend picture
if(isset($_POST[“submit”])) {
$examine = getimagesize($_FILES[“fileToUpload”][“tmp_name”]);
if($examine !== false) {
echo “File is a picture – ” . $examine[“mime”] . “.”;
$uploadOk = 1;
} else {
echo “File just isn’t a picture.”;
$uploadOk = 0;
}
}
// Test if file already exists
if (file_exists($target_file)) {
echo “Sorry, file already exists.”;
$uploadOk = 0;
}
// Test file measurement
if ($_FILES[“fileToUpload”][“size”] > 500000) {
echo “Sorry, your file is just too massive.”;
$uploadOk = 0;
}
// Permit sure file codecs
if($imageFileType != “jpg” && $imageFileType != “png” && $imageFileType != “jpeg”
&& $imageFileType != “gif” ) {
echo “Sorry, solely JPG, JPEG, PNG & GIF information are allowed.”;
$uploadOk = 0;
}
// Test if $uploadOk is ready to Zero by an error
if ($uploadOk == 0) {
echo “Sorry, your file was not uploaded.”;
// if all the pieces is okay, attempt to add file
} else {
if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {
echo “The file “. basename( $_FILES[“fileToUpload”][“name”]). ” has been uploaded.”;
} else {
echo “Sorry, there was an error importing your file.”;
}
}
?>



Leave a Reply

Your email address will not be published. Required fields are marked *