2016-09-08 5 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"; 

두 번 인코딩

에 무슨 일이 일어 나는지 생각하지 않았다

는이

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

두 번째 스크립트가 작동하지 않습니다. 디코딩 부분에. 0kb의 파일을 반환합니다 – Joey

+0

''D : /timekeeping/logs/94-20160908.enc.dat "'호출 할 때 존재합니까? 하나가 작동하면 – cske

0

이 내 문제를 해결하려고합니다. 두 함수가 잘 어울리지 않아서 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

에 응답하는 수표를 추가하고 다른 하나는 문제가되지 않을 것입니다. – cske

관련 문제