Hi, I have a task to complete which is “Write a script that upload a file from the client to the web server.” does the code below fully meet this requirement? The files are being stored locally and this is the part I’m not sure about, is that considered ok in relation to the web server part?
<?php
$currentDir = getcwd(); //Gets the current working directory
$uploadDirectory = "/Uploads - Liam Docherty Program/"; //Defining name of folder created which stores uploaded files.
$errors = []; // Store errors here
$fileExtensions = ['jpeg','jpg','png','pdf','txt']; // // file extensions my program supports
$fileName = $_FILES['myfile']['name'];
$fileSize = $_FILES['myfile']['size'];
$fileTmpName = $_FILES['myfile']['tmp_name'];
$fileType = $_FILES['myfile']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));
$uploadPath = $currentDir . $uploadDirectory . basename($fileName);
echo $uploadPath; //Process involved in outputting text
//Below represents the selection statement in relation to the chosen file by the user
if (isset($_POST['submit'])) {
if (! in_array($fileExtension,$fileExtensions)) {
$errors[] = "This file extension is not allowed. Please upload a JPEG, PNG, PDF or a TXT file.";
}
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
echo "The file " . basename($fileName) . " has been uploaded. Thanks for using Liam Docherty's program!";
} else {
echo "An error occurred somewhere. Try again or contact Liam Docherty.";
}
} else {
foreach ($errors as $error) {
echo $error . "These are the errors" . "\n";
}
}
}
?>
<!-- LIAM DOCHERTY'S PROGRAM -->