2009-11-26 96 views
0

我使用这个脚本上传图片到服务器:图片上传 - 拉丁字符问题

<?php 

if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" && ($_FILES["image_upload_box"]["size"] < 2000000)) 
    { 

     $max_upload_width = 450; 
     $max_upload_height = 450; 
     if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){ 
      $max_upload_width = $_REQUEST['max_width_box']; 
     }  
     if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){ 
      $max_upload_height = $_REQUEST['max_height_box']; 
     } 
     if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){  
      $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]); 
     }  

     $remote_file =$directory."/".$_FILES["image_upload_box"]["name"]; 
     imagejpeg($image_source,$remote_file,100); 
     chmod($remote_file,0644); 

     list($image_width, $image_height) = getimagesize($remote_file); 

     if($image_width>$max_upload_width || $image_height >$max_upload_height){ 
      $proportions = $image_width/$image_height; 

      if($image_width>$image_height){ 
       $new_width = $max_upload_width; 
       $new_height = round($max_upload_width/$proportions); 
      }  
      else{ 
       $new_height = $max_upload_height; 
       $new_width = round($max_upload_height*$proportions); 
      }  


      $new_image = imagecreatetruecolor($new_width , $new_height); 
      $image_source = imagecreatefromjpeg($remote_file); 

      imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); 
      imagejpeg($new_image,$remote_file,100); 

      imagedestroy($new_image); 
     } 

     imagedestroy($image_source); 



    }else{ 
     something.... 
    } 

?> 

这是效果很好,直到我上载与文件名字符拉丁文中的照片。 例如文件名:kékhegyek.jpg。上传文件名后将是:KĂ©k hegyek.jpg

我该如何解决这个问题?

谢谢

+0

您使用的是什么版本的PHP? – Mez 2009-11-26 14:21:14

+0

很高兴看到至少*某人*知道如何正确解决我们的问题。 :) – 2009-11-26 14:26:08

+0

Php版本:5.2.9-2 – Holian 2009-11-26 14:33:55

回答

0

这通常是下面的底层文件系统。

你下面有什么文件系统?

+0

Filesystem:NTFS – Holian 2009-11-26 14:35:53

0

您的页面采用UTF-8格式,但服务器采用Latin-1格式。你必须让他们一样。

+0

如何检查服务器是否为Latin-1?上传后,我读回文​​件名和echo基名($ img),名字是好的。只有在驱动器上我得到这个假文件名 – Holian 2009-11-26 14:39:06

+0

在文件名上使用utf8_decode()。如果你在Windows上,你可能使用ANSI(cp 1252),Latin-1是99%兼容的。 – 2009-11-26 14:51:56

0

这是在运行什么样的服务器?这看起来很像你有一个UTF-8编码形式,但文件名被改为拉丁文的某处 - 可能是文件写入文件系统时。这就是为什么知道你在哪种服务器/操作系统上运行这一点很重要。最后,这取决于所使用的文件系统。

如果您的服务器的文件系统不支持UTF-8,则可以尝试使用utf8_decode()iconv()将名称转换为正确的字符集。

你也可以考虑剥离非拉丁字符。这通常是最简单的方法,我总是用变音符号来完成。

这是关于编码总体上是好的阅读: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

+0

服务器是:Wamp 2.0(Apache 2.2.11; PHP:5.2.9-2; MYSQL:5.1.33)。文件系统NTFS,Op.System XP教授 - 匈牙利语(包含拉丁字符支持..) – Holian 2009-11-26 14:44:43

0

我修改代码,如你所说: 这只是一个片段:

$remote_file =$directory."/".$_FILES["image_upload_box"]["name"]; 
    $remote_file=utf8_decode($remote_file); 
    imagejpeg($image_source,$remote_file,100); 
    chmod($remote_file,0644); 

好了,现在,上传图片后,文件名是正确的:Kékhegyek.jpg

这部分我的代码,我从目录中读取所有图像并列出它们:

$images = glob("" . $directory . "*"); 
    $imgs = ''; 
    foreach($images as $image){ $imgs[] = "$image"; } 
    $imgs = array_slice($imgs, 0, 20); 
    foreach ($imgs as $img) { 
    // $img=utf8_decode($img); 
    echo "<form action='datasheet_edit.php' id='$img' method='post'>"; 
    echo "<div class=\"photo\">"; 
    echo "<img src='$img' width='100' height='50%' alt=\"\"><br>\n"; 
    echo "<a href=\"$img\">",basename($img),"</a><br>\n</div>"; 
    echo "<input type='hidden' id='fordelete' name='fordelete' value='$img' />"; 
    echo "</div>\n"; 
    echo "</form>"; 
    } 

这是很好的工作,但上述文件名错误:KK hegyek.jpg 我试图用UTF8_DECODE这里(的注释行), 但这样的产量为:K hegyek.jpg

然后后,我试图用函数utf8_encode,瞧,输出:KEK hegyek.jpg

但可惜的是,代码错误的链接部分,“怎么该链接是:http://localhost/page/Kék%20hegyek.jpg

问题是,我有一个按钮,我可以删除图像。

unlink($ filename);

文件名是:Kékhegyek.jpg而不是Kék%20hegyek.jpg所以我不能删除它。

我要疯了...

对我来说,最终的解决方案:

  1. 在远程文件替换的 “空间”,以 “_”
  2. 然后$ remote_file = utf8_decode(remote_file $);
  3. 当打印文件名$ img = utf8_encode($ img);
  4. 当点击删除:unlink(utf8_decode($ _ POST ['fordelete']));

此解决方案适用于我。我认为不是解码 - 编码 - 解码的最佳方式 - whaah,但它对我来说可以。