Does my code meet my assignment requirements?


#1

Hi,

I have been given a task to write a script that uploads a file from the client to the web server. As a result, I created this. The files which the user uploads is stored locally in a folder. However, the assignment task mentions “to the web server” therefore, I think I haven’t fully completed the task. If so, how can I use PHP to upload the file extensions supported in my code to upload to a web server?

Thanks for any help in advance it’s very appreciated!

<?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";
        }
    }
}

?>

`


#2

As in a school assignment? I always base my file uploads off of the W3 Schools example…


#3

Yes correct, I’ll look into that now.

Thank you for the response.