news
media
projects
links
styles
about
feeds

September 17, 2004

[PHP] Cached fopen();

Here's a great one, cached fopen.

function cached_fopen($file, $file_mode, $timeout_seconds = 600, $cache_path = "/tmp", $error_report = false){

	clearstatcache();
	$cache_filename=$cache_path . "/" . urlencode($file) .".cached";

		if (!( @file_exists($cache_filename ) and ( ( @filemtime($cache_filename) + $timeout_seconds) > ( time() ) ) ) ){
			
			if($error_report) echo 'Caching new file '.$file .' in '.$cache_path;
			
			if(!$f = fopen($file,"r")){
				if($error_report) echo 'ERROR: Unable to open remote file: '.$file;
				return false;
			}
			
			if(!$f2 = fopen($cache_filename,"w+")){
				if($error_report) echo 'ERROR: Unable to open local cache file: '.$cache_filename;
				return false;
			}
				
			while ($r=fread($f,8192) ) {
				fwrite($f2,$r);
			}
				
			fclose($f2); 
			fclose($f);
		} else {
			if($error_report) echo 'Using cached file: '.$cache_filename;	
		}
	
	$handle = fopen($cache_filename, $file_mode);
	return $handle;
}


This is based off of some code I found in a comment on php.net under the fopen function. I modified it a little bit. I've gotta find a better way to do this code section.

By: Dustin DuPree

Add Comment

August 21, 2004

[AS 1.0] Quick'n'Dirty pre-loader

This goes on the first frame, content starts on 2nd frame.

stop();
this.onEnterFrame = function(){
	var bytes = _root.getBytesTotal();
	var bytes_loaded = _root.getBytesLoaded();
	perc = Math.floor(100*(bytes_loaded/bytes));
	percer=""+perc+"%";
	if (bytes_loaded == bytes) {
		this.gotoAndStop(2);
		this.loader = "COMPLETE";
		delete this.onEnterFrame;
	} else {
		this.loadbar._xscale = perc;
		round_loaded = Math.round(bytes_loaded/1000);
		round_total = Math.round(bytes/1000);
		this.loader = "Loading: ("+round_loaded
			+"kb / "+round_total+"kb) "+percer;
	}
}

By: Dustin DuPree

Add Comment

August 16, 2004

[AS 1.0] Track Mouse & Draw.

Pretty simple mouse tracking method. Click to draw.

var mp:Array = [];
tmouse = function(){
	mp.push({x:_xmouse, y:_ymouse});
}
trackInterval = setInterval(tmouse, 10);

onMouseDown = function(){
	lineStyle(1,0x000000,100);
	moveTo(mp[0].x, mp[0].y);
	for(var i=0;i < mp.length;i++){
		lineTo(mp[i].x, mp[i].y);
		moveTo(mp[i].x, mp[i].y);
	}
	mp.splice(0,mp.length-1);
}
Mouse.addListener(this);

By: Dustin DuPree

Add Comment

recent media

Perfect Paradigm by Dustin DuPree
Perfect Paradigm
Flash Levels by Dustin DuPree
Flash Levels
Motion In Flash by Dustin DuPree
Motion In Flash