You are here: Re: Chinese filenames « PHP Programming Language « IT news, forums, messages
Re: Chinese filenames

Posted by Umberto Salsi on 01/29/07 14:38

"Taras_96" <taras.di@gmail.com> wrote:

> Does PHP even support opening chinese filenames?

I don't know how exactly fopen() handles strings containing characters
other that ASCII, but it is better to not rely on the underlying file
system for portability reasons. Always use simple ASCII characters. For
files uploaded via HTTP, store their original name in a DB table properly
created to support UTF-8.

> Secondly, I'm having some trouble accessing filenames with chinese
> characters that have been uploaded via HTTP. I'm using PHP 5 and
> Apache 2.2. When I
> attempt to upload a file with chinese filenames, the file name gets
> mutated into dashes, pretty much matching the behaviour described at
> 'http://gallery.menalto.com/node/57709'. However, I need the original
> filename (to store in a DB). The post on the manual website by
> kweechang at yahoo dot com at http://au3.php.net/manual/en/
> features.file-upload.php describes using javascript to set a hidden
> field. This would work fine but for now I'm trying not to resort to
> javascript on my webpage. Does anyone know how the original filename
> can be retrieved without using javascript? Maybe there is a setting in
> Apache?

0. Ensure your PHP script be properly UTF-8 encoded. This is important
if it contains some literal string.

1. Ensure the page containing the FORM be UTF-8 encoded. For example:

<?php
header("Content-Type: text/html; charset=UTF-8");
?>
<html><body>
<FORM method=post
enctype="multipart/form-data"
accept="image/gif,image/png,image/jpeg,image/pjpeg"
action="the-program-receiving-the-data.php">
Photo (accepted GIF, PNG o JPEG, max 500 KB):
<INPUT type=hidden name=MAX_FILE_SIZE value=512000>
<INPUT type=file name=PHOTO size=50 maxlength=512000><p>
<INPUT type=submit name=save_button value=Save>
</FORM>
</body></html>

The file name returned from the client will have the same encoding of the
page containing the FORM, that is UTF-8.

2. The name of the file can be acquired as a UTF-8 string:

$field = "PHOTO";

if( ! isset($_FILES) || ! isset($_FILES[$field]) )
die("No file uploaded.");

$error = (int) $_FILES[$field]['error'];
$name = (string) $_FILES[$field]['name'];
$type = (string) $_FILES[$field]['type'];
$size = (int) $_FILES[$field]['size'];
$tmp_name = (string) $_FILES[$field]['tmp_name'];

if( $error !== 0 )
die("Upload error code $error.");

# Here: check actual UTF-8 encoding and max length for $name.
# Here: check actual MIME $type against the allowed MIME types.
# Here: check actual $size limit.
# Here: store the file $tmp_name in a proper place with a proper name.

3. Ensure the DB you are using really has support for UTF-8. For example,
retrieve the file name once saved and compare it with the string just
acquired from the POST.

4. Don't try to save the file under the underlying file system using the
name provided by the client, always use some other identifier, for example
the primary key assigned by the DB (typically a simple number). Since the
file name now contain only simple ASCII chars, fopen() should not give
problems.

Best regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация