PHP解码base64文件内容 [英] PHP Decode base64 file contents

查看:449
本文介绍了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";
?>

这篇关于PHP解码base64文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆