douqian5920 2013-06-24 10:23
浏览 29
已采纳

在PHP中重复文件夹名称后附加(1),(2)

Currently I have to create a folder similar in window file system.

For example: if folder is exist , add (1) at the end , if (1) is exist , add (2) at the end, the problem is , are there standard function in PHP or I have to write my own function?

if (isdir($folderPath)) {

        $folderPath =  ...special handling or any PHP standard function ..
        mkdir($folderPath);

}

From reading PHP manual , so far there is no similar parameter for me to handle this? Are there any function besides mkdir will work ?Thanks

  • 写回答

1条回答 默认 最新

  • doujian0265 2013-06-24 10:32
    关注

    I don't know of any function that does this. A bruteforece approach comes to mind:

    function make_filename($name) {
        if (file_exists($name)) {
            $i = 0;
            do {
                $testname = sprintf("%s (%d)", $name, ++$i);
            }
            while (file_exists($testname));
            return $testname;
        }
        return $name;
    }
    
    mkdir(make_filename("test"));
    

    I wouldn't recommend this if you expect a LOT of duplicated filenames.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度