How to Move And Copy Files From One Folder to Another Folder in PHP?

03-Apr-2023

.

Admin

How to Move And Copy Files From One Folder to Another Folder in PHP?

Hi Dev,

This tutorial is focused on how to move and copy files from one folder to another folder in PHP. you'll learn how to move a file into a different folder or directory on the server using PHP. We will use how to copy files into a different folder on the server using PHP. if you have a question about PHP move and copy all files from one folder or directory to another folder then I will give a simple example with a solution.

Now, let's see the article on PHP move and copy all files from one folder or directory to another folder. it's a simple example of how to move a file into a different folder or directory on the server using PHP. you can understand the concept of how to copy files into a different folder on the server using PHP. if you have a question about how to copy and move files into a different folder on the server using PHP then I will give a simple example with a solution.

Example 1: PHP Move File From One Folder to Another


<?php

//Store the path of the source file

$filePath = 'images/img.jpeg';

//Store the path of the destination file

$destinationFilePath = 'copyImages/img.jpeg';

//Move File from images to copy images folder

if(!rename($filePath, $destinationFilePath)) {

echo "File can't be moved!";

}else {

echo "File has been moved!";

}

?>

Example 2: PHP Copy File From One Folder to Another

<?php

//Store the path of the source file

$filePath = 'images/img.jpeg';

//Store the path of the destination file

$destinationFilePath = 'img/test.jpeg';

//Copy File from images to copy images folder

if(!copy($filePath, $destinationFilePath)) {

echo "File can't be copied!";

}else {

echo "File has been copied!";

}

?>

I hope it could help you...

#PHP