0

I use UTF-8 letters. It's works fine, but if first letter from file is "č", "ž", "š"... not work good. File name is: ča ča ča.doc I have this code:

$name1 = $_FILES["file"]["name"];  //here is ča ča ča.doc
$ext = pathinfo($name1, PATHINFO_EXTENSION); //here is doc
$name = basename($name1, $ext); //here is "a ča ča." missing first letter

This not work only if here is first leter "č,š,ž,đ..."

Nejc Galof
  • 2,538
  • 3
  • 31
  • 70

1 Answers1

1

PHP iconv()

Try to encode the $name1 yet again to a different encoding, Windows-1252.

//encode to windows-1252 to save to the filesystem
$name1 = $_FILES["file"]["name"];  //here is ča ča ča.doc
$encoded_filename = iconv("UTF-8","Windows-1252//IGNORE",$name1);

or CP858

$name1 = $_FILES["file"]["name"];  //here is ča ča ča.doc
$encoded_filename = iconv("UTF-8", "CP858//IGNORE", $name1)

After 30 min r&d on this, i found, you could try this:

$search = array('š','á','ž','í','ě','é','ř','ň','ý','č',' ');
$replace = array('s','a','z','i','e','e','r','n','y','c','-');

$code_encoding = "UTF-8"; // this is my guess, but put whatever is yours
$os_encoding = "CP-1250"; // this is my guess, but put whatever is yours

$name1 = $_FILES["file"]["name"];
$name1 = iconv($os_encoding , $code_encoding, $name1); // convert before replace
$name1 = str_replace($search, $replace, $name1); 

Reference: https://stackoverflow.com/a/1767011/2236219

Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93