메뉴 건너뛰기

프로그램언어

2019.01.08 14:18

파일

조회 수 1226 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

파일

파일의 복사


<?php
$file = 'readme.txt';
$newfile = 'example.txt.bak';

if (!copy($file, $newfile)) {
   echo "failed to copy $file...\n";
}
?>

파일삭제


<?php
unlink('deleteme.txt');
?>

파일 내용 읽기

1.file_get_contents : 텍스트로 이루어진 파일을 읽어서 문자열을 리턴한다.


<?php
 #파일과 같은 디렉토리에 readme.txt 파일이 존재해야 한다.
$file = './readme.txt';
echo file_get_contents($file);
?>
2. file_put_contents : 문자열을 파일에 저장한다.


<?php
$file = './writeme.txt';
file_put_contents($file, 'coding everybody');
?>
네트워크를 통해 데이터 읽어오기


<?php
$homepage = file_get_contents('http://php.net/manual/en/function.file-get-contents.php');
echo $homepage;
?>

권한

파일을 읽고 쓸 때 권한의 문제로 오류가 발생할 수 있다. 권한에 대한 문제는 다소 복잡하기 때문에 동영상 강의를 참고한다.

아래 코드는 특정 파일이 읽을 수 있는 상태인지를 확인한다.

1. is_readable : 파일을 읽을 수 있는 상태인지 확인 하는 함수


<?php
$filename = 'readme.txt';
if (is_readable($filename)) { #읽을 수 있는 상태 인지 확인
   echo 'The file is readable';
} else {
   echo 'The file is not readable';
}
?>

2. is_writable : 특정 파일이 쓰기 가능한지 확인하는 함수


<?php
$filename = 'writeme.txt';
if (is_writable($filename)) { #특정파일이 쓰기 가능한지 확인
   echo 'The file is writable';
} else {
   echo 'The file is not writable';
}
?>

3. file_exists : 파일 존재 여부를 확인하는 함수


<?php
$filename = 'readme.txt';
if (file_exists($filename)) { #파일 존재 여부를 확인
   echo "The file $filename exists";
} else {
   echo "The file $filename is not exists";
}
?>


  1. No Image 07Mar
    by
    2017/03/07 Views 20061 

    파일 이름에서 확장자 추출마스터

  2. No Image 06Mar
    by
    2017/03/06 Views 20184 

    파일 업로드 (중복처리)

  3. No Image 14Apr
    by
    2015/04/14 Views 25334 

    파일 삭제

  4. No Image 08Jan
    by
    2019/01/08 Views 1610 

    파일 및 데이타베이스 백업

  5. No Image 19Jun
    by
    2020/06/19 Views 618 

    파일 다운로드 함수(멀티 이어받기/속도제한)

  6. No Image 26Mar
    by
    2021/03/26 Views 243 

    파일 output을 return 하기

  7. No Image 08Jan
    by 조쉬
    2019/01/08 Views 1226 

    파일

  8. No Image 08Jul
    by
    2021/07/08 Views 322 

    특정일의 주차 구하기

  9. No Image 26Mar
    by
    2014/03/26 Views 21623 

    특수문자 없애는 정규표현식

  10. No Image 22Aug
    by
    2016/08/22 Views 20732 

    템플릿 관련 정보

  11. No Image 16Jan
    by
    2019/01/16 Views 1249 

    태그 또는 멘션 소스 뽐아내기방법

  12. No Image 24Jul
    by
    2018/07/24 Views 5741 

    키를 이용한 암호화/복호화 함수입니다.

  13. No Image 27Mar
    by
    2021/03/27 Views 231 

    클래스와 인스턴스 그리고 메소드 만들기

  14. No Image 25Mar
    by
    2021/03/25 Views 270 

    큰따옴표(") 와 작은따옴표(')

  15. No Image 19Jul
    by
    2018/07/19 Views 6039 

    쿠폰번호 발행 업데이트판. (간단한 클래스화[PHP4 기준] 등...)

  16. No Image 27Feb
    by
    2014/02/27 Views 29211 

    쿠키변수받기

  17. No Image 23Nov
    by
    2020/11/23 Views 314 

    쿠키 확인 후 만료시 세션 파괴하는 방법

  18. No Image 26Mar
    by
    2021/03/26 Views 307 

    콜론 연산자

  19. No Image 12Jan
    by
    2023/01/12 Views 213 

    코드 생성 하기

  20. No Image 07Mar
    by
    2017/03/07 Views 21131 

    체크박스, post 로 넘기고 받아서 다시 체크하기, checkbox

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved