copy 함수

함수 copy는 원본 파일에서 지정된 위치로의 사본을 생성합니다. 첫 번째 매개변수로 원본 파일에 대한 경로를, 두 번째 매개변수로 파일을 복사해야 할 경로를 받습니다. 함수는 복사가 성공하면 true를, 오류가 발생하면 false를 반환합니다.

문법

phpcopy(source, dest);

예제

파일을 source.txt에서 destination.txt로 복사해 봅시다:

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

코드 실행 결과 (파일이 성공적으로 복사된 경우):

phptrue

예제

존재하지 않는 파일을 복사해 보겠습니다:

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

코드 실행 결과:

phpfalse

예제

원본 파일 존재 여부를 확인하며 복사하기:

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';
  }
?>

함께 보기

  • 함수 rename,
    파일 이름을 바꾸거나 이동시킵니다.
  • 함수 unlink,
    파일을 삭제합니다.
  • 함수 file_exists,
    파일 존재 여부를 확인합니다.