php copyfile – PHP Copy File from One Folder to Another Example

php copy files(php copyfile) – The copy() function copies a file. Use the PHP copy() file function to copy a file from a location to another.

php copyfile | php copy files

The copy() function overwrites the destination file if it exists. A copy of the source file to the destination file gets created.

bool copy( string $source, string $destination, resource $context )

How to copy a file from one directory to another using PHP?

/* Store the path of source file */
$filePath = 'media/mainexample.jpeg';
/* Store the path of destination file */
$finalstepinationFilePath = 'blog/mainexample.jpeg';
/* Copy File from media to blog folder */
if( !copy($filePath, $finalstepinationFilePath) ) {  
    echo "Sorry, File can't be copied!";  
else {  
    echo "Good Luck, File has been copied!";  

Don’t Miss : PHP Creating Dynamic Directory (Folder) Example

Also Read This πŸ‘‰   How to Get Last Executed Query in Codeigniter?

use the copy() function :

// Will copy blog/mainexample.php to bar/mainexample.php
// overwritting it if necessary
copy('blog/mainexample.php', 'bar/mainexample.php');

use the rename() function :

This rename() function will move the file not copy.

rename('blog/mainexample.php', 'bar/mainexample.php');

I hope you get an idea about php copy files.
I would like to have feedback on my
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.