如何从输入 php 文件中获取数据类型

How to get data type from input php file

所以这是我的前端代码:

<form action="" method="post">
    <input type="file" name="image" class="input">
</form>

和控制器:

$upload_image = $_FILES['image']['name'];
        if ($upload_image) {
            $config['allowed_types'] = 'xml|docx|jpg|png|pdf|xlsx';
            $config['max_size'] = '999999';
            $config['upload_path'] = './assets/img/image/';
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('image')) {
                $new_image = $this->upload->data('file_name');
                $this->db->set('image', $new_image);
            } else {
                echo $this->upload->display_errors();
            }
        }

在 $config 中,prog 知道刚刚输入的文件类型,如 img docx 和其他文件,我如何获得刚刚输入的文件类型作为 $types?

如果您询问如何获取刚刚提交的文件的文件类型,那很简单,您只需使用

<?php

    $types = $_FILE['file']['type'];

?>

pathinfo 是一个数组。我们可以查看目录名文件名扩展名

$path_parts = pathinfo('test.png');

echo $path_parts['extension'], "\n";
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['filename'], "\n";

https://www.php.net/manual/en/function.pathinfo.php