Copies a file from one path to another.
What Is copy() in PHP?
Copies a file from one path to another.
Its syntax is:
copy(string $from, string $to, ?resource $context = null): bool
Parameters:
$from— Accepted as string; required argument.$to— Accepted as string; required argument.$context— Accepted as ?resource; optional default null.
Return type: bool. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
Availability: Filesystem access required.
Example 1: Basic Usage
<?php
$sourceFile = tempnam(sys_get_temp_dir(), "src_"); $targetFile = $sourceFile . ".copy"; file_put_contents($sourceFile,"hello"); var_dump(copy($sourceFile,$targetFile)); unlink($sourceFile); unlink($targetFile);
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
Example 2: A Different Input
<?php
$sourceFile = tempnam(sys_get_temp_dir(), "src_"); $targetFile = $sourceFile . ".copy"; file_put_contents($sourceFile,"data"); copy($sourceFile,$targetFile); echo file_get_contents($targetFile); unlink($sourceFile); unlink($targetFile);
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
Practical Example: Applying It in a Program
<?php
$sourceFile = tempnam(sys_get_temp_dir(), "src_"); $backupFile = $sourceFile . ".bak"; file_put_contents($sourceFile,"report"); if (copy($sourceFile,$backupFile)) { echo "Backup created"; } unlink($sourceFile); if (is_file($backupFile)) { unlink($backupFile); }
Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
Important Behavior and Edge Cases
- Key behavior: copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
- Environment: Filesystem access required.
Comparison With Related Functions
| Function | Purpose |
|---|---|
copy() | Copies a file from one path to another. |
rename() | rename() moves or renames a file; copy() preserves the original while creating a destination. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: copy() can overwrite a destination file and does not validate file contents | copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first. |
Key Takeaways
- Copies a file from one path to another.
- copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.
- Filesystem access required.
Summary: copy() can overwrite a destination file and does not validate file contents. Check file paths and permissions first.