简体   繁体   中英

How to take out .gif, .jpg or .png

I use Windows and I display a file extention.

When I upload an image file with PHP, a image name of image1_big.jpg becomes image1_big.jpg.jpg.

And image2.gif becomes image2.gif.gif.

I don't want to turn off file extension.

How can I avoid this problem?

function addProduct(){
    $data = array( 
        'name' => db_clean($_POST['name']),
        'shortdesc' => db_clean($_POST['shortdesc']),
        'longdesc' => db_clean($_POST['longdesc'],5000),
        'status' => db_clean($_POST['status'],8),
        'class' => db_clean($_POST['class'],30),
        'grouping' => db_clean($_POST['grouping'],16),
        'category_id' => id_clean($_POST['category_id']),
        'featured' => db_clean($_POST['featured'],5),
        'price' => db_clean($_POST['price'],16)

    );

    if ($_FILES){
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200';
        $config['remove_spaces'] = true;
        $config['overwrite'] = false;
        $config['max_width']  = '0';
        $config['max_height']  = '0';
        $this->load->library('upload', $config);    
        if (strlen($_FILES['image']['name'])){
            if(!$this->upload->do_upload('image')){
                $this->upload->display_errors();
                exit();
            }
            $image = $this->upload->data();
            if ($image['file_name']){
                $data['image'] = "images/".$image['file_name'];
            }
        }
        if (strlen($_FILES['thumbnail']['name'])){
            if(!$this->upload->do_upload('thumbnail')){
                $this->upload->display_errors();
                exit();
            }
            $thumb = $this->upload->data();
            if ($thumb['file_name']){
                $data['thumbnail'] = "images/".$thumb['file_name'];
            }
        }
    }
    $this->db->insert('omc_products', $data);   

    $new_product_id = $this->db->insert_id();
...
...

if you feel you need to, you can just strip the file extention. However, i would go with BalusC's comment that it is not the correct behavior for PHP in general:

$filename = substr($filename_passed_to_upload, 0, (strlen($filename_passed_to_upload) - 4));

or, more rigourously:

$temp = explode(".", $filename_passed_to_upload);
$new_filename = $temp[0]; 
// be careful, this fails when the name has a '.' in it other than for the extention
// you'll probably want to do something with a loop with $temp

The basename function will take care of the unnecessary extension(s) if you tell it how:

$filename = dirname($old_filename) . '/' . basename($old_file_name, '.gif');

Alternatively, there's the Perl regex to remove double extensions (this will handle all double- and triple-extended files), so that only the last extension is used.

#                                +-+-- handles .c through .jpeg
#                                | |
$filename = preg_replace('/(\\..{1,4}){2,}$/', '$1', $old_filename);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM