I've been looking for too much but I have not found the answer, the case is this:
json_encode()
. Here's how I do it:
header('Content-Type: application/json; charset=UTF-8');
function listar_directorios_ruta($repository) {
$response = array();
try
{
if (is_dir($repository))
{
$files = new FilesystemIterator($repository);
foreach ($files as $file) {
if ($file->getFilename()[0] === '.') continue;
array_push($response, array(
'name' => $file->getFilename()
));
}
}
}
catch(Exception $ex)
{
}
return $response;
}
$response['files'] = listar_directorios_ruta('directorio');
$responseJSON = json_encode($response);
file_put_contents('listado_archivos.json', $responseJSON);
In the directory I have a single file called ñ.txt, when creating the file if ñ.txt appears, up to here "everything is fine".
I do the following in Python (command line):
>>>test = open('listado_archivos.json').read()
>>>test
'n\xcc\x83.txt'
>>>
>>>print test
ñ.txt
>>>
>>>newFile = open(test, 'w')
>>>createFile.write('Contenido para el archivo')
>>>createFile.close()
I try to show the contents of the file:
# cat ñ.txt
cat: can't open 'ñ.txt': No such file or directory
But if I do the following with Python directly, if it works:
>>> test = 'ñ.txt'
>>> test
'\xc3\xb1.txt'
>>>
>>> print test
ñ.txt
>>>
>>> newFile = open(test, 'w')
>>> newFile.write('Contenido para el archivo')
>>> newFile.close()
>>> quit()
# cat ñ.txt
Contenido para el archivo
The coding of the ñ when it comes from PHP is different from that of Python.
How to make PHP correctly translate or interpret the ñ so that Python can create the file correctly?
Greetings ...