2009-12-29 45 views
0

我使用Windows并显示文件扩展名。如何取出.gif,.jpg或.png

当我用PHP上传图片文件时,image1_big.jpg的图片名称变成image1_big.jpg.jpg。

而image2.gif变成image2.gif.gif。

我不想关闭文件扩展名。

我该如何避免这个问题?

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(); 
... 
... 
+5

这不是默认行为。这是(错误的)PHP文件上传处理代码造成的。如果您发布了SSCCE(http://sscce.org),那么我们可能会在PHP代码中发现错误。 – BalusC 2009-12-29 21:39:14

+0

将你的脚本粘贴在这里... – 2009-12-29 21:44:08

+0

这真的没有告诉我们什么。我们可能需要看到do_upload()函数和/或data()函数。 – helloandre 2009-12-29 23:35:04

回答

1

如果您觉得需要,您可以将文件扩展名剥离。但是,我会BalusC的评论去,这不是一般的正确行为PHP:

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

,或者更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 
0

basename功能会照顾不必要的延长( ),如果你告诉它如何:

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

另外,还有Perl的正则表达式来去除双扩展(这将处理所有双和三重扩展文件),使Ô最后使用最后的扩展名。

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