copy()函数介绍
copy函数用于拷贝或复制文件。成功时返回 TRUE , 或者在失败时返回 FALSE 。
语法:
bool copy(string $source,string $dest[,resource $context])
将文件从 source 拷贝到 dest。如果要移动文件的话,请使用rename()函数。
参数:
source:源文件路径。
dest:目标路径。如果 dest 是一个 URL,则如果封装协议不支持覆盖已有的文件时拷贝操作会失败。
copy()函数实例
使用copy()函数复制拷贝文件
$filename = 'data.txt';
$filename2 = $filename . '.old';
$result = copy($filename, $filename2);
if ($result) {
/* http://www.manongjc.com/article/1301.html */
print "$filename 已经复制拷贝到 $filename2.\n";
} else {
print "Error: couldn't copy $filename to $filename2!\n";
}
?>
再看一个实例:
function move($source, $dest) {
if(!copy($source, $dest)) return false;
if(!unlink($source)) return false;
return true;
}
if(!move("data.txt", "/tmp/tmpdir/newfile.txt")) {
echo "Error! Couldn't move the file!
";
}
?>