Solved

using fopen with hebrew file name inside a UTF-8 encoded file problem

Posted on 2008-10-29
9
1,782 Views
Last Modified: 2013-12-13
Hello Experts,

I'm having problems saving a file in php which has an hebrew file name characters.
The problem occurs if the php file which I run the script from is a UTF-8 encoded php file.
If the php file is ANSI encoded the file created is ok.

I'm working with Windows 2000 Server and the Regional Options of the system are configured for the Hebrew language.

Please your advice,
Many thanks,
Doron Tal


<?php

$fp = fopen('images/àáÙÕß.dat', 'w');

fwrite($fp, '1');

fwrite($fp, '23');

fclose($fp);

?>

Open in new window

test-ansi.txt
test-utf.txt
0
Comment
Question by:doront99
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Assisted Solution

by:mgonullu
mgonullu earned 100 total points
ID: 22839031
Please see this document:
http://www.php.net/fwrite

if you have to write a file in UTF-8 format, you have to add an header to the file like this :

<?php
$f=fopen("test.txt", "wb");
$text=utf8_encode("ýaý!");
// adding header
$text="\xEF\xBB\xBF".$text;
fputs($f, $text);
fclose($f);
?>
0
 

Author Comment

by:doront99
ID: 22839098
hey mgonullu,

I don't need to write a file in UTF-8 format.

What I need is to name the file with a utf-8 file name.
Please see example attached to my original message.

Thanks,
Doron
0
 

Author Comment

by:doront99
ID: 22839102
the issue as actually about the FILE NAME and not about the file's content.

Thanks
0
 
LVL 9

Expert Comment

by:mgonullu
ID: 22845893
Hmmm,
I get the point,
what is the error your are getting?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:doront99
ID: 22847772
it's not an error, but the file's name that was created on the folder is wrong.
for example: ó ó¡ó"ó"óŸ.dat instead of àáÙÕß.dat
which are the same name, but the first is the unicode in characters (two chars for each letter in the string).

Thanks,
Doron
0
 
LVL 25

Assisted Solution

by:Squinky
Squinky earned 100 total points
ID: 22848478
You say the system is configured for Hebrew - is it using an 8-bit encoding (ISO-8859 or Windows code page) or unicode? If you are writing UTF-8 filenames in an 8-bit system, it will not work. Make sure your system is expecting UTF-8, or alternatively switch your charset to UTF-system-wide.
0
 

Accepted Solution

by:
doront99 earned 0 total points
ID: 22892704
From what I read about the issue on the net, PHP prior version 6 does not support UTF inside the code (like UTF-8 variables).

So, I guess this is it.

Thanks,
Doron
0
 
LVL 25

Expert Comment

by:Squinky
ID: 22894238
No, that's not it. This kind of thing should not be affected by whether PHP supports unicode or not. PHP will happily handle UTF-8 data transparently, even in PHP5. What PHP6 changes is how it processes UTF-8 data, that is functions like strlen do not understand UTF-8 in PHP5, so you will get unexpected results, however, that has zero impact on being able to put some UTF-8 data into a string and pass it around without problems.

You can't have UTF-8 variable names, but there is nothing stopping you putting literal UTF-8 strings into variables and it will work just fine, for example:

$var = "­)3é4";

(that's a mixture of Cyrilic, Amenian, Katakana, Arabic and Hebrew, fingers crossed that EE copes with it...)

For that string, strlen will give the wrong result in PHP5 but the correct one in PHP6. Either way, if you do:

echo $var;

it will work fine in both.

Now i would expect that you can use UTF-8 for filenames too, if your underlying OS supports it, as PHP just treats it as a bunch of bytes. It's quite common for OSs to use UTF-16 instead, so you probably just need to make sure that your encodings line up.

I just tried this on OS X:

<?php
$var = "­)3é4";
file_put_contents($var, $var);
?>

And it worked perfectly, creating a file containing that string, named using that string.
0
 
LVL 25

Expert Comment

by:Squinky
ID: 22894251
Well there's a surprise - EE can't cope with UTF-8, which makes this kind of hard to show...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now