使用 PHP 上传多个文件并在 CSS 中存储表格

Uploading multiple files with PHP & storing form in CSS

我有一个收集学生信息的表格,简单版在这里:

<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="">

例如,此表格随后会发送至 submit.php。 (该页面有一个随机URL然后将用户发送到一个新页面)

接下来的 PHP 获取用户的所有输入并将它们写入 CSV,其中包括学生的文件名和用于交叉引用的个人标识。 PHP 还应上传 responses 文件夹内文件夹中的每个文件。所以 csvstudentpersonal.

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

?>

由于某种原因输出了未知错误,我觉得这与两个文件被上传到同一个文件有关。这两个 ID 必须分别在同一个表格中完成。我希望我能得到报酬来解决这个问题,但我不知道从这里该何去何从,因为即使 CSV 也不会写入,而且这是基于确实有效的代码。

当目录实际存在时,已生成的 Apache error_log 对其中大多数 failed to open stream: No such file or directory 抛出 Undefined index。还有 fwrite() expects parameter 1 to be resource, boolean givenfclose() expects parameter 1 to be resource, boolean。询问是最后的手段,因为理论上这应该有效,如果出于某种原因这是一个明显的错误,我们深表歉意。

如有任何帮助,我们将不胜感激。谢谢:)

有两个问题:

  1. 您需要在 URL 的开头使用 ./ 而不是 /,这样您的 URL 是相对的,而不是指您网站的基础。 这将修复 failed to open stream: No such file or directory 错误。

  2. 如果要发送文件,您需要在表单中添加 enctype="multipart/form-data"

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

    这将修复 Undefined index: studentidentification in /home/hosting/domains/example.com/public_html/onboarding/forms/collectid/submit.php 错误。

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