Image upload - Latin chars problem
I use this script to upload images to serveR:
<?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....
}
?>
This is works well, till i upload a photo with latin chars in filename. For example the filename: kék hegyek.jpg. After upload file name will be: KĂ©k hegyek.jpg
How can i solve this?
Thank you
This is generally down to the underlying filesystem.
What filesystem do you have underneath this?
Your page is in UTF-8 but server in Latin-1. You have to make them the same.
What kind of a server is this running on? This looks very much like you have a UTF-8 encoded form, but the file name gets changed to latin somewhere along the way - possibly when the file is written to the file system. That's why it's important to know what kind of server / operating system you run this on. I the end, it's down to the file system used.
If your server's filesystem doesn't support UTF-8, you can try to use utf8_decode() or iconv() to convert the name into the right character set.
You can also think about stripping out non-latin characters. That is most often the easiest way, I do that all the time with umlauts.
This is general good reading about encodings: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
I modify the code, as you suggested: This is just a snippet:
$remote_file =$directory."/".$_FILES["image_upload_box"]["name"];
$remote_file=utf8_decode($remote_file);
imagejpeg($image_source,$remote_file,100);
chmod($remote_file,0644);
Ok, now, after uploading the image, the file name is correct: Kék hegyek.jpg
This part of my code, i read all images from directory and listing them:
$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>";
}
This is work well, but the mentioned file-name wrong: K�k hegyek.jpg I tried to use UTF8_DECODE here (the uncommented line), but so the output was: K?hegyek.jpg
After then i tried to use UTF8_ENCODE and voila, the output: Kék hegyek.jpg
But unfortunatelly, the link part of the code wrong, 'coz the link is: http://localhost/page/Kék%20hegyek.jpg.
And the problem is, that i have a button, which i can delete the image.
unlink($filename);
The file name is: Kék hegyek.jpg and not Kék%20hegyek.jpg so i can't delete it.
Im going crazy...
The final solution for me:
- In the remote file replace "space" to "_"
- Then $remote_file=utf8_decode($remote_file);
- When print the filename $img=utf8_encode($img);
- And when click to delete: unlink(utf8_decode($_POST['fordelete']));
This solution is work for me. I think not the best way to decode-encode-decode--whaah, but its ok for me.
精彩评论