在while循环中的函数调用中设置的标题只进行一次

I have a call from PHP to a function, but it stops after the first run.

while ($rowfight = $resultfights->fetch_assoc()) {
    // ..
    merge($file1, $file2, $filename_result, $rowfight['team1Name'], $rowfight['team2Name'], $rowfight['description'], '' );
    // ..
}

function merge($filename_x, $filename_y, $filename_result, $team1, $team2, $battle, $winner ) {
    // Do some stuff with image here
    // ..
    header('Content-Type: image/png');
    header('Content-Disposition: attachment; filename='.basename($filename_result));
    imagepng($image);
    // ..
}

It downloads the first .png image, and then stops.

What to do?
(I know it's something with the headers but I can't find out what)

Solution:

I used the Dropbox api, and then I have files on my hd, shortly after creation.

Dropbox