Fungsi copy

Fungsi copy membuat salinan file dari sumber ke lokasi yang ditentukan. Parameter pertama fungsi menerima path ke file sumber, parameter kedua - path tujuan penyalinan file. Fungsi mengembalikan true jika penyalinan berhasil dan false jika terjadi error.

Sintaks

phpcopy(source, dest);

Contoh

Mari salin file dari source.txt ke destination.txt:

php<?php
  $res = copy('source.txt', 'destination.txt');
  var_dump($res);
?>

Hasil eksekusi kode (jika file berhasil disalin):

phptrue

Contoh

Mari coba salin file yang tidak ada:

php<?php
  $res = copy('nonexistent.txt', 'destination.txt');
  var_dump($res);
?>

Hasil eksekusi kode:

phpfalse

Contoh

Penyalinan dengan pengecekan keberadaan file sumber:

php<?php
  if (file_exists('source.txt')) {
    $res = copy('source.txt', 'destination.txt');
    echo $res ? 'File copied' : 'Copy failed';
  } else {
    echo 'Source file not found';
  }
?>

Lihat juga

  • fungsi rename,
    yang mengganti nama atau memindahkan file
  • fungsi unlink,
    yang menghapus file
  • fungsi file_exists,
    yang memeriksa keberadaan file