我使用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();
...
...
這不是默認行爲。這是(錯誤的)PHP文件上傳處理代碼造成的。如果您發佈了SSCCE(http://sscce.org),那麼我們可能會在PHP代碼中發現錯誤。 – BalusC 2009-12-29 21:39:14
將你的腳本粘貼在這裏... – 2009-12-29 21:44:08
這真的沒有告訴我們什麼。我們可能需要看到do_upload()函數和/或data()函數。 – helloandre 2009-12-29 23:35:04