开发者

Upload problem with PHP: Thumbnail picking up the image name

I have the following model(codeigniter) code to upload image and thumbnail.

However the result of image path becomes, images/comfort_big.jpg and images/comfort_big.jpg.jpg.

The thumbnail image picks up the name of image and add .jpg.

I am uploading images/confort_thumb.jpg for thumb.

Can anyone tell me what's wrong please?

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开发者_如何学JAVA->upload->display_errors();
                exit();
            }
            $thumb = $this->upload->data();
            if ($thumb['file_name']){
                $data['thumbnail'] = "images/".$thumb['file_name'];
            }
        }
    }


I had a quick look at File Uploading Class in the user guide
In the preferences tables it states:

Note:The filename should not include a file extension.

So my guess is that you get the user to name the file

$config['file_name'] = "User defined";

or remove the extension programmatically. Since you know and list the extensions already you could do

$types = array(".gif",".jpg",".png"); 
$image['file_name'] = str_replace($types , "", $image['file_name'] );


Your problem is that when you create a thumb you are passing the full image.

So it will add another extension to it.

Use Below code to remove extension from file name.

<?php 
    $filename=$image['file_name'];

    $file_name_with_dot = substr($filename,0, strrpos($filename, '.') + 1);

    $only_file_name = substr($file_name_with_dot,0,strlen($file_name_with_dot)-1);
?>

Now pass this $only_file_name variable to your thumb function..

It will be better to use automatic thumb creation from main image.

Hope this will help for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜