The University of Dodoma: Internet Programming and Applications II
The University of Dodoma: Internet Programming and Applications II
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$target_dir = "assignment1/";
$uploadOk = 1;
$filetype = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$uploadOk = 0;
if(file_exists($target_file)){
$uploadOk = 0;
if($filetype != "jpg" && $filetype !== "png" && $filetype != "jpeg" && $filetype != “gif”){
$ftErr = "Only jpg, png, jpeg and gif file formats are allowed.";
$uploadOk = 0;
}
if($uploadOk == 0){
}else{
if(move_uploaded_file($_FILES["file1"]["tmp_name"],$target_file)){
}else{
?>
</form>
<?php
?>
</body>
</html>
Output:
Explanations:
Step 1. We create several variables for storing our error or success messages to be displayed at the top
or bottom, though this is not much necessary because messages can be displayed anywhere within the
page.
Step 2. Specify the directory where your files will be placed after being uploaded from the page. For my
case, the directory or folder is named as “assignment_1”.
Step 3. Specify the file name to be used from input in the form. Name is an attribute of input in the
form, it the one used with any method either POST or GET for the input to be recognized. For my case
file name is “file1”.
Step 4. Set $uploadOk =1. This will be used to validate if there is no any error with our input file hence
the file will be moved to our destination folder or directory.
Step 5. Create a variable which will be used to store information about file extension. Pathinfo() function
is used to obtain file extension. Srttolower() function is used to return a string in small letters. Here It
returns a file extension in small letters. Forexample, jpg, png, jpeg.
Step 6. Now we need to validate file size, file type, if file already exists. If everything is okey then file will
be moved to destination folder hence successful message will be displayed according to script.
Otherwise, the file will not be uploaded and error message will be displayed according to script.