Doing/Web
[PHP] 파일 복사, 삭제, 이름 변경
YongArtist
2016. 5. 10. 23:15
Copy(원본 파일명, 복사 파일명);
Unlink(삭제 파일명);
복사, 삭제에 성공할 경우 true를, 실패하면 오류 코드 메시지를 표시
함수 앞에 @를 붙여 보안 유효성을 높여줌
이미 존재하는 파일이면 덮어씀
예시-파일 복사
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php // test.php파일을 복사본 test.phps로 만듭니다. $oldfile = 'test.php'; // 원본파일 $newfile = 'test.phps'; // 복사파일 // file_exists 실제 존재하는 파일인지 체크 if(file_exists($oldfile)) { if(!copy($oldfile, $newfile)) { echo "파일 복사 실패"; } else if (file_exists($newfile)) { echo "파일 복사 성공"; } } ?> | cs |
예시-파일 이동
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php // test.php파일을 test.txt로 만듭니다. $oldfile = 'test.php'; // 원본파일 $newfile = 'test.txt'; // 복사파일 // file_exists 실제 존재하는 파일인지 체크 if(file_exists($oldfile)) { if(!copy($oldfile, $newfile)) { echo "파일 복사 실패"; } else if (file_exists($newfile)) { // 복사에 성공하면 원본 파일을 삭제 if(!@unlink($oldfile)){ if(@unlink($newfile)){ echo "파일 이동 실패"; } } } } ?> | cs |
bool rename ( string $oldname , string $newname [, resource $context ] )
예시-이름 변경
1 2 3 4 5 6 | <?php rename("space1","space2") // space1 폴더를 space2 폴더로 이름 변경 ?> | cs |