doukao5073 2012-08-30 11:11
浏览 15
已采纳

PHP海量文件副本

If I have a file called file.html how do i make 10 clones of this file via PHP such that they are renamed file1....file10?

$filename = 'file.html'
$copyname = 'file2.html'
if ($file = @fopen($copyname, 'x')) {
    // We've successfully created a file, so it's ours.  We'll close
    // our handle.
    if (!@fclose($file)) {
        // There was some problem with our file handle.
        return false;
    }

    // Now we copy over the file we created.
    if (!@copy($filename, $copyname)) {
        // The copy failed, even though we own the file, so we'll clean
        // up by itrying to remove the file and report failure.
        unlink($copyname);
        return false;
    }

    return true;
}
  • 写回答

2条回答 默认 最新

  • 红酒泡绿茶 2012-08-30 11:13
    关注

    Small file approach: this allows you to do something with the contents of the file before saving it:

    $text = file_get_contents('file.html');
    for($i = 0; $i < 100; $i++) {
        file_put_contents('file'.$i.'.html', $data);
    }
    

    Bigger files approach: this does not allow you to access the contents of the file before saving it, it only tells the underlying OS to make the copy (equivalent to a linux bash command of cp file.html file1.html):

    for($i = 0; $i < 100; $i++) {
        copy('file.html', 'file'.$i.'.html');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?