PHP 튜토리얼

PHP 홈 PHP 소개 PHP 설치 PHP 구문 PHP 주석 PHP 변수 PHP 에코 / 인쇄 PHP 데이터 유형 PHP 문자열 PHP 숫자 PHP 수학 PHP 상수 PHP 연산자 PHP If...Else...Elseif PHP 스위치 PHP 루프 PHP 함수 PHP 배열 PHP 슈퍼글로벌 PHP 정규식

PHP 양식

PHP 양식 처리 PHP 양식 유효성 검사 PHP 양식 필요 PHP 양식 URL/이메일 PHP 양식 완성

PHP 고급

PHP 날짜 및 시간 PHP 포함 PHP 파일 처리 PHP 파일 열기/읽기 PHP 파일 생성/쓰기 PHP 파일 업로드 PHP 쿠키 PHP 세션 PHP 필터 PHP 필터 고급 PHP 콜백 함수 PHP JSON PHP 예외

PHP OOP

PHP OOP란? PHP 클래스/객체 PHP 생성자 PHP 소멸자 PHP 액세스 수정자 PHP 상속 PHP 상수 PHP 추상 클래스 PHP 인터페이스 PHP 특성 PHP 정적 메서드 PHP 정적 속성 PHP 네임스페이스 PHP 반복 가능

MySQL 데이터베이스

MySQL 데이터베이스 MySQL 연결 MySQL 생성 DB MySQL 테이블 생성 MySQL 삽입 데이터 MySQL 마지막 ID 가져오기 MySQL은 다중 삽입 MySQL 준비 MySQL 선택 데이터 MySQL 어디 MySQL 주문 기준 MySQL 데이터 삭제 MySQL 업데이트 데이터 MySQL 제한 데이터

PHP XML

PHP XML 파서 PHP SimpleXML 파서 PHP SimpleXML - 가져오기 PHP XML 국외 거주자 PHP XML DOM

PHP - AJAX

AJAX 소개 AJAX PHP AJAX 데이터베이스 AJAX XML AJAX 라이브 검색 AJAX 투표

PHP 예제

PHP 예제 PHP 컴파일러 PHP 퀴즈 PHP 연습 PHP 인증서

PHP 참조

PHP 개요 PHP 배열 PHP 캘린더 PHP 날짜 PHP 디렉토리 PHP 오류 PHP 예외 PHP 파일 시스템 PHP 필터 PHP FTP PHP JSON PHP 키워드 PHP 라이브러리 XML PHP 메일 PHP 수학 PHP 기타 PHP MySQLi PHP 네트워크 PHP 출력 제어 PHP 정규식 PHP SimpleXML PHP 스트림 PHP 문자열 PHP 변수 처리 PHP XML 파서 PHP 우편번호 PHP 시간대

PHP 파일 열기/읽기/닫기


이 장에서는 서버에서 파일을 열고, 읽고, 닫는 방법에 대해 설명합니다.


PHP 열기 파일 - fopen()

파일을 여는 더 좋은 방법은 fopen()기능을 사용하는 것입니다. 이 기능은 기능보다 더 많은 옵션을 제공합니다 readfile().

수업 중에 "webdictionary.txt"라는 텍스트 파일을 사용합니다.

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

의 첫 번째 매개변수는 fopen()열려는 파일의 이름을 포함하고 두 번째 매개변수는 파일을 열어야 하는 모드를 지정합니다. 다음 예제는 fopen() 함수가 지정된 파일을 열 수 없는 경우에도 메시지를 생성합니다.

예시

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>

팁: 및 기능은 아래 fread()에서 fclose()설명합니다.

파일은 다음 모드 중 하나로 열릴 수 있습니다.

Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists


PHP 읽기 파일 - fread()

함수 는 fread()열린 파일에서 읽습니다.

의 첫 번째 매개변수는 fread()읽을 파일의 이름을 포함하고 두 번째 매개변수는 읽을 최대 바이트 수를 지정합니다.

다음 PHP 코드는 "webdictionary.txt" 파일을 끝까지 읽습니다.

fread($myfile,filesize("webdictionary.txt"));

PHP 닫기 파일 - fclose()

fclose()함수는 열려 있는 파일을 닫는 데 사용됩니다.

작업을 마친 후에는 모든 파일을 닫는 것이 좋은 프로그래밍 방법입니다. 리소스를 차지하는 서버에서 열린 파일이 실행되는 것을 원하지 않습니다!

fclose()닫으려는 파일의 이름(또는 파일 이름을 보유하는 변수)이 필요합니다 .

<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>

PHP 읽기 한 줄 - fgets()

fgets()함수는 파일에서 한 줄을 읽는 데 사용됩니다.

아래 예는 "webdictionary.txt" 파일의 첫 번째 줄을 출력합니다.

예시

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>

참고: 함수 를 호출한 후 fgets()파일 포인터가 다음 줄로 이동했습니다.


PHP 파일 끝 확인 - feof()

feof()함수는 "파일 끝"(EOF)에 도달했는지 확인합니다.

feof()함수는 길이를 알 수 없는 데이터를 반복할 때 유용합니다.

아래 예에서는 파일 끝에 도달할 때까지 "webdictionary.txt" 파일을 한 줄씩 읽습니다.

예시

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

PHP 읽기 단일 문자 - fgetc()

fgetc()함수는 파일에서 단일 문자를 읽는 데 사용됩니다.

아래 예에서는 파일 끝에 도달할 때까지 "webdictionary.txt" 파일을 문자별로 읽습니다.

예시

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>

참고: 함수 호출 후 fgetc()파일 포인터는 다음 문자로 이동합니다.


완전한 PHP 파일 시스템 참조

파일 시스템 기능에 대한 전체 참조를 보려면 전체 PHP 파일 시스템 참조 로 이동하십시오 .


PHP 연습

연습으로 자신을 테스트하십시오

연습:

파일을 열고 파일 끝까지 한 번에 한 문자를 출력하도록 올바른 구문을 작성하십시오.

$myfile = fopen("webdict.txt", "r");
while(!($myfile)) {
  echo ($myfile);
}