OK, the issue lies in this chunk of code, for each chunk it creates a new file so then the recombining later on doesn't have the right filename so yu end up with each chunk being a sequential file.
file_1.ext
file_2.ext etc.
I can't believe this is supposed to happen, have I missed somethign really obvious here. Some config thing I should have set or something?
If I coment out the code below then it works fine but of course has no protection against duplicate filenames.
// make sure filename is unique
if (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
$ext = strrpos($fileName, '.');
$fileName_a = substr($fileName, 0, $ext);
$fileName_b = substr($fileName, $ext);
$count = 1;
while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b))
$count++;
$fileName = $fileName_a . '_' . $count . $fileName_b;
}