You are on page 1of 2

INTECH 2201: Web Application Development Lab

2nd Semester 2019-2020


PHP File Upload

When working with file upload, we need to add the enctype attribute to the form tag. The
enctype specifies how the form data should be encoded when submitting it to the server.

The default value is application/x-www-form-urlencoded. For us to enable file upload, we need


to use multipart/form-data. Also, use the file as type of our input tag.

<form action="" method="post" enctype="multipart/form-data">


<input type="file" name="myfile">
<input type="submit" name="submit">
</form>

The $_SERVER holds the server and execution environment information. With the user of the
$_SERVER array, we can check the request method of the application. You can use the request
method to know if the form method is GET or POST.

$_SERVER['REQUEST_METHOD'] == 'POST'

When working with files, we are going to use the $_FILES variable, not the $_POST or $_GET.
It holds the name, type, temporary name (tmp_name), error, and size of the file.

The tmp_name holds the temporary directory of the file to be uploaded. Example directory is
C:\xampp\tmp\php8D25.tmp. The name holds the filename with the file extension, the type holds
the file time like image/png, and the size holds the size of the file in bytes. We use the temporary
directory and move the file to the new directory. Create an upload folder that will serve as the
storage of uploaded files.

In file upload, there is a tendency that files may have the same file name. To avoid this,
append timestamp before the file name. With this, use date_create() to return the current date and
time, and format it using date_format(). The parameter for the date format is YmdHis.

For example, the user will upload profile.png on February 16, 2020, at 10:15:20.

Year: Y (2020), y (20) Month: M (Feb), m (02) Day: d (16)


Hour: H (10) Minute: i (15) Second: s (20)

$date = date_create();
$stamp = date_format($date,'YmdHis');

After creating the time stamp, get the temporary directory of the file and create the new file
directory.

$temp = $_FILES['myfile']['tmp_name'];
$directory = "upload/" . $stamp . $_FILES['myfile']['name'];

The move_uploaded_file() function moves the file from temporary to the new directory.
Whereas the new directory is upload/20200216101520profile.png given that profile.png is the
filename and uploaded on February 17, 2020, 10:15:20. It will return true if the file is moved to the
directory or file if not.

move_uploaded_file($temp,$directory)
Complete Code

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {

$date = date_create();
$stamp = date_format($date,'YmdHis');
$temp = $_FILES['myfile']['tmp_name'];
$directory = "upload/" . $stamp . $_FILES['myfile']['name'];

if(move_uploaded_file($temp,$directory)) {
echo "uploaded";
} else {
echo "error";
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=s, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile">
<input type="submit" name="submit">
</form>
</body>
</html>

Prepared by JDRivera

You might also like