Home > Article > Backend Development > Detailed explanation of the use of php copy function (code example)
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 = '/user01/Desktop/admin/gfg.txt'; $destfile = 'user01/Desktop/admin/php.txt'; 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!