Home  >  Q&A  >  body text

How to get data type from input php file

This is my front-end code:

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

and controller:

$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();
            }
        }

In $config, prog knows the file type just entered, such as img docx, etc. How can I get the file type just entered as $types?

P粉670107661P粉670107661185 days ago239

reply all(2)I'll reply

  • P粉786432579

    P粉7864325792024-03-23 09:14:05

    pathinfo is an array. We can check directory name, file name, extension, etc.

    $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

    reply
    0
  • P粉438918323

    P粉4389183232024-03-23 00:21:05

    If you're asking how to get the file type of the file you just submitted, it's easy, you just use

    reply
    0
  • Cancelreply