php:上传后,如何在数据库上显示上传的文件名?

时间:2019-02-13 14:52:03

标签: php html mysql

我创建了一个表格,用户可以通过该表格申请职位。一切工作正常,文件正在上传到目标“上传”。但是现在我面临这个问题: 上传的文件名未显示在“ uploadCV”下的数据库列中

我尝试添加<input type="hidden" name="uploadCV" /><input type="hidden" name="uploadFile" />",但出现此错误:

不确定的索引:uploadCV。您认为我应该怎么做?

uploadFile.php:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadFile"]["name"]);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["uploadFile"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["uploadFile"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($FileType != "jpg" && $FileType != "png" && $FileType != "jpeg"
    && $FileType != 'pdf' && $FileType != "doc" && $FileType != "docx" ) {
    echo "Sorry, only JPG, JPEG, PNG, PDF and Doc files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

create-form.php:

<?php
//header("Location: http://localhost/Rocket/includes/thankYou.php");


include('connection.php');
include('../includes/uploadFile.php');

if(isset($_POST['addForm'])) {

    $fullName = $_POST['fullName'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $dob = $_POST['dob'];
    $degree = $_POST['degree'];
    $expYears = $_POST['expYears'];
    $position = $_POST['jobPosition'];
    $whyHire = $_POST['whyHire'];
    $uploadCV = $_POST['uploadCV'];
    $dateApplied = $_POST['dateApplied'];


    $db = new Database();
    $db->connect();
    $db->insert('users',array('fullName'=>$fullName,'email'=>$email, 'mobile'=>$mobile,
        'dob'=>$dob, 'degree'=>$degree, 'expYears'=>$expYears, 'position'=>$position,
        'whyHire'=>$whyHire, 'uploadCV'=>$uploadCV, 'dateApplied'=>$dateApplied));  // Table name, column names and respective values
    $res = $db->getResult();
    print_r($res);

    if($res) {
        echo "Sent to DB";
        die();
    } else {
        echo "query error";
    }

}

$target_dir = "/uploads/";
$target_file = $target_dir . basename($_FILES["uploadCV"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["uploadCV"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["uploadCV"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["uploadCV"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["uploadCV"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

jobForm.php:

<!DOCTYPE html>
<html>
<head>
    <title> Job Application Form </title>
</head>

<body>

<form action="../server/create-form.php" method="POST" enctype="multipart/form-data">
    <input type="text" name="fullName" placeholder="Full Name" />
    <input type="email" name="email" placeholder="E-mail Address" />
    <input type="number" name="mobile" placeholder="Mobile Number" />
    <!-- Birth Day -->
   <input type="date" name="dob" />
    <!-- Academic Degree -->
    <div>
        <select name="degree">
            <option value="High school">High School</option>
            <option value="Diploma">Diploma</option>
            <option value="Bachelor">Bachelor</option>
            <option value="Masters">Masters</option>
            <option value="PHD">PH.D</option>
        </select>
    </div>
    <!-- Years of EXP -->
    <div>
        <select name="expYears">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
            <option value="16">16</option>
            <option value="17">17</option>
            <option value="18">18</option>
            <option value="19">19</option>
            <option value="20">20</option>
        </select>
    </div>
    <!-- job position -->
    <div>
        <select name="jobPosition">
            <option name="jobPosition" selected ><?php echo $_GET['jobTitle']; ?></option>
            <option name="Web Developer" value="Web Developer">Web Developer</option>
            <option name="Web Designer" value="Web Designer">Web Designer</option>
            <option name="Graphic Designer" value="Graphic Designer">Graphic Designer</option>
            <option name="Data Scientist" value="Data Scientist">Data Scientist</option>
            <option name="Business Manager" value="Business Manager">Business Manager</option>
        </select>
    </div>
    <!-- why hire? -->
    <div>
        <textarea name="whyHire" placeholder="Why should we hire you?"></textarea>
    </div>
    <!-- Upload CV -->
    <div>
        <input type="file" name="uploadFile"/>
        <input type="hidden" name="uploadFile"/>
    </div>
    <!-- date applied -->
    <input type="hidden" name="dateApplied" value="<?php echo date('Y-m-d')?>" />
    <!-- Submit Form -->
    <input type="submit" name="addForm" value="Apply for this position"/>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我想说的是从上传脚本中删除它。那里有文件名,请使用该文件名来推送数据库。

编辑:

// this is your insert query with the only thing changed is uploadCV
    $db->insert('users',array('fullName'=>$fullName,'email'=>$email, 'mobile'=>$mobile,
        'dob'=>$dob, 'degree'=>$degree, 'expYears'=>$expYears, 'position'=>$position,
        'whyHire'=>$whyHire, 'uploadCV'=>$target_file, 'dateApplied'=>$dateApplied));  // Table name, column names and respective values
    $res = $db->getResult();