2012-05-11 2 views
0

PHP 스크립트에서 작성한 텍스트 파일에 공백이있는 str_pad()를 사용하여 같은 공간을 잃지 않고 다운로드하려고합니다. ftp를 사용하여 다운로드 할 때 문제가 없으며 텍스트 파일의 모든 공백은 그대로 유지됩니다. 그러나이 다운로드 스크립트를 사용하여 사용자가 다운로드 할 수있게되면 공백 대신 정크 문자가 표시되고 데이터가 뒤섞여 나타납니다. 사용자가이 파일을 다운로드 할 수있는 다른 방법이 있으면 Pl 도움.PHP 스크립트에서 텍스트 파일을 강제로 다운로드

내 PHP 파일 다운로드 - download.php 다음 download.php에게 PHP 스크립트에 의해 생성된다

<html> 
<head> 
<title>Downloading Text file</title> 
</head><body> 
<br> 
<br> 
<a href='download.php?filename=Text1.txt'>Download print file </a><br> 
<br> 
<br> 
</body> 
</html> 

내 원래 Text1.txt 파일 출력을 호출

<?php 
function output_file($file, $name, $mime_type='') 
{ 

if(!is_readable($file)) die('File not found or inaccessible!'); 

$size = filesize($file); 
$name = rawurldecode($name); 

$known_mime_types=array(
"txt" => "text/plain", 
"html" => "text/html", 
"php" => "text/plain" 
); 

if($mime_type==''){ 
    $file_extension = strtolower(substr(strrchr($file,"."),1)); 
if(array_key_exists($file_extension, $known_mime_types)){ 
    $mime_type=$known_mime_types[$file_extension]; 
} else { 
    $mime_type="application/force-download"; 
}; 
}; 

@ob_end_clean(); 


if(isset($_SERVER['HTTP_RANGE'])) 
{ 
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2); 
list($range) = explode(",",$range,2); 
list($range, $range_end) = explode("-", $range); 
$range=intval($range); 
if(!$range_end) { 
    $range_end=$size-1; 
} else { 
    $range_end=intval($range_end); 
} 

$new_length = $range_end-$range+1; 
header("HTTP/1.1 206 Partial Content"); 
header("Content-Length: $new_length"); 
header("Content-Range: bytes $range-$range_end/$size"); 
} else { 
$new_length=$size; 
header("Content-Length: ".$size); 
} 

$chunksize = 1*(1024*1024); 
$bytes_send = 0; 
if ($file = fopen($file, 'r')) 
{ 
if(isset($_SERVER['HTTP_RANGE'])) 
fseek($file, $range); 

while(!feof($file) && 
    (!connection_aborted()) && 
    ($bytes_send<$new_length) 
    ) 
{ 
    $buffer = fread($file, $chunksize); 
    print($buffer); //echo($buffer); 
    flush(); 
    $bytes_send += strlen($buffer); 
} 
fclose($file); 
} else 
die('Error - can not open file.'); 

die(); 
} 

set_time_limit(0); 


$file_path=$_REQUEST['filename']; 

output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain'); 

?> 

HTML 파일 str_pad() 사용 :

 3M India Ltd             78788 
     No.2, Abbayappa Layout, 4th Main, NS Palaya,        
     Bannerghatta Road, Bangalore 560 076       11/05/2012 
                     10:10:57 

       500       322 
              AAACB5984DXM001   29400127541 
              KA-01-N-2345    29400127541 

답변

0

무엇이 발생하는지 확인하십시오. 선의

경우 ($ 파일 =하면 fopen ($ 파일, 'R'))

변화

하는 경우 (fopen의 ($ 파일, 'R'))

+0

위의 변경을 한 후에도 아무런 차이가 없지만 다른 해결책이 있습니까?() 무엇이 일어 났는지를 결정하기 위해 feof :
경고 : 죄송합니다 – user1114409

+0

,이 텍스트 파일의 출력 공급 인수는 라인 /home/public_html/svga/datagrid/examples/download.php에 유효한 스트림 자원 아닙니다 90< 경고 : feof() : supp – user1114409

+0

'echo file_get_contents ($ file);로 디버깅을 시도하십시오. exit;',이 코드를 함수의 시작 부분에 놓고 결과를 확인하십시오. 문제가 없으면 함수를 계속 진행하십시오. – adydy

관련 문제