2013-09-07 3 views
0

PHP 헤더 안에 다운로드 이미지 이름을 변경하려하지만 작동하지 않습니다 : 나는 이러한 모든 가능성을 시도했지만 그 중 일부는 작동하지 않습니다. 어느 사람이 내가 뭘 잘못하고 있는지 말해 줄 수 있니? 이미지를 다운로드문서 헤더에서 PHP 변수를 사용하는 방법?

header('Content-disposition: attachment; filename=$imageNewName"); 

PHP는 다음과 같이

<? 

$file = $_GET['url']; 

$imageNewName = "david_".basename($file); 

//echo $imageNewName; 

//download image 

download($file,2000); 

/* 
Set Headers 
Get total size of file 
Then loop through the total size incrementing a chunck size 
*/ 

function download($file,$chunks){ 
    set_time_limit(0); 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    //header('Content-disposition: attachment; filename='.basename($file)); 
     header('Content-disposition: attachment; filename=$imageNewName"); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Expires: 0'); 
    header('Pragma: public'); 
    $size = get_size($file); 
    header('Content-Length: '.$size); 

    $i = 0; 
    while($i<=$size){ 
     //Output the chunk 
     get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks)); 
     $i = ($i+$chunks); 
    }  
} 

//Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk 
function chunk($ch, $str) { 
    print($str); 
    return strlen($str); 
} 

//Function to get a range of bytes from the remote file 
function get_chunk($file,$start,$end){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $file); 
    curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk'); 
    $result = curl_exec($ch); 
    curl_close($ch); 
} 

//Get total size of file 
function get_size($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_NOBODY, true); 
    curl_exec($ch); 
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); 
    return intval($size); 
} 
?> 
+3

작은 따옴표로 문자열을 열고 큰 따옴표로 닫으십시오 ... –

+0

코드에 오류가 있습니다. 파일을 열 때 표시되어야합니다. 그렇지 않은 경우 오류를 표시하도록 PHP를 구성해야합니다. –

답변

1

당신이 줄을 작성해야이 :

header('Content-disposition: attachment; filename='.$imageNewName); 

따라서, 문자열이 닫히고 $ imageNewName 그것에 연결됩니다.

+0

나는 이미지 다운로드를 시도했지만, 내가 부른 스크립트 이름이 새로운 이미지 이름으로 사용되었다! 저장할 영상의 이름은 script.php가되지만 $ imageNewName에있는 이름이 될 이미지의 이름이 필요합니다! – user1788736

+0

미안하지만, 당신이하려는 말을 이해할 수 없습니다. – Sikian

+0

덕분에 나는 당신의 솔루션이 작동하지 않는 이유 인 $ imageNewName 변수를 함수에 전달해야했습니다! – user1788736

관련 문제