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:
- Autocomplete Search Box in PHP MySQL
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP
- Get Highest Value in Multidimensional Array PHP
- PHP Get Min or Minimum Value in Array