Saturday, 28 March 2015

PHP download FTP file and convert from ANSI to UTF-8

So, I have a file, and I'm using Joomla with CSVI to import the file from an ftp server into the database. The file is automatically made by a third party, and it is in ANSI format.

This file contains '£' which CSVI and Joomla do not like when it is in ANSI format. I need to convert it to UTF-8 in order to import such characters. Of course to convert, I will need to store locally as well. Here's my research:

First thing is to get the file and store on server:

Found the PHP function ftp_get

Once on the server, I found two ways to convert:
mb_convert_encoding()
iconv()

this is quite helpful for getting and writing the file:
$file = file_get_contents('file.php');
$file = iconv('greek-charset','UTF-8', $file);
file_put_contents('file.php', $file);
//ta-da!

No comments:

Post a Comment