php - 如何检查php上传文件的目录是否正确?

标签 php mysql

我正在制作一份申请表,供用户申请工作并上传简历。我制作的 php 代码将文件名发送到数据库,因此我猜它工作正常。

因为经过几次测试后我没有看到任何文件。我尝试更改上传目录,以便当用户上传简历时,它会转到根目录中的“uploads”文件夹。

<?php

if(isset($_POST['uploadCV'])) {
    $file = $_FILES['uploadCV'];
    $fileName = $_FILES['uploadCV']['name'];
    $fileTmpName = $_FILES['uploadCV']['tmp_name'];
    $fileSize = $_FILES['uploadCV']['size'];
    $fileError = $_FILES['uploadCV']['Error'];
    $fileType = $_FILES['uploadCV']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png', 'doc', 'docs', 'pdf');
    if(in_array($fileActualExt, $allowed) ) {
        if($fileError === 0) {
            if($fileSize < 50000 ) {
                $fileNameNew = uniqid('', true). '.' . $fileActualExt;
                $fileDestination = 'uploads/' . $fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                echo "File uploaded!";
            } else {
                echo "File is too large...minimum size is 50MB";
            }
        } else {
            echo "there was a error uploading your file, please ty again!";
        }
    } else {
        echo "You can't upload this file type!";
    }
}

对于 HTML:

<input type="file" name="uploadCV"/>

创建表单.php:

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

include('connection.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";
    }
}

我期望代码将用户选择的文件上传到“上传”文件夹,但遗憾的是没有运气,我不明白为什么。但我有一种感觉,我写的上传目录的方式可能是错误的。

最佳答案

伙计,你应该更改 if 条件如下:

<?php

if(isset($_FILES['uploadCV'])) {
    $file = $_FILES['uploadCV'];
    $fileName = $_FILES['uploadCV']['name'];
    $fileTmpName = $_FILES['uploadCV']['tmp_name'];
    $fileSize = $_FILES['uploadCV']['size'];
    $fileType = $_FILES['uploadCV']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png', 'doc', 'docs', 'pdf');
    if(in_array($fileActualExt, $allowed) ) {
            if($fileSize < 50000 ) {
                $fileNameNew = uniqid('', true). '.' . $fileActualExt;
                $fileDestination = 'uploads/' . $fileNameNew;
                print_r($fileDestination);
                move_uploaded_file($fileTmpName, $fileDestination);
                echo "File uploaded!";
            } else {
                echo "File is too large...minimum size is 50MB";
            }
    } else {
        echo "You can't upload this file type!";
    }
}
?>

<html>
    <body>
        <form action="" enctype="multipart/form-data" method="post">
            <input type="file" name="uploadCV"/>            
            <input type="submit" name="subkit" value="submit"/>            
        </form>
    </body>
</html>

您应该检查$_FILES而不是$_POST。另外,您可以从 $_FILES 中删除错误。希望它会对您有所帮助。

关于php - 如何检查php上传文件的目录是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54631359/

相关文章:

php - 如何通过 PHP 中的运行时脚本在 MySQL 中禁用区分大小写

php - 如何比较 php 变量与 mysql 中的日期列?

mysql - 来自一列的分组伙伴

mysql - 如何为同一数据库中的所有表设置唯一的主键

php - 如何打印(导出)到 MS-Word 搜索结果 (PHP)

php - 如果记录相同则更新 SQL 行

php - PHP 中的 Singleton 类的新实例用于每个 HTTP 请求

PHP header (位置 :) redirect with root directory

mysql - 内连接 MySQL 错误 : 1054 (Unknown column 'prodotti.id' in 'on clause' )

mysql - 使用一些文本更新最新(按 ID)记录中的字段