html – Uploading multiple files with PHP & storing form in CSS-ThrowExceptions

Exception or error:

I have a form that collects student information, the simple version of which is here:

<form method="post" name="submit" action="submit" autocomplete="off"> <br>
<input type="name" id="forename" name="forename" placeholder="Forename" required> <br>
<input type="name" id="middlename" name="middlename" placeholder="Middle Name/s" required> <br>
<input type="name" id="surname" name="surname" placeholder="Surname" required> <br>
<input type="tel" id="mobile" name="mobile" placeholder="Mobile Number (+44)" required> <br>
<input type="email" id="email" name="email" placeholder="Email" required> <br>
<input type="message" id="course" name="course" placeholder="Course You're Studying" required> <br>
<input type="message" id="code" name="code" placeholder="Course Code" required> <br>
<input type="message" id="campus" name="campus" placeholder="Campus" required> <br>
<input type="message" id="message" name="message" placeholder="Comments"> <br>
<br>
<input type="file" title="Attach your ID" id="studentidentification" name="studentidentification" required> <br>
<input type="file" title="Attach personal ID" id="personalidentification" name="personaldentification" required> <br>
<br>
<input type="submit" name="submit" id="submit" value="Submit" required="">

This form is then sent to submit.php for example. (the page has a random URL and then sends the user to a new page)

The PHP to follow takes all of the user’s inputs and writes them to a CSV that includes the file names of the student and personal identifications for cross referencing. The PHP should also upload each file in the folders within the responses folder. So csv, student and personal.

<?php
// DECLARE
    $forename = $_POST['forename'];
        $middlename = $_POST['middlename'];
        $surname = $_POST['surname'];
        $mobile = $_POST['mobile'];
        $email = $_POST['email'];
        
        $course = $_POST['course'];
        $code = $_POST['code'];
        $campus = $_POST['campus'];
        $comments = $_POST['comments'];
        
        $studentidentification = basename($_FILES["studentidentification"]["name"]);
        $personalidentification = basename($_FILES["personalidentification"]["name"]);
        
        $timestamp =date('l jS \of F Y h:i:s A');
//WRITE
     $text = "".$forename.",".$middlename.",".$surname.",".$mobile.",".$email.",".$course.",".$ucas.",".$campus.",".$conditions.",".$studentidentification.",".$personalidentification.",".$timestamp." \n";
        $file = fopen("/responses/csv/results.csv","a+ \n");
        fwrite($file, $text);
        fclose($file);

// STUDENT'S ID
$target_dir = "/responses/student/";
$target_file = $target_dir . basename($_FILES["studentidentification"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if (file_exists($target_file)) {
    echo "Sorry $forename, there is already a file with this name on the server. Please try renaming it or adding a version number then try again.";
    $uploadOk = 0;
}

if ($uploadOk == 0) {
    echo " Your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["studentidentification"]["tmp_name"], $target_file)) {
        echo "Thank you, $forename. Your file ". basename( $_FILES["studentidentification"]["name"]). " has been uploaded.";
    } else {
        echo " Sorry $forename, there was an unknown error encountered while uploading your file. Please try again.";
    }
}

// PERSONAL ID
$target_dir1 = "/responses/personal/";
$target_file1 = $target_dir1 . basename($_FILES["personalidentification"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if file already exists
if (file_exists($target_file1)) {
    echo "Sorry $forename, there is already a file with this name on the server. Please try renaming it or adding a version number then try again.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo " Your file was not uploaded."; // Sorry $forename, your file was not uploaded, please try again
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["personalidentification"]["tmp_name"], $target_file1)) {
        echo "Thank you, $forename. Your file ". basename( $_FILES["personalidentification"]["name"]). " has been uploaded.";
    } else {
        echo " Sorry $forename, there was an unknown error encountered while uploading your file. Please try again.";
    }
}

 header("Location: /newstudents/welcome");

?>

For some reason the unknown error is output and I feel like it is to do with the fact that two files are being uploaded in the same file. The two IDs must be done separately and within the same form. I wish I was getting paid to figure this out but I don’t know where to go from here because even the CSV doesn’t write and this has been based off code that does work.

The Apache error_log that has been generated throws Undefined index for most of them failed to open stream: No such file or directory when the directories are actually there. Also are fwrite() expects parameter 1 to be resource, boolean given and fclose() expects parameter 1 to be resource, boolean. Asking is a last resort because in theory this should work so sorry if for some reason it’s a glaringly obvious error.

Any help whatsoever would be appreciated. Thank you 🙂

How to solve:

There are 2 issues:

  1. You need to use ./ instead of / at the start of your URLs, so that your URLs are relative and do not refer to the base of your website.
    This will fix the failed to open stream: No such file or directory error.

  2. You need to add enctype="multipart/form-data" onto your form if you want to send files:

    <form method="post" name="submit" action="submit" autocomplete="off" enctype="multipart/form-data">
    ...
    </form>
    

    This will fix the Undefined index: studentidentification in /home/hosting/domains/example.com/public_html/onboarding/forms/collectid/submit.php error.

Also, ensure that you have file_uploads set to on in your php.ini (PHP configuration) file.

Leave a Reply

Your email address will not be published. Required fields are marked *