Php 图像上传-拉丁字符问题

Php 图像上传-拉丁字符问题,php,Php,我使用此脚本将图像上载到服务器: <?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;

我使用此脚本将图像上载到服务器:

 <?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....
    }

?>

这通常取决于底层文件系统


下面有什么文件系统?

您的页面是UTF-8,但服务器是拉丁语-1。您必须使它们相同。

这是在什么类型的服务器上运行的?这看起来很像是一个UTF-8编码的表单,但是文件名在某个地方被更改为拉丁语-可能是在文件写入文件系统时。这就是为什么知道您在什么类型的服务器/操作系统上运行它很重要的原因。最后,这取决于所使用的文件系统

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

你也可以考虑去掉非拉丁字符。这通常是最简单的方法,我一直在用umlauts

这是关于编码的一般性好读物:
我按照您的建议修改代码: 这只是一个片段:

   $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éK hegyek.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>";
     }
$images=glob(“$directory.”*”;
$imgs='';
foreach($imagesas$image){$imgs[]=“$image”;}
$imgs=阵列片($imgs,0,20);
foreach($imgs作为$img){
//$img=utf8\u解码($img);
回声“;
回声“;
回声“
\n”; 回声“
\n”; 回声“; 回音“\n”; 回声“; }
这是一个很好的工作方式,但是提到的文件名是错误的:K�k hegyek.jpg 我尝试在这里使用UTF8_解码(未注释行), 但结果是:K?hegyek.jpg

之后,我尝试使用UTF8_编码,结果是:KéK hegyek.jpg

但不幸的是,代码的链接部分是错误的,因为链接是:

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

取消链接($filename)

文件名是:KéK hegyek.jpg,而不是KéK%20hegyek.jpg,因此我无法删除它

我快疯了

我的最终解决方案是:

  • 在远程文件中,将“空格”替换为“\u1”
  • 然后$remote\u file=utf8\u解码($remote\u file)
  • 打印文件名$img=utf8\u encode($img)时
  • 当点击删除时:取消链接(utf8_解码($_POST['fordelete'])
    这个解决方案对我来说是可行的。我认为这不是最好的解码方式——哇,但对我来说没关系

    您使用的是什么版本的PHP?很高兴看到至少有人知道如何正确称呼我们。:)顺便说一句,您的
    $remote_文件创建非常不安全。如果您必须接受来自用户的文件名(这不是一个好主意),则需要对该名称进行严格的清理。这并不像听起来那么简单;除了处理斜杠问题外,还有许多字符(以及像
    COM
    这样的特殊情况名称)将给Windows服务器带来极大的困难。此外,您当前允许上传任何文件;它必须通过
    getimagesize
    进行解析才能正确处理,但如果它不是有效的图像(例如IE将嗅探并执行的一些HTML),它仍然会保存文件。我如何检查服务器是否为Latin-1?上传后,我读回文件名,使用echo的basename($img),名称就可以了。只有在驱动器上,我才得到这个错误的文件名。在文件名上使用utf8_decode()。如果您使用的是Windows,则可能使用ANSI(cp 1252),拉丁语-1的兼容性为99%。服务器为:Wamp 2.0(Apache 2.2.11;PHP:5.2.9-2;MYSQL:5.1.33)。文件系统NTFS,Op.system XP Prof-Hungarian(包含拉丁字符支持…)