Fungsi copy() PHP;
Sekaligus juga akan dijelaskan pengertian, sintaks, parameter copy() dan beberapa contoh copy() PHP. Yang akan banyak membantu Anda dalam memahami cara kerja fungsi salin. Dan bagaimana Anda bisa menggunakannya?
PHP. Copy() Contoh Fungsi
Fungsi penyalinan ini adalah fungsi bawaan dari PHP. Terutama yang digunakan untuk menyalin file. Ini menyalin file yang diberikan ke file tujuan. Jika file tujuan sudah ada. Itu ditimpa. Fungsi salin () mengembalikan false saat gagal dan true saat sukses
sintaksis
Sintaks fungsi php copy() berikut
copy ( $source file, $destination file )Catatan. - Fungsi ini membutuhkan 2 parameter, file sumber pertama, dan file tujuan kedua
Deskripsi parameter
file $sumber. Ini menentukan jalur ke file sumber
berkas $tujuan. Ini digunakan untuk menentukan jalur ke file tujuan
Nilai Pengembalian. Mengembalikan false pada kegagalan dan true pada kesuksesan
Contoh 1. - File salinan PHP
Untuk menyalin file dari satu folder ke folder lain atau folder yang sama. Kita dapat dengan mudah membuat salinan atau duplikat file ke aslinya di PHP dengan fungsi bawaan PHP yang disebut copy()
Ringkasan. dalam tutorial ini, Anda akan belajar cara menyalin file dengan menggunakan fungsi file PHP copy() dari satu lokasi ke lokasi lain
Pengantar fungsi file copy() PHP
Untuk menyalin file dari satu lokasi ke lokasi lain, Anda menggunakan fungsi copy()
copy ( string $source , string $dest , resource $context = ? ) : bool
Code language: PHP (php)_Fungsi file copy()_ memiliki tiga parameter
- $source adalah jalur lengkap ke file yang ingin Anda salin
- $dest adalah jalur lengkap ke file tempat file akan disalin
- $context_ adalah sumber konteks yang valid
Fungsi copy()_ mengembalikan
<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)1 jika file berhasil disalin atau<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Perhatikan bahwa jika file $dest_ ada, fungsi copy() akan menimpanya
Contoh file salinan PHP
Mari kita ambil beberapa contoh penggunaan fungsi copy()
1) Contoh file salinan PHP sederhana
Contoh berikut menggunakan fungsi copy()_ untuk menyalin file
<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)7 ke<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)8<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
2) Periksa apakah file tujuan ada sebelum menyalin
Contoh berikut menggunakan fungsi copy()_ untuk menyalin file
<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)7 ke file<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)8. Juga, memeriksa apakah<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)_8 ada sebelum menimpa file<?php $source = 'readme.txt'; $dest = 'readme.bak'; !file_exists($source) && die("The file $source does not exist"); file_exists($dest) && die("The file $dest already exists"); echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)3) Fungsi pembantu file salin PHP
Fungsi pembantu
<?php $source = 'readme.txt'; $dest = 'readme.bak'; !file_exists($source) && die("The file $source does not exist"); file_exists($dest) && die("The file $dest already exists"); echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)_3 berikut menyalin file. Mengembalikan false jika file sumber tidak ada atau file tujuan ada dan argumen yang ditimpa adalah<?php $source = 'readme.txt'; $dest = 'readme.bak'; echo copy($source, $dest) ? "The file $source was copied to $dest successfully!" : "Error copying the file $source";
Code language: HTML, XML (xml)1