- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP copy() | Copy Content of One File into Another
The PHP copy() function is used when we need to copy the content of one file to another. For example:
<?php copy("codes.txt", "cracker.txt"); ?>
The content of codes.txt file gets copied into cracker.txt file. If cracker.txt file already contains some content, then it will get overwritten.
Note - The copy() function returns true on success and false on failure. For example:
<?php $source_file = "codes.txt"; $target_file = "cracker.txt"; if(copy($source_file, $target_file)) echo "The content of $source_file is copied to $target_file"; else echo "Unable to copy"; ?>
PHP copy() Function Syntax
The syntax of copy() function in PHP, is:
copy(source, destination, context)
The third or last (context) parameter is optional, used to specify a context resource, created using the stream_context_create() function.
PHP Copy Content of One File to Another
<?php $source_file = "one.txt"; $target_file = "two.txt"; echo "<h1>Before Copy</h1>"; echo "<p>---$source_file---</p>"; $x = fopen($source_file, "r") or die("Unable to Open the File, $source_file"); echo fread($x, filesize($source_file)); fclose($x); echo "<p>---$target_file---</p>"; $x = fopen($target_file, "r") or die("Unable to Open the File, $target_file"); echo fread($x, filesize($target_file)); fclose($x); if(copy($source_file, $target_file)) { echo "<h1>After Copy</h1>"; echo "<p>---$source_file---</p>"; $x = fopen($source_file, "r") or die("Unable to Open the File, $source_file"); echo fread($x, filesize($source_file)); fclose($x); echo "<p>---$target_file---</p>"; $x = fopen($target_file, "r") or die("Unable to Open the File, $target_file"); echo fread($x, filesize($target_file)); fclose($x); } else echo "<p>Unable to copy</p>"; ?>
The output of above PHP example, is:
Note - The fopen() opens a file.
Note - The fclose() closes a file.
Note - The fread() is used to read the content of an opened file, using its pointer.
Note - The filesize() returns the size of specified file in bytes.
If you simplify above PHP example, then it would be similar to:
<?php $source_file = "one.txt"; $target_file = "two.txt"; if(copy($source_file, $target_file)) echo "<p>File copied successfully!</p>"; else echo "<p>Unable to copy</p>"; ?>
« Previous Tutorial Next Tutorial »