라이브러리

[PHP] streamWrapper::stream_lock - 권고 파일 잠금




PHP Stream Wrapper: stream_lock

PHP의 Stream Wrapper는 파일 시스템에 접근하는 방법을 제공합니다. Stream Wrapper는 파일을 읽거나 쓰기 위해 사용됩니다. `stream_lock` 메소드는 파일에 대한 잠금을 설정하는 메소드입니다.

stream_lock 메소드


`stream_lock` 메소드는 파일에 대한 잠금을 설정합니다. 이 메소드는 파일을 읽거나 쓰기 전에 호출되어야 합니다. 파일에 대한 잠금을 설정하면 다른 프로세스나 스레드가 파일을 읽거나 쓰기 전에 잠금을 해제해야 합니다.

예제


#hostingforum.kr
php

// 파일을 열기

$file = fopen('example.txt', 'r+');



// 파일에 대한 잠금을 설정

stream_lock($file, LOCK_EX);



// 파일을 읽기

echo fread($file, 1024);



// 파일에 대한 잠금을 해제

stream_unlock($file);



// 파일을 닫기

fclose($file);



LOCK_EX


`LOCK_EX`는 파일에 대한 잠금을 설정합니다. 다른 프로세스나 스레드가 파일을 읽거나 쓰기 전에 잠금을 해제해야 합니다.

LOCK_SH


`LOCK_SH`는 파일에 대한 읽기 잠금을 설정합니다. 다른 프로세스나 스레드가 파일을 읽기 전에 잠금을 해제해야 합니다.

LOCK_UN


`LOCK_UN`는 파일에 대한 잠금을 해제합니다.

예제 (LOCK_EX, LOCK_SH, LOCK_UN)


#hostingforum.kr
php

// 파일을 열기

$file = fopen('example.txt', 'r+');



// 파일에 대한 읽기 잠금을 설정

stream_lock($file, LOCK_SH);



// 파일을 읽기

echo fread($file, 1024);



// 파일에 대한 읽기 잠금을 해제

stream_unlock($file);



// 파일에 대한 쓰기 잠금을 설정

stream_lock($file, LOCK_EX);



// 파일에 쓰기

fwrite($file, 'Hello, World!');



// 파일에 대한 쓰기 잠금을 해제

stream_unlock($file);



// 파일에 대한 읽기 잠금을 설정

stream_lock($file, LOCK_SH);



// 파일을 읽기

echo fread($file, 1024);



// 파일에 대한 읽기 잠금을 해제

stream_unlock($file);



// 파일을 닫기

fclose($file);



참고


* PHP Stream Wrapper: https://www.php.net/manual/en/book.stream.php
* stream_lock: https://www.php.net/manual/en/function.stream-lock.php
* LOCK_EX: https://www.php.net/manual/en/constant.LOCK-EX.php
* LOCK_SH: https://www.php.net/manual/en/constant.LOCK-SH.php
* LOCK_UN: https://www.php.net/manual/en/constant.LOCK-UN.php
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 119 페이지

검색

게시물 검색