1

I created a simple code for uploading pictures to a folder, with PHP.

On the server side I have

    <?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();

The problem?

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?

How do I fix this?

Thanks in advance

slevin
  • 4,166
  • 20
  • 69
  • 129

2 Answers2

2

It sounds like your database doesn't have the correct collation. Make sure the tables/columns are using utf8_general_ci for their collation.

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'

...and when you have a POST request use the following two...

SET time_zone = '+00:00'
SET NAMES 'utf8'

These will help ensure that UTF8 characters are maintained correctly.

John
  • 1
  • 13
  • 98
  • 177
  • I change the collation of the table's column using pgAdmin. I set it to `pg_catalog."C"`. That's for postgreSQL. Names save fine now. But in the folder I still see `χωΟΞ―Ο‚ τίτλο`. In the php file I have ` header('Content-Type: text/plain; charset=utf-8');` and `$dbh->exec("SET NAMES UTF8"); $dbh->query('SET NAMES UTF8');`. Of course webpage not render the image because it looks for an image that does not exist. Should I edit the php file? Or maybe the apache server? Or it's Window's fault. I dont know, I'm lost – slevin Sep 24 '13 at 22:58
  • Make sure you aren't saving documents with a BOM (Byte Order Mark). Read more at my site here: http://www.jabcreations.com/web/xhtml/editors – John Sep 24 '13 at 23:02
  • I use DreamWeaver. Interesting article. Thanks. In DreamWeaver, encoding is `utf8`, Unicode Normalization Form is `C` and the Include Unicode Signature (BOM) is unchecked. So I'm coverd (?). I just cannot understand if its the server, the database or the file that handles uploading. – slevin Sep 24 '13 at 23:18
  • "in the folder I still see..." ...so what are you using to view the folder? No idea the OS, the file manager, etc. For Windows you'd have to make sure the language support is installed under `Region and Language` in the control panel. I'm crashing, I'll check replies tomorrow. :) – John Sep 24 '13 at 23:45
  • I have Windows 7 Home Premium. The default Language in `Region and Language` is Greek. So...Yeah...I have no idea what to do now...Anyway, thanks – slevin Sep 24 '13 at 23:51
  • Thanks for your time. I found the solution. But thanks anyway, your info about the BOM and Region and Language made me wiser – slevin Sep 26 '13 at 18:27
0

Finally, I figure out that "PHP filesystem functions can only handle characters that are in system codepage". Thanks to this I solve my problem.

I used the iconv function

So I changed the move_uploaded_file line like so

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . iconv('UTF-8', 'Windows1253',$_FILES["file"]["name"]));
Community
  • 1
  • 1
slevin
  • 4,166
  • 20
  • 69
  • 129