The copy() function in PHP is an inbuilt function which is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets 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 which are source and destination.
- $source: It specifies the path to the source file.
- $dest: It is used to specify the path to the destination file.
Return Value: It returns true on success and false on failure.
Errors And Exception:
- The copy() function in PHP doesn’t works for remote files.It only works on files which are accessible by the server’s filesystem.
- If the destination file already exists, it gets overwritten.
Examples:
Input : echo copy("gfg.txt", "geeksforgeeks.txt");
Output : true
Input : $srcfile = '/user01/Desktop/admin/gfg.txt';
$destfile = 'user01/Desktop/admin/geeksforgeeks.txt';
echo copy($srcfile, $destfilefile);
Output : true
Below programs illustrate the copy() function:
Program 1:
<?php
echo copy ( "gfg.txt" , "geeksforgeeks.txt" );
?>
|
Output:
true
Program 2:
<?php
$srcfile = '/user01/Desktop/admin/gfg.txt' ;
$destfile = 'user01/Desktop/admin/geeksforgeeks.txt' ;
if (! copy ( $srcfile , $destfilefile )) {
echo "File cannot be copied! \n" ;
}
else {
echo "File has been copied!" ;
}
?>
|
Output:
File has been copied!
Reference:
http://php.net/manual/en/function.copy.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Jun, 2023
Like Article
Save Article