开发者

Remove UTF-8 DOM in URL - JS & PHP

I am having a problem with "strange" chars in URL.

I am using Uploadify script to upload files on server. I am getting a problem when I try onComplete load a preview image.

Here is my js:

<script type="text/javascript">
$(document).ready(function() {
   $('#file_upload').uploadify({
     'height'    : 30,
     'width'     : 100,
     'buttonImg' : '<?=base_url()?>style/images/buttons/button_dodaj_gray.png',
     'cancelImg' : '<?=base_url()?>style/js/uploadify/cancel.png',
     'wmode'     : 'transparent',
     'onComplete': function(event, ID, fileObj, response, data) {
          if( response != "ERROR" )
          {
              var imageSrc = "";
              //imageSrc = "/ads_images_temp/";
              imageSrc = response;
              $('.image_container').html('<img src="<?=base_url()?>'+imageSrc+'"></img>');
          }
     },
     'fileExt'   : '*.jpg;*.jpeg;*.png;*.gif',
     'displayData': 'percentage',
     'multi'     : false,
     'uploader'  : '<?=base_url()?>style/js/uploadify/uploadify.swf',
     'script'    : '/tools/upload/product/glavna',

     'auto'      : true
   });
});
</script>

The problem is in line: $('.image_container').html('<img src="<?=base_url()?>'+imageSrc+'"></img>');.

I don't get clean URL of image but I get this: %EF%BB%BF768f32dd43cc1f90b79c83cceed57eb2.png.

Filename: 768f32dd43cc1f90b79c83cceed57eb2.png

UPDATE: My PHP file for reading image:

if ( !empty($_FILES) )
            {
                $config['upload_path'] = $temp_path;
                $config['allowed_types'] = '*';
                $config['max_size'] = 4000;
                $config['encrypt_name'] = true;

                $this->load->library('upload', $config);
                $this->upload->initialize($config);

                if( ! ($this->upload->do_upload("Filedata"))) // Napaka pri nalaganju slike
                {
                    //echo $this->upload->display_errors('<p>', '</p>');
                    echo "ERROR";
                }
                else // Vse OK, nadaljuj
                {
                    $img = $this->upload->data();

                    if($img['image_width'] > 650)
                    {
                        $config['image_library'] = 'gd2';
                        $config['source_image'] = $img['full_path'];
                        $config['create_thumb'] = TRUE;
                        $config['maintain_ratio'] = TRUE;
                        $config['width'] = 650;

                        $this->load->library('image_lib', $config);

                        $this->image_lib->resize();
                    }

                    // Resize image    
                    $this->Product_model->resize_image($img['full_path'], $temp_path.'/crop_'.$img["raw_name"].$img["file_ext"], 160, 120);
                    $this->Product_model->resize_image($img['full_path'], $temp_path.'/crop_crop_'.$img["raw_name"].$img["file_ext"], 80, 60);

                   开发者_如何学编程 //$this->session->set_userdata("oddaja_oglasa_glavnaSlika",$img["raw_name"].$img["file_ext"]);

                    $imeSlike = $img["raw_name"].$img["file_ext"];

                    echo $imeSlike;
                }
            }

EDIT 2:

Chrome console screenshot: http://imageshack.us/f/818/uploadifychromeconsole1.png/

Regards, Mario


EF BB BF is the UTF-8 representation of the Byte Order Mark (BOM). Something is saving or returning the UTF-8 encoded data with the optional BOM at the beginning.

Looks like it has been reported in the uploadify forum.

Edit

OK, so the file that echo $imeSlike must have been saved with the BOM as the first 3 bytes. You need to configure your editor to not add the BOM when saving PHP files (or indeed any file).

As an alternative, if you are using Windows, a great editor for highlighting these sorts of encoding problems is EditPad (although there are plenty of others in which you can configure whether the BOM is added). You can turn off the BOM in the Options. Or you could try saving the file as ASCII encoding which will also remove the BOM. However, depending on the content of your PHP this might not be a practical solution.


I have found a solution - JSON!:)

Echo in PHP:

    $json = array("name" => $imeSlike, "error" => 0);
    echo json_encode($json);

In JS:

...
'onComplete': function(event, ID, fileObj, response, data) {
        eval("var obj1="+response);

        if( obj1.error == 0 )
        {
            $('.image_container').html('<img src="<?=base_url()?>ads_images_temp/crop_'+obj1.name+'"></img>');
        }
     },
...

Thank you all for your time and sharing solutions!

Regards, Mario

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜