2014-07-21 76 views
-2

我的代碼不工作始終。大多數時候它下載0 BYTE圖像。我可以通過這個代碼,下載特定的圖像,並且可以是該代碼被它的名字保存圖像的大小現金。如果我重命名圖像,它下載0 BYTE。文件下載下載0字節的文件,在PHP

$file_path= $full_path; 

$file = pathinfo($file_path); 

$base = $file['basename']; 

$dir = $file['dirname']; 

header('Content-Description: File Transfer'); 

header('Content-Type: application/octet-stream'); 
header("Content-Disposition: attachment; filename=".$base); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: no-cache'); 
header('Content-Length: ' . filesize($base)); 
ob_clean(); 
flush(); 

$path = $dir."/".$base; 

readfile($path); 

exit; 
+0

你檢查是否url_fopen已開啓您的服務器? – scragar

+0

不,我沒有檢查它。服務器是我的電腦,它是一個真正的IP。我如何檢查它? –

回答

0

此代碼解決我的問題....

function download($path) 
{ 
// if file is not readable or not exists 
if (!is_readable($path)) 
    die('File does not exist or it is not readable!'); 

// get file's pathinfo 
$pathinfo = pathinfo($path); 
// set file name 
$file_name = $pathinfo['basename']; 

    $mime = 'application/octet-stream'; 

// set headers 
header('Pragma: public'); 
header('Expires: -1'); 
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0'); 
header('Content-Transfer-Encoding: binary'); 
header("Content-Disposition: attachment; filename=\"$file_name\""); 
header('Content-Length: ' . filesize($path)); 
header("Content-Type: $mime"); 

// read file as chunk to reduce memory usages 
if ($fp = fopen($path, 'rb')) { 
    ob_end_clean(); 

    while(!feof($fp) and (connection_status()==0)) { 
     print(fread($fp, 8192)); 
     flush(); 
    } 

    @fclose($fp); 
    exit; 
} 

}

download($file_path);