Home » Php » How can I change a file's extension using PHP?

How can I change a file's extension using PHP?

Posted by: admin April 23, 2020 Leave a comment

Questions:

How can I change a file’s extension using PHP?

Ex: photo.jpg to photo.exe

How to&Answers:

In modern operating systems, filenames very well might contain periods long before the file extension, for instance:

my.file.name.jpg

PHP provides a way to find the filename without the extension that takes this into account, then just add the new extension:

function replace_extension($filename, $new_extension) {
    $info = pathinfo($filename);
    return $info['filename'] . '.' . $new_extension;
}

Answer:

substr_replace($file , 'png', strrpos($file , '.') +1)

Will change any extension to what you want. Replace png with what ever your desired extension would be.

Answer:

Replace extension, keep path information

function replace_extension($filename, $new_extension) {
    $info = pathinfo($filename);
    return ($info['dirname'] ? $info['dirname'] . DIRECTORY_SEPARATOR : '') 
        . $info['filename'] 
        . '.' 
        . $new_extension;
}

Answer:

http://www.php.net/rename

Answer:

Once you have the filename in a string, first use regex to replace the extension with an extension of your choice. Here’s a small function that’ll do that:

function replace_extension($filename, $new_extension) {
    return preg_replace('/\..+$/', '.' . $new_extension, $filename);
}

Then use the rename() function to rename the file with the new filename.

Answer:

Just replace it with regexp:

$filename = preg_replace('"\.bmp$"', '.jpg', $filename);

You can also extend this code to remove other image extensions, not just bmp:

$filename = preg_replace('"\.(bmp|gif)$"', '.jpg', $filename);

Answer:

Better way:

substr($filename, 0, -strlen(pathinfo($filename, PATHINFO_EXTENSION))).$new_extension

Changes made only on extension part. Leaves other info unchanged.

It’s safe.

Answer:

For regex fans,
modified version of Thanh Trung’s ‘preg_replace’ solution that will always contain the new extension (so that if you write a file conversion program, you won’t accidentally overwrite the source file with the result) would be:

preg_replace('/\.[^.]+$/', '.', $file) . $extension

Answer:

You could use basename():

$oldname = 'path/photo.jpg';
$newname = (dirname($oldname) ? dirname($oldname) . DIRECTORY_SEPARATOR  : '') . basename($oldname, 'jpg') . 'exe';

Or for all extensions:

$newname = (dirname($oldname) ? dirname($oldname) . DIRECTORY_SEPARATOR  : '') . basename($oldname, pathinfo($path, PATHINFO_EXTENSION)) . 'exe';

Finally use rename():

rename($oldname, $newname);

Answer:

Many good answers have been suggested. I thought it would be helpful to evaluate and compare their performance. Here are the results:

  • answer by Tony Maro (pathinfo) took 0.000031040740966797 seconds. Note: It has the drawback for not including full path.
  • answer by Matt (substr_replace) took 0.000010013580322266 seconds.
  • answer by Jeremy Ruten (preg_replace) took 0.00070095062255859 seconds.

Therefore, I would suggest substr_replace, since it’s simpler and faster than others.

Just as a note, There is the following solution too which took 0.000014066696166992 seconds. Still couldn’t beat substr_replace:

$parts = explode('.', $inpath);
$parts[count( $parts ) - 1] = 'exe';
$outpath = implode('.', $parts);

Answer:

I needed this to change all images extensions withing a gallery to lowercase. I ended up doing the following:

// Converts image file extensions to all lowercase
$currentdir = opendir($gallerydir);
while(false !== ($file = readdir($currentdir))) {
  if(strpos($file,'.JPG',1) || strpos($file,'.GIF',1) || strpos($file,'.PNG',1)) {
    $srcfile = "$gallerydir/$file";
    $filearray = explode(".",$file);
    $count = count($filearray);
    $pos = $count - 1;
    $filearray[$pos] = strtolower($filearray[$pos]);
    $file = implode(".",$filearray);
    $dstfile = "$gallerydir/$file";
    rename($srcfile,$dstfile);
  }
}

This worked for my purposes.