保存在 mysql 表中时,文件名中不包含文件格式


File format not included in file name when saved in mysql table

出于某种原因,当我上传文档并将文件名存储在mysql表中时,文件名中不包含文件扩展名。例如,我上传了pdf文件"car.pdf",它在mysql表中的存储方式是"car"。(请参阅单词后面的句点,不包括文件扩展名)。我已经检查了这个网站,我也用谷歌搜索过,但找不到明确的答案。

    $doc_name = mysqli_real_escape_string($dbc, trim($_FILES['doc']['name']));
    $doc_type = $_FILES['doc']['type'];
    $doc_size = $_FILES['doc']['size'];
            if(!empty($title)){
            if(!empty($doc_name)){
                if (($doc_type == 'application/pdf') || ($doc_type == 'application/msword') || ($doc_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')){
                    if(($doc_size > 0) && ($doc_size <= FILE_MAXFILESIZE)){
                        if ($_FILES['doc']['error'] == 0) {
                        $final_name = time() . $doc_name;
                        $target = $uploadpath . $final_name;
                            if(move_uploaded_file($_FILES['doc']['tmp_name'], $target)) {
                                $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('There was a problem with your query. Please, contact administrator'); 
                                if(!empty($description)){
                                $query = "INSERT INTO uploaded_files (file, title, description, date_time, user_id, dep_id) VALUES ('$final_name', '$title', '$description', now(), '" . $_SESSION['user_id'] . "', '$dep_id')";
                                }.... rest of the code goes here

如果字符串的长度超过列定义中的长度,MySQL可能会截断您正在插入的字符串。在这种情况下,请检查文件路径名的字符串名称是否超过 32。

通常,人们会期望此 INSERT 查询失败,但如果配置设置允许,它可能不会这样做。

如果 mysql 配置在您的控制之下(并且您希望避免插入字符串的这种静默截断),请首先将strict_trans_tables添加到 my.ini(OR my.conf)文件中的sql_mode设置中。

祝你好运。