SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with fread() and unicode characters

    Hi,

    I have a script that reads one file from a server and saves it on another. It works fine except when it is trying to process unicode characters. For example, it would take a file that has the contest of:

    Andrés

    and copy it over as:

    Andrés

    Here is the code:
    PHP Code:
        foreach ($files as $file) {
            
    $url_file fopen($file,'rb');
            if (
    $url_file) {
                
    $newfile fopen($newfile_name[$c],'wb');
                while (!
    feof($url_file)) {
                  
    $chunk fread($url_file,1024);
                  
    fwrite($newfile,$chunk);
                }
        } 
    Can anyone explain what is going on and how to fix it?

    Thanks!

  2. #2
    SitePoint Addict fesh's Avatar
    Join Date
    Jan 2005
    Location
    Jackson, New Jersey
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe try reading in non-binary mode so that it reads

    PHP Code:
    foreach ($files as $file) {
            
    $url_file fopen($file,'r');
            if (
    $url_file) {
                
    $newfile fopen($newfile_name[$c],'w');
                while (!
    feof($url_file)) {
                  
    $chunk fread($url_file,1024);
                  
    fwrite($newfile,$chunk);
                }
        } 
    Fesh
    imagine what it's going to be like

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but that didn't work.

    Other suggestions?

  4. #4
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try not reading it in chunks, but as a whole!

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed the script to read 8024 bytes, which for my purposes, would ready the whole file at once, but that didn't work either.

    As it turns out, I decided to take a look at the file that was being copied over and the characters in that file are garbled as well. So this has nothing to do with my script. (yay!)

    But something is still going on that I will have to figure out as to why the characters are being garbled.

    Thanks for all the help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •