Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP解码base64文件内容_Php_Base64 - Fatal编程技术网

PHP解码base64文件内容

PHP解码base64文件内容,php,base64,Php,Base64,我有一个脚本,它获取文件的内容并使用base64对其进行编码。此脚本工作正常: <?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 b

我有一个脚本,它获取文件的内容并使用base64对其进行编码。此脚本工作正常:

<?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";
?>

现在,我想把内容解码回原来的值。我试过:

<?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";
?>


但不起作用。

您没有提供“不起作用”的详细信息。我假设您进行双重编码或双重解码,因为输入和输出是同一个文件,请考虑

<?php

$in = 'teszt';
$enc = base64_encode($in);
echo $enc,"\n";
$enc2 = base64_encode($enc);
echo $enc2,"\n";
$enc3 = base64_encode($enc2);
echo $enc3,"\n";

这解决了我的问题。这两个函数不能很好地结合在一起,因此我将文件\u get\u内容与base64\u 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";
?>


两者都写在同一页上?什么不起作用?向我们展示输入和输出示例,并解释实际得到的和预期得到的之间的差异。不,它们不在同一个脚本上。我的第二个脚本不起作用。在解码部分。它返回0 KB的文件
“D:/timekeeping/logs/94-20160908.enc.dat”
调用时是否存在,我会给答案加上检查如果一个有效,另一个也会,这可能不是问题
    <?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";
?>