note 97485 modified in function.ftruncate by danbrown
| From: | danbrown@php.net | Date: | Thu, 22 Apr 2010 14:08:53 +0000 |
| Subject: | note 97485 modified in function.ftruncate by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168509@lists.php.net to get a copy of this message | ||
Simple script which will delete spaces from end of php files (suitable in big sites, when you are
including many files before sending headers and without output buffering):
<?php
function mapfiles($func,$filename,$level=0) {
if(is_dir($filename)) {
if($level>30) return; //limit recurence in case of looped dirs
$handle=opendir($filename);
while(($dirname=readdir($handle))!==false) {
if($dirname=='.'||$dirname=='..') continue;
mapfiles($func,$filename.'/'.$dirname,$level+1);
}
closedir($handle);
} else {
$func($filename);
}
}
function scanfile($filename) {
if(substr($filename,-4)!=".php") return;
$ff=fopen($filename,"r+");
fseek($ff, -10, SEEK_END);
$str=fread($ff,12);
$matches=array();
if(preg_match('/\?>(\s+)$/',$str,$matches)) {
$fsize=filesize($filename);
ftruncate($ff, $fsize-strlen($matches[1]));
}
fclose($ff);
}
mapfiles("scanfile",".");
?>
--was--
Simple script which will delete spaces from end of php files (suitable in big sites, when you are
including many files before sending headers and without output buffering):
function mapfiles($func,$filename,$level=0) {
if(is_dir($filename)) {
if($level>30) return; //limit recurence in case of looped dirs
$handle=opendir($filename);
while(($dirname=readdir($handle))!==false) {
if($dirname=='.'||$dirname=='..') continue;
mapfiles($func,$filename.'/'.$dirname,$level+1);
}
closedir($handle);
} else {
$func($filename);
}
}
function scanfile($filename) {
if(substr($filename,-4)!=".php") return;
$ff=fopen($filename,"r+");
fseek($ff, -10, SEEK_END);
$str=fread($ff,12);
$matches=array();
if(preg_match('/\?>(\s+)$/',$str,$matches)) {
$fsize=filesize($filename);
ftruncate($ff, $fsize-strlen($matches[1]));
}
fclose($ff);
}
mapfiles("scanfile",".");
http://php.net/manual/en/function.ftruncate.php