0% found this document useful (0 votes)
50 views

The University of Dodoma: Internet Programming and Applications II

The document describes a PHP script to upload files. It defines variables for error messages, specifies a target upload directory, checks for file size, type and duplicate names, and moves valid files or outputs error messages. Key steps are validating the upload, moving successful files, and displaying results.

Uploaded by

Arique
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

The University of Dodoma: Internet Programming and Applications II

The document describes a PHP script to upload files. It defines variables for error messages, specifies a target upload directory, checks for file size, type and duplicate names, and moves valid files or outputs error messages. Key steps are validating the upload, moving successful files, and displaying results.

Uploaded by

Arique
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

THE UNIVERSITY OF DODOMA

COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION


INDIVIDUAL ASSIGNMENT 1.

DEGREE PROGRAM: BSC-CS


COURSE NAME: Internet Programming and Applications II
COURSE CODE: CP 311
COURSE INSTRUCTOR: MR. SIPHAEL BETUEL.
STUDENT NAME: ERICK K KALIUPE.
REGISTRATION NUMBER: T/UDOM/2019/00425
Below is the php script to upload a file.

<!DOCTYPE html>

<html>

<head>

<title>IP Individual assignment_1</title>

</head>

<body>

<?php

$fxErr = $fsizeErr = $ftErr = $uploadOkErr = $success = $successErr = "";

$target_dir = "assignment1/";

$target_file = $target_dir . basename($_FILES["file1"]["name"]);

$uploadOk = 1;

$filetype = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

//checking file size

if($_FILES["file1"]["size"] > 500000){

$fsizeErr = "File is too large.";

$uploadOk = 0;

//check if file already exists

if(file_exists($target_file)){

$fxErr = "Sorry file name already exists.";

$uploadOk = 0;

//limit file type

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){

$uploadOkErr = "Sorry your file was not uploaded.";

}else{

if(move_uploaded_file($_FILES["file1"]["tmp_name"],$target_file)){

$success = "Your file was uploaded successfully.";

}else{

$successErr = "Sorry there was an error uploading your file.";

?>

<form action="aggie.php" method="POST" enctype="multipart/form-data">

<input type="file" name="file1"><br><br>

<input type="submit" name="submit" value="Click Here To Upload" id="erick">

</form>

<?php

echo $fxErr . "<br>";

echo $fsizeErr . "<br>";

echo $ftErr . "<br>";

echo $uploadOkErr . "<br>";

echo $success . "<br>";

echo $successErr . "<br>";

?>

</body>

</html>
Output:

Explanations:

We create a file upload script through the following steps.

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.

You might also like