copy
COPIES a particular
file if exists.
This function returns TRUE on success or FALSE on failure.
<?php bool copy ( str $source , str $dest , res $context = ? ) where , $source = The source path to the file $dest = The destination path $context = A valid context resource criated by stream_context_create ?>
$source
The source path to the file. To move the $source file, the best is to use the rename function.
$dest
The new destination to the path. If $dest file already exists, it will be overwritten. If $dest is an URL, the operation may fail if the wrapper does not support overwriting of existing files.
$context
A valid context resource criated by stream_context_create .
EXERCISE
<?php $path01 = __DIR__ . '/temp/' ; // You can establish other paths $srcfile01 = 'sw7.jpg' ; $dstfile01 = 'sw7.copy.jpg' ; if ( file_exists ( $path01 . $srcfile01 )) { if( copy ( $path01 . $srcfile01 , $path01 . $dstfile01 )) { echo 'The ' . $srcfile01 . ' file01 WAS copied as ' . $dstfile01 ; } else { echo 'The ' . $srcfile01 . ' file01 WAS NOT copied as ' . $dstfile01 ; } } else { echo 'The ' . $srcfile01 . ' file01 WAS NOT found.' ; } ?>
RESULT The sw7.jpg file WAS copied as sw7.copy.jpg Preventing the file being overwritten a different name was used, since the one whose save was in the same directory. EXERCISE
<?php $srcpath02 = __DIR__ . '/temp/' ; $dstpath02 = __DIR__ . '/temp/t2/' ; // You can establish other paths $dir2make02 = $dstpath02 ; if(! file_exists ( $dir2make02 )) { @ mkdir ( $dir2make02 ); if( file_exists ( $dir2make02 )) { echo 'Directory created successfully.<br><br>' ; } else { echo 'Directory can not be created.<br><br>' ; } } else { echo 'Directory already exists and can not be recreated.<br><br>' ; } $srcfile02 = 'sete anões.txt' ; $dstfileo2 = 'sete anões.txt' ; if ( file_exists ( $srcpath02 . $srcfile02 )) { if( copy ( $srcpath02 . $srcfile02 , $dstpath02 . $dstfileo2 )) { echo 'The "' . $srcfile02 . '" file WAS copied as "' . $dstfileo2 . '"<br>' ; } else { echo 'The "' . $srcfile02 . '" file WAS NOT copied as "' . $dstfileo2 . '"<br>' ; } } else { echo 'The "' . $srcfile02 . '" file WAS NOT found<br>' ; } ?>
RESULT Directory already exists and can not be recreated. The sete anões.txt file WAS copied as sete anões.txt .
EXERCISE
<?php $dest03 = __DIR__ ; // You can establish other paths $opts03 = [ 'https' =>[ 'method' => "GET" , 'header' => "Accept-language: en\r\n" . "Cookie: foo=bar\r\n" ] ]; $context03 = stream_context_create ( $opts03 ); $srcpath03 = $dest03 . '/temp/' ; $dstpath03 = $srcpath03 ; $srcfile03 = 'Your Wildest Dreams.m4a' ; $dstfile03 = 'Your W Dreams.m4a' ; if ( file_exists ( $srcpath03 . $srcfile03 )) { if( copy ( $srcpath03 . $srcfile03 , $dstpath03 . $dstfile03 , $context03 )) { echo 'The "' . $srcfile03 . '" file WAS copied as "' . $dstfile03 . '"' ; } else { echo 'The "' . $srcfile03 . '" file WAS NOT copied as "' . $dstfile03 . '"' ; } } else { echo 'The "' . $srcfile03 . '" file WAS NOT found.' ; } ?>
RESULT The Your Wildest Dreams.m4a file WAS copied as Your W Dreams.m4a The file used name was modified since it was saved in the same directory. Your user agent does not support the HTML5 Audio element.