PHP 5 Forms – Required Fields

Toggle navigation
TUTORIAL HOME
PHP 5 Forms – Required Fields
❮ Previous Next ❯
This chapter shows how to make input fields required and create error messages if needed.

PHP – Required Fields
From the validation rules table on the previous page, we see that the “Name”, “E-mail”, and “Gender” fields are required. These fields cannot be empty and must be filled out in the HTML form.

Field Validation Rules
Name Required. + Must only contain letters and whitespace
E-mail Required. + Must contain a valid email address (with @ and .)
Website Optional. If present, it must contain a valid URL
Comment Optional. Multi-line input field (textarea)
Gender Required. Must select one
In the previous chapter, all input fields were optional.

In the following code we have added some new variables: $nameErr, $emailErr, $genderErr, and $websiteErr. These error variables will hold error messages for the required fields. We have also added an if else statement for each $_POST variable. This checks if the $_POST variable is empty (with the PHP empty() function). If it is empty, an error message is stored in the different error variables, and if it is not empty, it sends the user input data through the test_input() function:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = “”;
$name = $email = $gender = $comment = $website = “”;

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
  if (empty($_POST[“name”])) {
    $nameErr = “Name is required”;
  } else {
    $name = test_input($_POST[“name”]);
  }

  if (empty($_POST[“email”])) {
    $emailErr = “Email is required”;
  } else {
    $email = test_input($_POST[“email”]);
  }

  if (empty($_POST[“website”])) {
    $website = “”;
  } else {
    $website = test_input($_POST[“website”]);
  }

  if (empty($_POST[“comment”])) {
    $comment = “”;
  } else {
    $comment = test_input($_POST[“comment”]);
  }

  if (empty($_POST[“gender”])) {
    $genderErr = “Gender is required”;
  } else {
    $gender = test_input($_POST[“gender”]);
  }
}
?>
PHP – Display The Error Messages
Then in the HTML form, we add a little script after each required field, which generates the correct error message if needed (that is if the user tries to submit the form without filling out the required fields):

Example
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF”]);?>”>

Name:
*

E-mail:

*

Website:

Comment:

Gender:
Female
Male
*

The next step is to validate the input data, that is “Does the Name field contain only letters and whitespace?”, and “Does the E-mail field contain a valid e-mail address syntax?”, and if filled out, “Does the Website field contain a valid URL?”.

❮ Previous Next ❯

PHP 5 Forms – Validate E-mail and URL

Toggle navigation
TUTORIAL HOME
PHP 5 Forms – Validate E-mail and URL
❮ Previous Next ❯
This chapter shows how to validate names, e-mails, and URLs.

PHP – Validate Name
The code below shows a simple way to check if the name field only contains letters and whitespace. If the value of the name field is not valid, then store an error message:

$name = test_input($_POST[“name”]);
if (!preg_match(“/^[a-zA-Z ]*$/”,$name)) {
  $nameErr = “Only letters and white space allowed”;
}
The preg_match() function searches a string for pattern, returning true if the pattern exists, and false otherwise.

PHP – Validate E-mail
The easiest and safest way to check whether an email address is well-formed is to use PHP’s filter_var() function.

In the code below, if the e-mail address is not well-formed, then store an error message:

$email = test_input($_POST[“email”]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = “Invalid email format”;
}
PHP – Validate URL
The code below shows a way to check if a URL address syntax is valid (this regular expression also allows dashes in the URL). If the URL address syntax is not valid, then store an error message:

$website = test_input($_POST[“website”]);
if (!preg_match(“/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i”,$website)) {
  $websiteErr = “Invalid URL”;
}
PHP – Validate Name, E-mail, and URL
Now, the script looks like this:

Example
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = “”;
$name = $email = $gender = $comment = $website = “”;

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
  if (empty($_POST[“name”])) {
    $nameErr = “Name is required”;
  } else {
    $name = test_input($_POST[“name”]);
    // check if name only contains letters and whitespace
    if (!preg_match(“/^[a-zA-Z ]*$/”,$name)) {
      $nameErr = “Only letters and white space allowed”;
    }
  }

  if (empty($_POST[“email”])) {
    $emailErr = “Email is required”;
  } else {
    $email = test_input($_POST[“email”]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = “Invalid email format”;
    }
  }

  if (empty($_POST[“website”])) {
    $website = “”;
  } else {
    $website = test_input($_POST[“website”]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    if (!preg_match(“/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i”,$website)) {
      $websiteErr = “Invalid URL”;
    }
  }

  if (empty($_POST[“comment”])) {
    $comment = “”;
  } else {
    $comment = test_input($_POST[“comment”]);
  }

  if (empty($_POST[“gender”])) {
    $genderErr = “Gender is required”;
  } else {
    $gender = test_input($_POST[“gender”]);
  }
}
?>
The next step is to show how to prevent the form from emptying all the input fields when the user submits the form.

❮ Previous Next ❯

PHP 5 Complete Form Example

PHP 5 Complete Form Example
❮ Previous Next ❯
This chapter shows how to keep the values in the input fields when the user hits the submit button.

PHP – Keep The Values in The Form
To show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the and tags. The little script outputs the value of the $name, $email, $website, and $comment variables.

Then, we also need to show which radio button that was checked. For this, we must manipulate the checked attribute (not the value attribute for radio buttons):

Name: <input type="text" name="name" value="”>

E-mail: <input type="text" name="email" value="”>

Website: <input type="text" name="website" value="”>

Comment:

Gender:
<input type="radio" name="gender"

value=”female”>Female
<input type="radio" name="gender"

value=”male”>Male
PHP – Complete Form Example
Here is the complete code for the PHP Form Validation Example:

Example

❮ Previous Next ❯

PHP 5 Multidimensional Arrays

PHP 5 Multidimensional Arrays
❮ Previous Next ❯
Earlier in this tutorial, we have described arrays that are a single list of key/value pairs.

However, sometimes you want to store values with more than one key.

This can be stored in multidimensional arrays.

PHP – Multidimensional Arrays
A multidimensional array is an array containing one or more arrays.

PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

The dimension of an array indicates the number of indices you need to select an element.

For a two-dimensional array you need two indices to select an element
For a three-dimensional array you need three indices to select an element
PHP – Two-dimensional Arrays
A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays).

First, take a look at the following table:

Name Stock Sold
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15
We can store the data from the table above in a two-dimensional array, like this:

$cars = array
  (
  array(“Volvo”,22,18),
  array(“BMW”,15,13),
  array(“Saab”,5,2),
  array(“Land Rover”,17,15)
  );
Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column.

To get access to the elements of the $cars array we must point to the two indices (row and column):

Example
<?php
echo $cars[0][0].”: In stock: “.$cars[0][1].”, sold: “.$cars[0][2].”.
“;
echo $cars[1][0].”: In stock: “.$cars[1][1].”, sold: “.$cars[1][2].”.
“;
echo $cars[2][0].”: In stock: “.$cars[2][1].”, sold: “.$cars[2][2].”.
“;
echo $cars[3][0].”: In stock: “.$cars[3][1].”, sold: “.$cars[3][2].”.
“;
?>
We can also put a For loop inside another For loop to get the elements of the $cars array (we still have to point to the two indices):

Example
<?php
for ($row = 0; $row < 4; $row++) {
  echo “

Row number $row

“;
  echo “

    “;
      for ($col = 0; $col < 3; $col++) {
        echo “

  • “.$cars[$row][$col].”</li>”;
      }
      echo “

“;
}
?>

❮ Previous Next ❯

PHP 5 Date and Time

PHP 5 Date and Time
❮ Previous Next ❯
The PHP date() function is used to format a date and/or a time.

The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.

Syntax
date(format,timestamp)
Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Get a Simple Date
The required format parameter of the date() function specifies how to format the date (or time).

Here are some characters that are commonly used for dates:

d – Represents the day of the month (01 to 31)
m – Represents a month (01 to 12)
Y – Represents a year (in four digits)
l (lowercase ‘L’) – Represents the day of the week
Other characters, like”/”, “.”, or “-” can also be inserted between the characters to add additional formatting.

The example below formats today’s date in three different ways:

Example
<?php
echo “Today is ” . date(“Y/m/d”) . “
“;
echo “Today is ” . date(“Y.m.d”) . “
“;
echo “Today is ” . date(“Y-m-d”) . “
“;
echo “Today is ” . date(“l”);
?>
PHP Tip – Automatic Copyright Year
Use the date() function to automatically update the copyright year on your website:

Example
© 2010-
Get a Simple Time
Here are some characters that are commonly used for times:

h – 12-hour format of an hour with leading zeros (01 to 12)
i – Minutes with leading zeros (00 to 59)
s – Seconds with leading zeros (00 to 59)
a – Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:

Example
<?php
echo “The time is ” . date(“h:i:sa”);
?>
Note that the PHP date() function will return the current date/time of the server!

Get Your Time Zone
If the time you got back from the code is not the right time, it’s probably because your server is in another country or set up for a different timezone.

So, if you need the time to be correct according to a specific location, you can set a timezone to use.

The example below sets the timezone to “America/New_York”, then outputs the current time in the specified format:

Example
<?php
date_default_timezone_set(“America/New_York”);
echo “The time is ” . date(“h:i:sa”);
?>
Create a Date With PHP mktime()
The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used (as shown in the examples above).

The mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax
mktime(hour,minute,second,month,day,year)
The example below creates a date and time from a number of parameters in the mktime() function:

Example
<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo “Created date is ” . date(“Y-m-d h:i:sa”, $d);
?>
Create a Date From a String With PHP strtotime()
The PHP strtotime() function is used to convert a human readable string to a Unix time.

Syntax
strtotime(time,now)
The example below creates a date and time from the strtotime() function:

Example
<?php
$d=strtotime(“10:30pm April 15 2014”);
echo “Created date is ” . date(“Y-m-d h:i:sa”, $d);
?>
PHP is quite clever about converting a string to a date, so you can put in various values:

Example
<?php
$d=strtotime(“tomorrow”);
echo date(“Y-m-d h:i:sa”, $d) . “
“;

$d=strtotime(“next Saturday”);
echo date(“Y-m-d h:i:sa”, $d) . “
“;

$d=strtotime(“+3 Months”);
echo date(“Y-m-d h:i:sa”, $d) . “
“;
?>
However, strtotime() is not perfect, so remember to check the strings you put in there.

More Date Examples
The example below outputs the dates for the next six Saturdays:

Example
<?php
$startdate = strtotime(“Saturday”);
$enddate = strtotime(“+6 weeks”, $startdate);

while ($startdate < $enddate) {
  echo date(“M d”, $startdate) . “
“;
  $startdate = strtotime(“+1 week”, $startdate);
}
?>
The example below outputs the number of days until 4th of July:

Example
<?php
$d1=strtotime(“July 04”);
$d2=ceil(($d1-time())/60/60/24);
echo “There are ” . $d2 .” days until 4th of July.”;
?>
Complete PHP Date Reference
For a complete reference of all date functions, go to our complete PHP Date Reference.

The reference contains a brief description, and examples of use, for each function!

❮ Previous Next ❯

PHP 5 Include Files

Toggle navigation
TUTORIAL HOME
PHP 5 Include Files
❮ Previous Next ❯
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

PHP include and require Statements
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

require will produce a fatal error (E_COMPILE_ERROR) and stop the script
include will only produce a warning (E_WARNING) and the script will continue
So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application’s security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Syntax
include ‘filename’;

or

require ‘filename’;
PHP include Examples
Example 1
Assume we have a standard footer file called “footer.php”, that looks like this:

<?php
echo “

Copyright © 1999-” . date(“Y”) . ” Omega.com

“;
?>
To include the footer file in a page, use the include statement:

Example

Welcome to my home page!

Some text.

Some more text.

Example 2
Assume we have a standard menu file called “menu.php”:

<?php
echo ‘Home
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
PHP Tutorial‘;
?>
All pages in the Web site should use this menu file. Here is how it can be done (we are using a

element so that the menu easily can be styled with CSS later):

Example

Welcome to my home page!

Some text.

Some more text.

Example 3
Assume we have a file called “vars.php”, with some variables defined:

<?php
$color=’red’;
$car=’BMW’;
?>
Then, if we include the “vars.php” file, the variables can be used in the calling file:

Example

Welcome to my home page!

<?php include 'vars.php';
echo “I have a $color $car.”;
?>

PHP include vs. require
The require statement is also used to include a file into the PHP code.

However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:

Example

Welcome to my home page!

<?php include 'noFileExists.php';
echo “I have a $color $car.”;
?>

If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:

Example

Welcome to my home page!

<?php require 'noFileExists.php';
echo “I have a $color $car.”;
?>

Use require when the file is required by the application.

Use include when the file is not required and application should continue when file is not found.

❮ Previous Next ❯

PHP 5 File Handling

Toggle navigation
TUTORIAL HOME
PHP 5 File Handling
❮ Previous Next ❯
File handling is an important part of any web application. You often need to open and process a file for different tasks.

PHP Manipulating Files
PHP has several functions for creating, reading, uploading, and editing files.

Be careful when manipulating files!

When you are manipulating files you must be very careful.
You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.

PHP readfile() Function
The readfile() function reads a file and writes it to the output buffer.

Assume we have a text file called “webdictionary.txt”, stored on the server, that looks like this:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
The PHP code to read the file and write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success):

Example
<?php
echo readfile(“webdictionary.txt”);
?>
The readfile() function is useful if all you want to do is open up a file and read its contents.

The next chapters will teach you more about file handling.

❮ Previous Next ❯

PHP 5 File Open/Read/Close

PHP 5 File Open/Read/Close
❮ Previous Next ❯
In this chapter we will teach you how to open, read, and close a file on the server.

PHP Open File – fopen()
A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

We will use the text file, “webdictionary.txt”, during the lessons:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:

Example
<?php
$myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”);
echo fread($myfile,filesize(“webdictionary.txt”));
fclose($myfile);
?>
Tip: The fread() and the fclose() functions will be explained below.

The file may be opened in one of the following modes:

Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
PHP Read File – fread()
The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The following PHP code reads the “webdictionary.txt” file to the end:

fread($myfile,filesize(“webdictionary.txt”));
PHP Close File – fclose()
The fclose() function is used to close an open file.

It’s a good programming practice to close all files after you have finished with them. You don’t want an open file running around on your server taking up resources!

The fclose() requires the name of the file (or a variable that holds the filename) we want to close:

<?php
$myfile = fopen(“webdictionary.txt”, “r”);
// some code to be executed….
fclose($myfile);
?>
PHP Read Single Line – fgets()
The fgets() function is used to read a single line from a file.

The example below outputs the first line of the “webdictionary.txt” file:

Example
<?php
$myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”);
echo fgets($myfile);
fclose($myfile);
?>
Note: After a call to the fgets() function, the file pointer has moved to the next line.

PHP Check End-Of-File – feof()
The feof() function checks if the “end-of-file” (EOF) has been reached.

The feof() function is useful for looping through data of unknown length.

The example below reads the “webdictionary.txt” file line by line, until end-of-file is reached:

Example
<?php
$myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”);
// Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . “
“;
}
fclose($myfile);
?>
PHP Read Single Character – fgetc()
The fgetc() function is used to read a single character from a file.

The example below reads the “webdictionary.txt” file character by character, until end-of-file is reached:

Example
<?php
$myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”);
// Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>
Note: After a call to the fgetc() function, the file pointer moves to the next character.

Complete PHP Filesystem Reference
For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.

❮ Previous Next ❯

PHP 5 File Create/Write

PHP 5 File Create/Write
❮ Previous Next ❯
In this chapter we will teach you how to create and write to a file on the server.

PHP Create File – fopen()
The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files.

If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

The example below creates a new file called “testfile.txt”. The file will be created in the same directory where the PHP code resides:

Example
$myfile = fopen(“testfile.txt”, “w”)

PHP File Permissions
If you are having errors when trying to get this code to run, check that you have granted your PHP file access to write information to the hard drive.

PHP Write to File – fwrite()
The fwrite() function is used to write to a file.

The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

The example below writes a couple of names into a new file called “newfile.txt”:

Example
<?php
$myfile = fopen(“newfile.txt”, “w”) or die(“Unable to open file!”);
$txt = “John Doe\n”;
fwrite($myfile, $txt);
$txt = “Jane Doe\n”;
fwrite($myfile, $txt);
fclose($myfile);
?>

Notice that we wrote to the file “newfile.txt” twice. Each time we wrote to the file we sent the string $txt that first contained “John Doe” and second contained “Jane Doe”. After we finished writing, we closed the file using the fclose() function.

If we open the “newfile.txt” file it would look like this:

John Doe
Jane Doe
PHP Overwriting
Now that “newfile.txt” contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file.

In the example below we open our existing file “newfile.txt”, and write some new data into it:

Example
<?php
$myfile = fopen(“newfile.txt”, “w”) or die(“Unable to open file!”);
$txt = “Mickey Mouse\n”;
fwrite($myfile, $txt);
$txt = “Minnie Mouse\n”;
fwrite($myfile, $txt);
fclose($myfile);
?>

If we now open the “newfile.txt” file, both John and Jane have vanished, and only the data we just wrote is present:

Mickey Mouse
Minnie Mouse
Complete PHP Filesystem Reference
For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.

❮ Previous Next ❯

PHP 5 File Upload

Toggle navigation
TUTORIAL HOME
PHP 5 File Upload
❮ Previous Next ❯
With PHP, it is easy to upload files to the server.

However, with ease comes danger, so always be careful when allowing file uploads!

Configure The “php.ini” File
First, ensure that PHP is configured to allow file uploads.

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

file_uploads = On
Create The HTML Form
Next, create an HTML form that allow users to choose the image file they want to upload:

    Select image to upload:
   
   

Some rules to follow for the HTML form above:

Make sure that the form uses method=”post”
The form also needs the following attribute: enctype=”multipart/form-data”. It specifies which content-type to use when submitting the form
Without the requirements above, the file upload will not work.

Other things to notice:

The type=”file” attribute of the tag shows the input field as a file-select control, with a “Browse” button next to the input control
The form above sends data to a file called “upload.php”, which we will create next.

Create The Upload File PHP Script
The “upload.php” file contains the code for uploading a file:

<?php
$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST[“submit”])) {
    $check = getimagesize($_FILES[“fileToUpload”][“tmp_name”]);
    if($check !== false) {
        echo “File is an image – ” . $check[“mime”] . “.”;
        $uploadOk = 1;
    } else {
        echo “File is not an image.”;
        $uploadOk = 0;
    }
}
?>
PHP script explained:

$target_dir = “uploads/” – specifies the directory where the file is going to be placed
$target_file specifies the path of the file to be uploaded
$uploadOk=1 is not used yet (will be used later)
$imageFileType holds the file extension of the file
Next, check if the image file is an actual image or a fake image
Note: You will need to create a new directory called “uploads” in the directory where “upload.php” file resides. The uploaded files will be saved there.

Check if File Already Exists
Now we can add some restrictions.

First, we will check if the file already exists in the “uploads” folder. If it does, an error message is displayed, and $uploadOk is set to 0:

// Check if file already exists
if (file_exists($target_file)) {
    echo “Sorry, file already exists.”;
    $uploadOk = 0;
}
Limit File Size
The file input field in our HTML form above is named “fileToUpload”.

Now, we want to check the size of the file. If the file is larger than 500KB, an error message is displayed, and $uploadOk is set to 0:

// Check file size
if ($_FILES[“fileToUpload”][“size”] > 500000) {
    echo “Sorry, your file is too large.”;
    $uploadOk = 0;
}
Limit File Type
The code below only allows users to upload JPG, JPEG, PNG, and GIF files. All other file types gives an error message before setting $uploadOk to 0:

// Allow certain file formats
if($imageFileType != “jpg” && $imageFileType != “png” && $imageFileType != “jpeg”
&& $imageFileType != “gif” ) {
    echo “Sorry, only JPG, JPEG, PNG & GIF files are allowed.”;
    $uploadOk = 0;
}
Complete Upload File PHP Script
The complete “upload.php” file now looks like this:

<?php
$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST[“submit”])) {
    $check = getimagesize($_FILES[“fileToUpload”][“tmp_name”]);
    if($check !== false) {
        echo “File is an image – ” . $check[“mime”] . “.”;
        $uploadOk = 1;
    } else {
        echo “File is not an image.”;
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo “Sorry, file already exists.”;
    $uploadOk = 0;
}
// Check file size
if ($_FILES[“fileToUpload”][“size”] > 500000) {
    echo “Sorry, your file is too large.”;
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != “jpg” && $imageFileType != “png” && $imageFileType != “jpeg”
&& $imageFileType != “gif” ) {
    echo “Sorry, only JPG, JPEG, PNG & GIF files are allowed.”;
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo “Sorry, your file was not uploaded.”;
// if everything is ok, try to upload 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 uploading your file.”;
    }
}
?>
Complete PHP Filesystem Reference
For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.

❮ Previous Next ❯