Home  >  Article  >  Backend Development  >  Detailed explanation of the use of php copy function (code example)

Detailed explanation of the use of php copy function (code example)

藏色散人
藏色散人Original
2019-04-22 13:31:174105browse

The copy() function in PHP is a built-in function used to copy the specified file. It copies the source file to the destination file, if the destination file already exists, it will be overwritten. The copy() function returns true on success and false on failure.

Syntax:

bool copy ( $source, $dest )

Parameters:

The copy() function in PHP accepts two parameters, namely the source parameter and target parameters.

$source: It specifies the path of the source file.

$dest: used to specify the path of the target file.

Return value:

Returns true when successful and false when failed.

Errors and Exceptions:

The copy() function in PHP does not work with remote files. It only works on files accessible to the server's file system.

If the target file already exists, it will be overwritten.

copy() function code usage example 1:

<?php 
  
// 复制gfg.txt到php.txt 
echo copy("gfg.txt", "php.txt"); 
  
?>

Output:

true

copy() function code usage example 2:

<?php 
  
$srcfile = &#39;/user01/Desktop/admin/gfg.txt&#39;; 
$destfile = &#39;user01/Desktop/admin/php.txt&#39;; 
  
if (!copy($srcfile, $destfilefile)) { 
    echo "文件不能被复制 \n"; 
} 
else { 
    echo "文件已被复制!"; 
} 
  
?>

Output:

文件已被复制!

Related recommendations: "PHP Tutorial"

The above is the detailed content of Detailed explanation of the use of php copy function (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn