Skip to content

Instantly share code, notes, and snippets.

@miyukki
Created March 28, 2012 11:30
Show Gist options
  • Save miyukki/2225539 to your computer and use it in GitHub Desktop.
Save miyukki/2225539 to your computer and use it in GitHub Desktop.
role.php is print text in current directory on display, and delete printed text.
<?php
while(true){
if ($handle = opendir('./')) {
/* ディレクトリをループする際の正しい方法です */
while (false !== ($file = readdir($handle))) {
if(endWith($file, '.txt')) {
printComment($file);
}
}
closedir($handle);
}
}
function printComment($file) {
$fp = @fopen($file, 'r+');
while( !feof( $fp ) ){
echo fgets( $fp, 9182 );
}
flock($fp, LOCK_EX);
ftruncate($fp,0);
flock($fp, LOCK_UN);
fclose($fp);
}
function endWith($haystack, $needle){
$length = (strlen($haystack) - strlen($needle));
// 文字列長が足りていない場合はFALSEを返します。
if($length < 0) return FALSE;
return strpos($haystack, $needle, $length) !== FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment