0

我正在尝试更改 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);
}
?>
4

1 回答 1

1

您应该按如下方式编写此行:

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

因此,该字符串被关闭并且 $imageNewName 被连接到它。

于 2013-09-07T18:25:57.177 回答