copy a file in php

To copy a file in PHP, you can use the following code:

main.php
$source_file = 'path/to/source/file';
$destination_file = 'path/to/destination/file';

// Open the source file and get its contents
$source_handle = fopen($source_file, 'r');
$source_content = fread($source_handle, filesize($source_file));

// Open the destination file and write the contents of the source file to it
$destination_handle = fopen($destination_file, 'w');
fwrite($destination_handle, $source_content);

// Close the file handles
fclose($source_handle);
fclose($destination_handle);
497 chars
15 lines

This code opens the source file, reads its contents, opens the destination file, and writes the contents of the source file to the destination file. Finally, it closes the file handles.

Note that this method is suitable for small files. For larger files, you may want to consider using the stream_copy_to_stream method or other methods.

gistlibby LogSnag