PHP FTP_GET - Can't handle special characters

Discussion in 'PHP' started by koolsamule, Mar 15, 2010.

  1. #1
    Hi Chaps,

    I have a PHP FTP app where users can up/download files.

    Our company deals with foreign languages, so some of the files may have special characters, such as (German) umlauts. Uploading files with umlauts seems fine, but when I download a file, it goes from:
    to
    Is there any way to get round this, so it keeps the correct file name?

    Cheers
     
    koolsamule, Mar 15, 2010 IP
  2. shockworks

    shockworks Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    And if you see it via FTP client (like Total Commander), you see correct name?

    Is it problem PHP or FTP server?
     
    shockworks, Mar 15, 2010 IP
  3. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, thanks for the reply, jusrt checked with Total Commander and the file does appear as Häuse.pdf, so I'm guessing it's an FTP issue?
     
    koolsamule, Mar 15, 2010 IP
  4. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, thanks for the reply, I've just finished some testing and this is what I've found:

    Local: Häuse.pdf
    Uploaded via PHP
    FTP: Häuse.pdf
    Downloaded via PHP
    Local: Häuse.pdf

    Local: Häuse.pdf
    Uploaded manually
    FTP: Häuse.pdf
    Downloaded via PHP (name:H�use.pdf) - Failed
    Local: Häuse.pdf
    Uploaded via Total Commander
    FTP: Häuse.pdf
    Downloaded via Total Commander
    Local: Häuse.pdf

    So I'm guessing the PHP up/download functions have an effect on the filename, can this be solved?

    Cheers
     
    koolsamule, Mar 15, 2010 IP
  5. mattinblack

    mattinblack Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What you have is a character encoding issue. Various code schemes exist and are used which specify specially accented characters using 2 bytes. Unicode being one probable type. Unfortunately the net mostly expects file names to be in ascii - so your unicode or whatever will generate two ascii characters. This could happen in many places (including on your computer) What you can do is convert the filename back to unicode - if you need to by simple substitution using a unicode table (do a google search) and a short PHP function. I once had a particularly intransigent problem of this sort using japanese Kanji characters. My solution was to rename my file to an MD5 hash of its name and preserve the kanji code in a database, and re-applying the kanji code as a filename in all displays on the site. All files outgoing were named with a random 6 character string - the users did not seem to mind!
     
    mattinblack, Mar 15, 2010 IP
  6. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Cool, I've give it a try, thanks for the advice. Cheers
     
    koolsamule, Mar 15, 2010 IP
  7. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK, I've got the upload function working correctly now, using:
    $raw_file = $x
    function FixEncoding($raw_file){
      if(mb_detect_encoding($raw_file)=='UTF-8'){
        return $raw_file;
      }else{
        return utf8_decode($raw_file);
      }
    }
    $file = utf8_decode($raw_file);
    Code (markup):
    ...which works for a file a sinlge file ($x), passed from a form.
    I have a ftp_nlist array which I need to convert to UTF-8, so that the disply list and download links work correctly. . . .
    This is my table so far:
    <table border="0" cellpadding="0" cellspacing="0" id="tbldisplay">
    <tr>
    <th>Type</th>
    <th>Title</th>
    <th>Open</th>
    <th>Download</th>
    <th>Delete</th>
    </tr>
    <?php
    $ftp_nlist = ftp_nlist($conn, ".");
    //alphabetical sorting
    sort($ftp_nlist);
    foreach ($ftp_nlist as $value) {
    //1. Size is '-1' => directory
      if (ftp_size($conn, $value) == -1) {
    //output as [ directory ]?>
    <tr>
    <td><img src="images/folder.png" /></td>
    <td><?php echo $value;?></td>
    <td><a href="<?php $_SERVER['PHP_SELF']?>?dir=<?php echo $ftpPath.'/'.$value ;?>"><img src="images/open_folder.png" width="20" height="20" border="0" /></a></td>
    <td>---</td>
    <td><a href="ftp_dir_delete.php?dir=<?php echo $ftpPath;?>&amp;delete_dir=<?php echo $value ;?>"><img src="images/delete2.png" border="0"/></a></td>
    </tr>
    <?php  }
    }
    foreach ($ftp_nlist as $value) {
    //2. Size is not '-1' => file
      if (!(ftp_size($conn, $value) == -1)) {
    //output as file?>
    <tr>
    <td><img src="images/file.png" /></td>
    <td><?php echo $value;?></td>
    <td>---</td>
    <td><a href="ftp_file_download_select.php?dir=<?php echo $ftpPath;?>&amp;file=<?php echo $value ;?>"><img src="images/download.png" border="0"/></a></td>
    <td><a href="ftp_file_delete.php?dir=<?php echo $ftpPath;?>&amp;file=<?php echo $value ;?>"><img src="images/delete2.png" border="0"/></a></td>
    </tr>
    <?php  }
    }
    ?>
    </table>
    PHP:
    If someone can help me get the values of the ftp_nlist through the encoding function to display the correct filenames, that would be ultra-sweet
     
    koolsamule, Mar 15, 2010 IP
  8. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    OK, I've got the upload function working correctly now, using:
    $raw_file = $x
    function FixEncoding($raw_file){
      if(mb_detect_encoding($raw_file)=='UTF-8'){
        return $raw_file;
      }else{
        return utf8_decode($raw_file);
      }
    }
    $file = utf8_decode($raw_file);
    Code (markup):
    ...which works for a file a sinlge file ($x), passed from a form.
    I have a ftp_nlist array which I need to convert to UTF-8, so that the disply list and download links work correctly. . . .
    This is my table so far:
    <table border="0" cellpadding="0" cellspacing="0" id="tbldisplay">
    <tr>
    <th>Type</th>
    <th>Title</th>
    <th>Open</th>
    <th>Download</th>
    <th>Delete</th>
    </tr>
    <?php
    $ftp_nlist = ftp_nlist($conn, ".");
    //alphabetical sorting
    sort($ftp_nlist);
    foreach ($ftp_nlist as $value) {
    //1. Size is '-1' => directory
      if (ftp_size($conn, $value) == -1) {
    //output as [ directory ]?>
    <tr>
    <td><img src="images/folder.png" /></td>
    <td><?php echo $value;?></td>
    <td><a href="<?php $_SERVER['PHP_SELF']?>?dir=<?php echo $ftpPath.'/'.$value ;?>"><img src="images/open_folder.png" width="20" height="20" border="0" /></a></td>
    <td>---</td>
    <td><a href="ftp_dir_delete.php?dir=<?php echo $ftpPath;?>&amp;delete_dir=<?php echo $value ;?>"><img src="images/delete2.png" border="0"/></a></td>
    </tr>
    <?php  }
    }
    foreach ($ftp_nlist as $value) {
    //2. Size is not '-1' => file
      if (!(ftp_size($conn, $value) == -1)) {
    //output as file?>
    <tr>
    <td><img src="images/file.png" /></td>
    <td><?php echo $value;?></td>
    <td>---</td>
    <td><a href="ftp_file_download_select.php?dir=<?php echo $ftpPath;?>&amp;file=<?php echo $value ;?>"><img src="images/download.png" border="0"/></a></td>
    <td><a href="ftp_file_delete.php?dir=<?php echo $ftpPath;?>&amp;file=<?php echo $value ;?>"><img src="images/delete2.png" border="0"/></a></td>
    </tr>
    <?php  }
    }
    ?>
    </table>
    PHP:
    If someone can help me get the values of the ftp_nlist through the encoding function to display the correct filenames, that would be ultra-sweet
     
    koolsamule, Mar 15, 2010 IP
  9. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    sorted it, thanks for all your help guys!
     
    koolsamule, Mar 15, 2010 IP
  10. miller23

    miller23 Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    i also have often to do with encoding problems but your tip and source code works perfectly. thanks
     
    miller23, Mar 17, 2010 IP