0

I am saving the content and title from a WYSIWYG widget (screenshot below) in an SQL database. I am also creating a new file every time (php). My problem is that the new file has a weird name because of the unicode. I am trying to make it work with the unicoded version of the name.

This is my WYSIWYG form :

enter image description here

This is the part that saves the content and title in the database without any unicode issues because the UTF8 was set for the database fields :

$sql = "INSERT INTO `articles`(`Content`, `Article_Name`) VALUE ('$_POST[htmlcontent]','$_POST[articlename]')";

And this is the part that creates the file but the name has an issue with the unicode :

$myfile = fopen("$articlename.php", "w") or die("Unable to open file!");

I first tried this:

$unicoded = iconv('windows-1250', 'utf-8', file_get_contents($articlename)); 
$myfile = fopen("$unicoded.php", "w") or die("Unable to open file!");

and then this:

    function utf8_fopen_read($articlename) { 
    $fc = iconv('windows-1250', 'utf-8', $articlename); 
    $handle=fopen("$fc", "w"); 
    fwrite($handle, $fc); 
    fseek($handle, 0); 
    return $handle;
} 

    $myfile = utf8_fopen_read("$articlename.php"); 

They all return a file named "φφφĎâ€.php" although it is supposed to be named "φφφ.php"

UPD:

I tried this and the console returns an empty string :

$fc = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $articlename); 
echo "<script>console.log('$fc')</script>";

I also tried this :

$name = urlencode($_POST["articlename"]) . ".php";
echo "<script>console.log('$name')</script>";
$myfile = fopen($name, "w") or die("Unable to open file!"); 

but the name is now something like this '%CF%86.php'

Datacrawler
  • 2,780
  • 8
  • 46
  • 100
  • 2
    what happens when you do this ? what do you expect to happen and what happen instead ? – Rabin Jun 18 '17 at 15:23
  • @Rabin I changed the question a bit. If the file is named φφφ, it should return this name. Alternatively, I will try to convert it into latin with a function. – Datacrawler Jun 18 '17 at 19:34
  • 1
    Which OS? Which version? Since PHP 7.1.0, PHP is able to handle Unicode filenames (it just needs to have default_charset = UTF-8). If this filename comes from the database, make sure you request the results in UTF-8 as well. (note that your SQL query has 2 SQL injections) – julp Jun 18 '17 at 19:47
  • @julp $name = urlencode($_POST["articlename"]) . ".php"; echo ""; echo ""; did not work either. – Datacrawler Jun 18 '17 at 20:00
  • @CBroethe answers are not clear in the other questions. None of the solutions worked for me.... – Datacrawler Jun 18 '17 at 20:03
  • @julp Apache/2.4.23 (Win64) PHP/5.6.25 – Datacrawler Jun 18 '17 at 20:05
  • @ApoloRadomer yea it will simply not work with that version – Evert Jun 18 '17 at 20:09
  • 1
    @Apolo Radomer Can you upgrade to PHP 7.1? If not, you cannot use a Unicode filename on Windows without using com or wfio extensions or (try to) convert your filename from UTF-8 to your local codepage (`$handle = fopen(iconv('utf-8', 'CP1250//TRANSLIT', $articlename), 'w')`? - same to read it) – julp Jun 18 '17 at 20:15

0 Answers0