PHP解码base64文件内容
问题描述:
我有一个脚本,该脚本获取文件的内容并使用base64对其进行编码.该脚本可以正常工作:
I have a script that gets the contents of a file and encodes it using base64. This script works fine:
<?php
$targetPath="D:/timekeeping/logs/94-20160908.dat";
$data = base64_encode(file_get_contents($targetPath));
$file = fopen($targetPath, 'w');
fwrite($file, $data);
fclose($file);
echo "file contents has been encoded";
?>
现在,我想将内容解码回其原始值.我试过了:
Now, I want to decode the contents back to its original value. I tried:
<?php
$targetPath="D:/timekeeping/logs/94-20160908.dat";
$data = base64_decode(file_get_contents($targetPath));
$file = fopen($targetPath, 'w');
fwrite($file, $data);
fclose($file);
echo "file contents has been decoded";
?>
但是不起作用.
答
这解决了我的问题.这两个函数不能很好地结合在一起,所以我将file_get_contents与base64_decode分开了
This fixed my problem. The two function does not go well together so I separated the file_get_contents from base64_decode
<?php
$targetPath="D:/timekeeping/logs/94-20160908.dat";
$data = file_get_contents($targetPath);
$content= base64_decode($data);
$file = fopen($targetPath, 'w');
fwrite($file, $content);
fclose($file);
echo "done";
?>