首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP批量文件复制

PHP批量文件复制
EN

Stack Overflow用户
提问于 2012-08-30 19:11:08
回答 2查看 164关注 0票数 0

如果我有一个名为file.html的文件,我如何通过PHP创建10个克隆,以便将它们重命名为file1....file10?

代码语言:javascript
运行
复制
$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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-30 19:13:31

小文件方法:这允许您在保存文件之前对文件内容执行一些操作:

代码语言:javascript
运行
复制
$text = file_get_contents('file.html');
for($i = 0; $i < 100; $i++) {
    file_put_contents('file'.$i.'.html', $data);
}

大文件方法:这不允许您在保存文件之前访问文件内容,它只告诉底层操作系统进行复制(相当于cp file.html file1.html的linux bash命令):

代码语言:javascript
运行
复制
for($i = 0; $i < 100; $i++) {
    copy('file.html', 'file'.$i.'.html');
}
票数 2
EN

Stack Overflow用户

发布于 2012-08-30 19:13:41

只需在循环中运行您的代码:

代码语言:javascript
运行
复制
$filename = 'file.html'
for($i=1; $i<=10; $i++) {
    $copyname = "file$i.html";
    copy($filename, $copyname);
}

您可以随意添加错误检查和处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12195564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档