How to Copy a file from one directory to another using PHP

In this tutorial, you will learn how to copy a file from one directory to another using PHP.

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

Here are examples:

  • Step 1: Create two directories
  • Step 2: Create a file to copy
  • Step 3: Copy the file
  • Step 4: Verify the copied file
  • Example: PHP code for copying a file from one directory to another
  • Example: php copy multiple files from one directory to another

Step 1: Create two directories

To copy a file from one directory to another, you first need to create two directories, the source directory from where the file needs to be copied, and the destination directory where the file will be copied.

Here is php built-in function of create directories:

mkdir('source_directory');
mkdir('destination_directory');

Step 2: Create a file to copy

Create a file in the source directory that you want to copy. You can create a file using the file_put_contents() function in PHP.

Here is php built-in function of copy file:

$file_content = "This is a sample file.";
$file_path = "source_directory/sample_file.txt";
file_put_contents($file_path, $file_content);

Step 3: Copy the file

After creating the source directory, destination directory, and file, you can now copy the file from the source directory to the destination directory using the copy() function in PHP.

Here is php built-in function of copy file:

$source_file_path = "source_directory/sample_file.txt";
$destination_file_path = "destination_directory/sample_file.txt";
copy($source_file_path, $destination_file_path);

Step 4: Verify the copied file

To verify that the file has been successfully copied to the destination directory, you can use the file_exists() function in PHP.

Example:

if(file_exists($destination_file_path)) {
  echo "File copied successfully.";
} else {
  echo "Failed to copy file.";
}

Example: PHP code for copying a file from one directory to another

With that in mind, let’s take a look at the PHP code for copying a file from one directory to another. You will be using the copy() function, which is built-in to PHP and allows you to copy a file from one location to another.

$source = '/path/to/source/file.ext';
$destination = '/path/to/destination/file.ext';

if (copy($source, $destination)) {
echo "File copied successfully.";
} else {
echo "Failed to copy file.";
}

Example: php copy multiple files from one directory to another

To copy multiple files from one directory to another using the copy() function in PHP, we need to loop through each file in the source directory and copy them one by one to the destination directory. Here’s an example code that demonstrates how to do this:

$source_dir = '/path/to/source/directory';
$destination_dir = '/path/to/destination/directory';

foreach (glob($source_dir . '/*') as $file) {
    $destination_file = $destination_dir . '/' . basename($file);
    if (copy($file, $destination_file)) {
        echo "File {$file} copied successfully to {$destination_file}.\n";
    } else {
        echo "Failed to copy file {$file}.\n";
    }
}

Conclusion

Copying files from one directory to another is an important task in web development. In this article, you have learned how to copy a file from one directory to another using PHP. You used the mkdir() function to create two directories, file_put_contents() function to create a file, and copy() function to copy the file from the source directory to the destination directory. You also used the file_exists() function to verify that the file has been successfully copied to the destination directory.

Recommended PHP Tutorial:

  1. Autocomplete Search Box in PHP MySQL
  2. Compare Arrays PHP | PHP array_diff() Function
  3. Get, Write, Read, Load, JSON File from Url PHP
  4. Functions: Remove First Character From String PHP
  5. Remove Specific/Special Characters From String In PHP
  6. How to Replace First and Last Character From String PHP
  7. Reverse String in PHP
  8. Array Push, POP PHP | PHP Array Tutorial
  9. PHP Search Multidimensional Array By key, value and return key
  10. json_encode()- Convert Array To JSON | Object To JSON PHP
  11. PHP remove duplicates from multidimensional array
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. Get Highest Value in Multidimensional Array PHP
  14. PHP Get Min or Minimum Value in Array

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *