1

I have an xml file with dynamic contents like this.

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <tracks>
    <idx>0</idx>
    <path>/mnt/HD/HD_a2/mymusic/Worth It - Fifth Harmony - ft. Kid Ink.mp3</path>
  </tracks>
  <tracks>
    <idx>1</idx>
    <path>/mnt/HD/HD_a2/mymusic/Flashlight - Jessie J.mp3</path>
  </tracks>
  <tracks>
    <idx>2</idx>
    <path>/mnt/HD/HD_a2/mymusic/価値がある.mp3</path>
  </tracks>
</config>

And also a php file which creates another xml file according to data fetch on first xml. Here's a piece of code in generating new xml:

$item = $xml->addChild('tracks');
$item->addChild('artist', base64_encode($mp3Artist));
$item->addChild('song_name', base64_encode($mp3Title));
$item->addChild('song_path', base64_encode($file)); 
$item->addChild('album', base64_encode($mp3Album));

However, when I encode /mnt/HD/HD_a2/mymusic/価値がある.mp3

the output is L21udC9IRC9IRF9hMi9teW11c2ljL+S+oeWApOOBjOOBguOCiy5tcDM= which is when I decode it again, the value becomes /mnt/HD/HD_a2/mymusic/価値ãŒã‚ã‚‹.mp3 but when I decode it on this site https://www.base64decode.org/ with utf-8 encoding, the output is the same as the first one which is /mnt/HD/HD_a2/mymusic/価値がある.mp3

So my question is, how can I achieved utf-8 encoding on php's base64_encode to output the exactly the same as on www.base64decode.org? Any help will be much appreciated.

LeViNcE
  • 304
  • 1
  • 6
  • 15

1 Answers1

0

You don't need to do anything just use base64_decode :

$encoded = base64_encode("/mnt/HD/HD_a2/mymusic/価値がある.mp3");
echo "\xEF\xBB\xBF" ;//Optionally add this line if you want to force a program to read as utf-8
echo $encoded." <br />";
$decoded = base64_decode($encoded);
echo $decoded;

And optionally add a echo "\xEF\xBB\xBF" ; on the first output line if you want to force a file to be interpreted as utf-8 by any program

Gabriel Rodriguez
  • 1,163
  • 10
  • 23
  • I examine my codes again and I see where the error occurs, the encoded path is used to url but when the browser detects `+` on string, it converts it to `%20` in my code, this `L21udC9IRC9IRF9hMi9teW11c2ljL+S+oeWApOOBjOOBguOCiy5tcDM=` will become `L21udC9IRC9IRF9hMi9teW11c2ljL%20S%20oeWApOOBjOOBguOCiy5tcDM=` and the result is 404 not found – LeViNcE Oct 02 '15 at 02:20
  • I just found the solution to my problem above, thanks to this! http://stackoverflow.com/questions/1374753/passing-base64-encoded-strings-in-url – LeViNcE Oct 02 '15 at 02:39