Location: PHPKode > scripts > class.jfile.php > class-jfile-php/class.jfile.php
<?PHP

//  filename:	class.jfile.php
//  author:	jani mikkonen (hide@address.com) 
//  comment:	class to open/close files with file locking with timeout support
//  license:	GPL
//  date:	29.11.2000
//
//  v1.0:	First release, totally untested and my first shot to produce something
//		like this. *all comments* are welcome!
//
// Usage:	Check out the comments. If you need this kind of class you probably can
//		read the code to see what happens inside it.
// TODO:	* When opening or locking file, check if its not done allready.
//		* When closing, check if file is actually opened and/or locked.


class jfile {

	var $fileHandle;
	var $fileName;
	var $timeOut = 10;
	var $status = 0;
	var $fileMode;
	var $lastStatus;
	var $timeOutm = 200;
	var $debug = false;
	
	// Simple constructor. Does allmost nothing except you must pass the filename as
	// a parameters to this class to actually open something.

	function jfile($fn) { 
		$this->lastStatus = true;
		$this->fileName = $fn;
	}

	// Private
	// Opens a file with certain mode (read,write,append) and without locking. 

	function openFile($mode) {
		if ($debug) print "in: openFile($mode)<br>";
		$this->lastStatus = false;
		$this->fileMode=$mode;
		$this->fileHandle = fopen($this->fileName,$this->fileMode);
		if (! $this->fileHandle == false ) {
			$this->lastStatus = true;
		}
		if ($debug) print "out: openFile($mode)<br>";
		return $this->lastStatus;
	}

	// Private
	// Closes file that has been previously opened.
	
	function closeFile() {
		if ($debug) print "in: closeFile()";
		$this->lastStatus = false;
		$this->fileMode='';
		$this->lastStatus = fclose($this->fileHandle);
		if ($debug) print "out: closeFile()";
		return $this->lastStatus;
	}

	// Public
	// Opens the file with certain mode and certain locking type. 
	// What the code does:
	// Tries first to open the file and if successfull, then lock it
	// with user given lock operation. In any case of failure, program sleeps
	// for 200 milliseconds and tries again. Loop counter is checked against
	// class varible timeOut and if counter is bigger, exit from the method
	// and not open the file. $mode is passed to fopen() and $locktype is
	// passed to flock operation.

	function openFileAsLocked($mode,$lockType) {
		if ($debug) print "in: openFileAsLocked($mode,$lockType)<br>";
		$this->lastStatus = false;
		$counter = 0;
		while ( ($this->lastStatus==false) && ($counter<$this->timeOut) ) {
			$this->openFile($mode);
			if ( $this->lastStatus==false ) {
				$counter ++;
				usleep($this->timeOutm);
				
			}
		}
		if ($this->lastStatus==true) {
			while ( ($this->lastStatus==false) && ($counter<$this->timeOut) ) {
				$this->lastStatus = flock($this->fileHandle,$locktype);
				if ($this->lastStatus==false) {
					$counter ++;
					usleep($this->timeOutm);
				}
			}
		} 

		if ($debug) print "out: openFileAsLocked($mode,$lockType)<br>";
		return $this->lastStatus;
	}


	// Public
	// Tries to free the lock and close the previously opened file
	// What the code does:
	// Exactly the same as openFileAsLocked() method but in reverse order.

	function closeLockedFile() {
		if ($debug) print "in: closeLockedFile()<br>";
		$this->lastStatus = false;
		$counter = 0;
		while ( ($this->lastStatus==false) && ($counter<$this->timeOut) ) {
			$this->lastStatus = flock($this->fileHandle,LOCK_UN);
			if (!$this->lastStatus) {
				$counter ++;
				usleep($this->timeOutm);
			}
		}
		if ($this->lastStatus==true) {
			while ( (! $this->lastStatus) && ($counter<$this->timeOut) ) {
				$this->closeFile();
				if ( !$this->lastStatus ) {
					$counter ++;
					usleep($this->timeOutm);
				}
			}
		}
		if ($debug) print "out: closeLockedFile()<br>";
		return $this->lastStatus;
	}
}
?>
Return current item: class.jfile.php