【问题标题】:Naming files without extensions in PHP, how to add an extension to a file? [closed]在PHP中命名没有扩展名的文件,如何为文件添加扩展名? [关闭]
【发布时间】:2013-11-03 16:06:47
【问题描述】:

我在PHP 工作,我有一些没有扩展名的文件。

例子:

MYfolder
    firstfile
    secondfile
    otherfile

等等。

我想列出文件,并给它所有“.xml”扩展名。我如何在 PHP 中做到这一点?

【问题讨论】:

  • 显示你尝试过的代码...

标签: php xml file rename


【解决方案1】:

你可以试试这样的:

<?php
    if ($handle = opendir('/MYfolder')) {
        while (false !== ($fileName = readdir($handle))) {
            rename($fileName, $filename.'xml');
        }
        closedir($handle);
    }
?>

【讨论】:

  • false !== ($fileName = readdir($handle)) 这很有趣。我一直在使用strpos($fileName, '.')!==0,我猜它的作用是一样的。
  • 我明白了:)。我希望你得到输出。
  • 我之前也试过这个。我又试了一次..我知道代码是正确的,但它没有这样做..无论如何感谢您的支持:)
【解决方案2】:

在 Linux/Unix 机器上:

exec('for F in your/directory/*; do mv $F{,.xml}; done');

【讨论】:

    猜你喜欢
    • 2012-12-24
    • 1970-01-01
    • 2019-07-07
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2016-09-06
    相关资源
    最近更新 更多