Home > Software engineering >  How to get data type from input php file
How to get data type from input php file

Time:05-29

So here is my front code:

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

and the 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 the $config the prog know the types of file that just inputted, like img docx and the other, how i can get the file types that just inputted as $types?

CodePudding user response:

If you are asking how to get the file type of a file just submitted that's easy you only have to use

<?php

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

?>

CodePudding user response:

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

  • Related