짧은 코드 긴 내용 - 초 간단 파일 카운터
스쿨 길드 정보에 올렸던 "재미있을까 재미없을까 200바이트 이하의 장난성 코드들…."
파일이나 디렉터리에 대한 권한이 없으면 깔끔하게 오류 메시지까지 출력합니다.
c.txt 파일을 올려놓고 간단하게 707 권한을 부여하고
위 소스 파일을 실행하면 숫자가 증가하는 것을 볼 수 있을 것입니다.
getcwd() - 현재 경로를 구합니다.
http://php.net/manual/kr/function.getcwd.php [한글 구 버전]
http://php.net/manual/en/function.getcwd.php [영문 신 버전]
is_file() - 파일인지 검사합니다.
http://php.net/manual/en/function.is-file.php
is_writable() - 쓰기 가능한지 검사합니다.
http://php.net/manual/en/function.is-writable.php
file_put_contents() - 파일에 내용을 기록합니다.
http://php.net/manual/en/function.file-put-contents.php
셸 명령어를 이용하면 좀 더 짧아집니다만
이것저것 고려해서 이 정도가 적당한 것 같습니다.
<?php
$a='need "'.($b='./c.txt').'" or "'.getcwd().'" +w permission!';if((($c=is_file($b))&&is_writable($b))||is_writable('.')){file_put_contents($b,$d=$c?file_get_contents($b)+1:1);echo $d;}else{echo $a;}
?>
$a='need "'.($b='./c.txt').'" or "'.getcwd().'" +w permission!';if((($c=is_file($b))&&is_writable($b))||is_writable('.')){file_put_contents($b,$d=$c?file_get_contents($b)+1:1);echo $d;}else{echo $a;}
?>
파일이나 디렉터리에 대한 권한이 없으면 깔끔하게 오류 메시지까지 출력합니다.
c.txt 파일을 올려놓고 간단하게 707 권한을 부여하고
위 소스 파일을 실행하면 숫자가 증가하는 것을 볼 수 있을 것입니다.
getcwd() - 현재 경로를 구합니다.
http://php.net/manual/kr/function.getcwd.php [한글 구 버전]
http://php.net/manual/en/function.getcwd.php [영문 신 버전]
is_file() - 파일인지 검사합니다.
http://php.net/manual/en/function.is-file.php
is_writable() - 쓰기 가능한지 검사합니다.
http://php.net/manual/en/function.is-writable.php
file_put_contents() - 파일에 내용을 기록합니다.
http://php.net/manual/en/function.file-put-contents.php
셸 명령어를 이용하면 좀 더 짧아집니다만
이것저것 고려해서 이 정도가 적당한 것 같습니다.
함께 나눈 이야기
댓글댓글 남기기