2012-05-11 37 views
0

str_pad()を使用してphpスクリプトで作成されたテキストファイルが空白になっています。 ftpを使ってダウンロードしても、問題はありません。テキストファイルのすべてのスペースはそのままです。 しかし、このダウンロードスクリプトを使用してユーザーがダウンロードできるようにすると、空白ではなく迷惑な文字が表示され、データがスクランブルされます。ユーザーがこのファイルをダウンロードする他の方法がある場合は、Plヘルプ。PHPを強制的にダウンロードするPHPスクリプト

ダウンロードする私のPHPファイル - download.php:

<?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'); 

?> 

PHPスクリプトによって作成された私の元Text1.txtファイル出力

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

download.phpを呼び出す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

これを試してみてください。 ( 'R' のfopen($ファイル))IF($ファイル=ののfopen($ファイル、R ''))であればライン

ため

変更

+0

あなたが上記の変更を行った後に違いはありませんが、他に解決策がありますか?
警告: – user1114409

+0

申し訳ありませんが、これはテキストファイルの出力ではfeof():与えられた引数は、ライン/home/public_html/svga/datagrid/examples/download.phpに有効なストリームリソースではありません90< 警告:feof():supp – user1114409

+0

'echo file_get_contents($ file);でデバッグを試してください。 exit; '、このコードを関数の先頭に置き、問題がなければ結果をチェックし、関数を続けます。 – adydy

関連する問題