如何使用PHP更改文件的扩展名?

时间:2021-02-03 00:00:34

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

如何使用PHP更改文件的扩展名?

Ex: photo.jpg to photo.exe

例如:photo.jpg到photo.exe

10 个解决方案

#1


32  

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:

PHP提供了一种方法来查找没有考虑到这一点的扩展名的文件名,然后添加新的扩展名:

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

#2


19  

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.

会将任何扩展名更改为您想要的内容。将png替换为您想要的扩展名。

#3


9  

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;
}

#4


8  

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.

然后使用rename()函数使用新文件名重命名该文件。

#5


7  

http://www.php.net/rename

http://www.php.net/rename

#6


3  

Just replace it with regexp:

只需用regexp替换它:

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

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

您还可以扩展此代码以删除其他图像扩展,而不仅仅是bmp:

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

#7


3  

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.

它是安全的。

#8


1  

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:

对于正则表达式粉丝,Thanh Trung的'preg_replace'解决方案的修改版本将始终包含新扩展名(因此,如果您编写文件转换程序,则不会意外地用结果覆盖源文件)将是:

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

#9


1  

You could use basename():

你可以使用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():

rename($oldname, $newname);

#10


-2  

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.

这符合我的目的。

#1


32  

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:

PHP提供了一种方法来查找没有考虑到这一点的扩展名的文件名,然后添加新的扩展名:

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

#2


19  

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.

会将任何扩展名更改为您想要的内容。将png替换为您想要的扩展名。

#3


9  

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;
}

#4


8  

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.

然后使用rename()函数使用新文件名重命名该文件。

#5


7  

http://www.php.net/rename

http://www.php.net/rename

#6


3  

Just replace it with regexp:

只需用regexp替换它:

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

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

您还可以扩展此代码以删除其他图像扩展,而不仅仅是bmp:

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

#7


3  

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.

它是安全的。

#8


1  

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:

对于正则表达式粉丝,Thanh Trung的'preg_replace'解决方案的修改版本将始终包含新扩展名(因此,如果您编写文件转换程序,则不会意外地用结果覆盖源文件)将是:

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

#9


1  

You could use basename():

你可以使用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():

rename($oldname, $newname);

#10


-2  

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.

这符合我的目的。