- 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 exit()
- PHP exit() vs. break
- PHP isset()
- 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 mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- 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 Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP copy(): Copy the 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 the codes.txt file gets copied into the cracker.txt file. If the 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 the copy() function in PHP is:
copy(source, destination, context)
The third or last (context) parameter is optional and is used to specify a context resource created using the stream_context_create() function.
PHP Copy Content of One File to Another
The PHP code below is an example of how the "copy()" function can be used to copy files. In this example, the content before and after copying will be displayed.
<?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 the above PHP example is:
Note: The fopen() function opens a file.
Note: The fclose() function closes a file.
Note: The fread() function is used to read the content of an opened file using its pointer.
Note: The filesize() function returns the size of the specified file in bytes.
If you simplify the 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 »