2016-09-08 17 views
0

ファイルの内容を取得してbase64を使用してエンコードするスクリプトがあります。このスクリプトは正常に動作します。PHPのデコード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"; 
?> 

しかし、動作しません。

+0

どちらも同じページにあります。 – Noman

+0

何が問題なのですか?入力と出力の例を示し、実際に得られたものと期待したものとの違いを説明してください。 –

+0

いいえ、同じスクリプトにはありません。 – Joey

答えて

0

あなたは、私は二重のエンコードに何が起こるかを見るために

<?php 

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

を検討し、あなたの入力&出力のbecouseダブルエンコードまたはダブルデコードが同じファイルあると仮定し、「動作しない」の詳細を提供しなかった

この

<?php 
$sourcePath="D:/timekeeping/logs/94-20160908.dec.dat"; 
$targetPath="D:/timekeeping/logs/94-20160908.enc.dat"; 
if (!file_exsits($sourcePath) || !file_readable($sourcePath)) { 
    die('missing source'); 
} 
$source = file_get_contents($sourcePath); 
if (empty($source)) { 
    die('source file is empty'); 
} 
$data = base64_encode($source); 
$file = fopen($targetPath, 'w'); 
fwrite($file, $data); 
fclose($file); 
echo "file contents has been encoded"; 
?> 

<?php 
$sourcePath="D:/timekeeping/logs/94-20160908.enc.dat"; 
$targetPath="D:/timekeeping/logs/94-20160908.dec.dat"; 
if (!file_exsits($sourcePath) || !file_readable($sourcePath)) { 
    die('missing source'); 
} 
$source = file_get_contents($sourcePath); 
if (empty($source)) { 
    die('source file is empty'); 
} 
$data = base64_decode($source); 
$file = fopen($targetPath, 'w'); 
fwrite($file, $data); 
fclose($file); 
echo "file contents has been decoded"; 
?> 
+0

私の2番目のスクリプトは動作しません。デコード部に出力する。それは0kbのファイルを返します – Joey

+0

'' D:/timekeeping/logs/94-20160908.enc.dat ''は起動時に存在しますか? 、私は – cske

0

これは私の問題を修正してみてください。 2つの関数がうまくいっていないので、file_get_contentsを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"; 
?> 
+0

に答えるための小切手を追加します、もしもう一方がうまくいくなら、もう1つは問題ではないでしょう – cske

関連する問題