PHP更改文件扩展名 [英] PHP change file extension

查看:81
本文介绍了PHP更改文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改文件的扩展名,但是每当文件执行时,文件似乎已损坏.

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.

扩展名为.tmp,我正尝试将其更改为.jpg.

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

如果我下载了.tmp并将其手动更改为.jpg,则它可以工作,但是在PHP运作时却不能.

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?

谢谢!

推荐答案

尝试一下

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

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

这篇关于PHP更改文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆