PHP如何使用copy()函数?示例

2021年3月19日13:59:25 发表评论 606 次浏览

PHP中的copy()函数是一个内置函数, 用于创建指定文件的副本。它将源文件复制到目标文件, 如果目标文件已存在, 它将被覆盖。 copy()函数成功返回true, 失败返回false。

语法如下:

bool copy ( $source, $dest )

参数:PHP中的copy()函数接受两个参数, 即源和目标。

  1. $来源:它指定源文件的路径。
  2. $ dest:用于指定目标文件的路径。

返回值:成功返回true, 失败返回false。

错误与异常:

  1. PHP中的copy()函数不适用于远程文件, 仅适用于服务器文件系统可访问的文件。
  2. 如果目标文件已经存在, 它将被覆盖。

例子:

Input : echo copy("gfg.txt", "lsbin.txt");
Output : true

Input : $srcfile = '/user01/Desktop/admin/gfg.txt';
        $destfile = 'user01/Desktop/admin/lsbin.txt';
        echo copy($srcfile, $destfilefile);
Output : true

下面的程序说明了copy()函数:

程序1:

<?php
  
// Copying gfg.txt to lsbin.txt
echo copy ( "gfg.txt" , "lsbin.txt" );
  
?>

输出如下:

true

程序2:

<?php
  
// Copying gfg.txt to lsbin.txt
$srcfile = '/user01/Desktop/admin/gfg.txt' ;
$destfile = 'user01/Desktop/admin/lsbin.txt' ;
  
if (! copy ( $srcfile , $destfilefile )) {
     echo "File cannot be copied! \n" ;
}
else {
     echo "File has been copied!" ;
}
  
?>

输出如下:

File has been copied!

参考:

http://php.net/manual/en/function.copy.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: