GoLang copy

is this article helpful? yes | no
GoLang replacement for PHP's copy [edit | history]
func Copy(source, dest string) (bool, error) {
	fd1, err := os.Open(source)
	if err != nil {
		return false, err
	}
	defer fd1.Close()
	fd2, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		return false, err
	}
	defer fd2.Close()
	_, e := io.Copy(fd2, fd1)
	if e != nil {
		return false, e
	}
	return true, nil
}

PHP copy

PHP original manual for copy [ show | php.net ]

copy

(PHP 4, PHP 5, PHP 7)

copyCopies file

Description

bool copy ( string $source , string $dest [, resource $context ] )

Makes a copy of the file source to dest.

If you wish to move a file, use the rename() function.

Parameters

source

Path to the source file.

dest

The destination path. If dest is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files.

Warning

If the destination file already exists, it will be overwritten.

context

A valid context resource created with stream_context_create().

Return Values

Returns TRUE on success or FALSE on failure.

Changelog

Version Description
5.3.4 Changed the context parameter to actually have an effect. Previously, any context was ignored.
5.3.0 Added context support.
4.3.0 Both source and dest may now be URLs if the "fopen wrappers" have been enabled. See fopen() for more details.

Examples

Example #1 copy() example

<?php
$file 
'example.txt';
$newfile 'example.txt.bak';

if (!
copy($file$newfile)) {
    echo 
"failed to copy $file...\n";
}
?>

See Also