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

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

藏色散人
Release: 2023-04-06 08:24:02
Original
4123 people have browsed it

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 )
Copy after login

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"); 
  
?>
Copy after login

Output:

true
Copy after login

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 "文件已被复制!"; 
} 
  
?>
Copy after login

Output:

文件已被复制!
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template