0

i am trying to change download image name inside php header but it never works: I tried all these possibilities but non of them work. could any one tell me what i am doing wrong ?

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

php that download image:

<?

$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);
}
?>
2
  • 3
    You're opening the string with a single quote and closing it with a double quote... Commented Sep 7, 2013 at 18:25
  • There is an error in your code. It should be visible when you open the file. If it is not then you need to configure PHP to show errors. Commented Sep 7, 2013 at 18:26

1 Answer 1

1

You should write this line as follows:

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

Therefore, the string is closed and $imageNewName is concatenated to it.

Sign up to request clarification or add additional context in comments.

4 Comments

i tried this the image start downloading but with the name of script that i call is used for new image name! The name of imaged to be saved becomes script.php but i want it the name of image to be whatever is in $imageNewName!
Sorry, but I can't understand what you're trying to say.
thanks i had to pass $imageNewName variable to the function that is why your solution was not working!
Haha, that kind of stuff happens :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.