使用php保存文件时设置字符集 [英] set charset when saving files with php
问题描述
我创建了一个简单的代码,用于使用PHP将照片上传到文件夹。
I created a simple code for uploading pictures to a folder, with PHP.
在服务器端我有
<?php
header('Content-Type: text/plain; charset=utf-8');
//check if file is actually an image etc.
//if is an image, send it to "upload" folder
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
//save to the database a string like "upload/myImage.jpg", so I can render it on the site later
$stu = $dbh->prepare("UPDATE multi SET m_place=:name WHERE m_id = :id");
$stu->bindParam(':name', $n, PDO::PARAM_STR);
$n= "upload/".$_FILES["file"]["name"];
$stu->execute();
问题?
的图像是在文件夹中的英文我看到一个myImage01.jpg,在数据库中也upload / myImage01.jpg。但是,如果图像的名称在希腊语中的文件夹中,我看到OOΞΞΏ.jpg和在数据库上传/ΟΟ‰Ο-噢,Ξ-ΟΞ»ΏΏ.jpg。这是错的。我应该得到χωριςτιτλο(这是希腊的无标题btw)。所以,我猜charset问题?
If the name of the image is in english in the folder I see a "myImage01.jpg" and in the database also "upload/myImage01.jpg". But, if the name of the image is in greek in the folder I see "χωΟΞ―Ο‚ τίτλο.jpg" and in the db "upload/χωΟΞ―Ο‚ τίτλο.jpg".Which is wrong. Insted of χωΟΞ―Ο‚ τίτλο I should get "χωρις τιτλο" (thats greek for "no title" btw). So , I guess charset problem?
如何解决这个问题?
提前感谢
推荐答案
t具有正确的归类。确保表/列使用 utf8_general_ci
进行排序。
It sounds like your database doesn't have the correct collation. Make sure the tables/columns are using utf8_general_ci
for their collation.
处理UTF8时非常重要的是使用以下两个MySQL行 GET
请求...
Also extremely important when handling UTF8 is to use the following two MySQL lines for GET
requests...
SET time_zone = '+00:00'
SET CHARACTER SET 'utf8'
...和当您有 POST
请求时使用以下两个...
...and when you have a POST
request use the following two...
SET time_zone = '+00:00'
SET NAMES 'utf8'
请确保正确维护UTF8字符。
These will help ensure that UTF8 characters are maintained correctly.
这篇关于使用php保存文件时设置字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!