drzfnr0275 2016-10-07 12:40
浏览 94
已采纳

创建前检查文件大小

I'm creating a system to generate sitemaps for a application I'm working on, and one of the requirements for generating sitemaps is that each sitemap shouldn't have a file size greater than 10mb (10,485,760 bytes) as can be seen here.

This is my code to create the sitemap:

$fp = fopen($this->getSitemapPath() . $filename, 'w');
fwrite($fp, $siteMap->__toString());
fclose($fp);

The method $siteMap->__toString() holds a maximum of 50000 links.

Is there a way to check the resulting file size before calling the function fwrite?

  • 写回答

1条回答 默认 最新

  • dt3358 2016-10-07 12:43
    关注

    Sure, you can use mb_strlen to get the length of your string before you write it out to a file.

    $contents = $siteMap->__toString();
    
    if(mb_strlen($contents, '8bit') >= 10485760) {
        echo "Oops, this is too big";
    } else {
        fwrite($fp, $contents);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作