tags:

views:

31

answers:

1

Hello,

I am trying to change a file exenstion, but whenever I do the file seems to corrupt.

$oldFileName = $targetDir . DIRECTORY_SEPARATOR . $fileName;
$newString = preg_replace('"\.tmp$"', '.jpg', $oldFileName);
rename($oldFileName, $newString);

The code works and changes the extension, but yet the file when downloaded, comes up as being corrupt.

The exension is .tmp and I am trying to change it to .jpg.

If I download the .tmp and manually change it to a .jpg it works, but not when the PHP does it.

Anyone know why this may be happening?

Thanks!

+3  A: 

try this

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak'; //new file with extension

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>
Starx
Thanks! The copy function seemed to solve the problem, where the rename just corrupted the file.
Kyle
If this did it, then +1, but rename() shouldn't break files?! That is really odd. Anyway....
Pekka