I have this code:
$codif ="file --mime-encoding -b inputfile.txt";
$codif = shell_exec($codif);
$encode = "iconv --from-code=$codif --to-code=UTF-8 --output=tempfile.txt inputfile.txt";
I have tried
shell_exec($encode); //1
exec($encode); //2
system($encode); //3
And I have this just to see what's the generated command:
echo $encode;
Which outputs this:
iconv --from-code=iso-8859-1 --to-code=UTF-8 --output=tempfile.txt inputfile.txt
The problem is that with either of the three forms of executing the command I get next error:
sh: 2: --to-code=UTF-8: not found
While executing the output command at the shell works perfectly.
I have also tried changing the --to-code=UTF-8
to -t UTF-8
with the same results.
So the questions are, what am I doing wrong and how to fix it? thanks!
You have a new line at the end of the $codif variable, which messes things up. That needs to be stripped out some way. You can try this for example:
$codif ="file --mime-encoding -b inputfile.txt|tr -d '\n'";
$codif = shell_exec($codif);
Piping output of file
command to tr -d "\n"
will remove the new line. You can of course remove that in the php code aswell.