Location: PHPKode > scripts > File Joiner > class.filejoiner.php
<?php

class FileJoiner {
	var $fileList;
	var $timeout;
	var $fileContents;
	var $compression;

	function __construct($fileList=array()) {
		try {
			if(empty($fileList)) throw new Exception("File list is empty.");
			if(!is_array($fileList)) throw new Exception("File list is expected to be array.");

			$this->fileList = $fileList;
			$this->fileContents = '';
			$this->timeout = 30;
			$this->compression = false;
		}
		catch (Exception $e) {
			echo 'Error: ' .$e->getMessage();
		}
	}

	function join($fileName='') {
		if (empty($fileName)) throw new Exception("File name cannot be empty.");
		foreach ($this->fileList as $url) {
			$this->getFile($url, $this->timeout);
		}
		try {
			// Save file
			if (!$fp = fopen($fileName, 'w')) throw new Exception("Cannot open file ($fileName).");
			if (false === fwrite($fp, $this->fileContents)) throw new Exception("Cannot write to file ($fileName).");
			fclose($fp);
		}
		catch (Exception $e) {
			echo 'File IO error: ' .$e->getMessage();
		}
	}

	function getFile($url, $timeout=0) {
		try {
			$fileContents = '';
			# use CURL library to fetch remote file
			$ch = curl_init();
			$this->url = $url;
			$this->timeout = $timeout;
			curl_setopt ($ch, CURLOPT_URL, $url);
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
			$fileContents = curl_exec($ch);
			if ( curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200 ) {
				throw new Exception('Bad data file '.$url);
			}
			if($this->compression) $fileContents = preg_replace('/\s+/', ' ', $fileContents);
			$this->fileContents .= "/* ".basename($url)." */\n\n".$fileContents."\n\n";
		}
		catch (Exception $e) {
			echo 'Network error: ' .$e->getMessage();
		}
	}
}

// Get CSS files
$fileList = array();
$fileList[] = 'http://www.ackrentals.net/css/reset.css';
$fileList[] = 'http://www.ackrentals.net/css/960_24_col.css';
$fileList[] = 'http://www.ackrentals.net/css/syles.css';
$fileList[] = 'http://www.ackrentals.net/css/calendar_styles.css';
$fileList[] = 'http://www.ackrentals.net/css/styles_prop_details.css';
$fileList[] = 'http://www.ackrentals.net/css/textstyles.css';
$fileList[] = 'http://www.ackrentals.net/css/print.css';
$fileList[] = 'http://www.ackrentals.net/css/menus.css';
$fileList[] = 'http://www.ackrentals.net/css/tabs.css';
$fileList[] = 'http://www.ackrentals.net/css/formStyles.css';
$fileList[] = 'http://www.ackrentals.net/css/image_gallery_plugin.css';
$fileList[] = 'http://www.ackrentals.net/css/text.css';
$fileList[] = 'http://www.ackrentals.net/css/textstyles.css';
$fileList[] = 'http://www.ackrentals.net/js/fancybox/jquery.fancybox-1.3.1.css';

$joiner = new FileJoiner($fileList);
$joiner->compression = true;
$joiner->join('css/allstyles.css');
unset($joiner);

////

$fileList = array();
$fileList[] = 'http://www.ackrentals.net/js/DD_belatedPNG.js';
$fileList[] = 'http://www.ackrentals.net/js/swfobject_new.js';
$fileList[] = 'http://www.ackrentals.net/js/RegExpValidate.js';
$fileList[] = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
$fileList[] = 'http://cdn.jquerytools.org/1.1.2/tiny/jquery.tools.min.js';
$fileList[] = 'http://www.ackrentals.net/js/fancybox/jquery.mousewheel-3.0.2.pack.js';
$fileList[] = 'http://www.ackrentals.net/js/fancybox/jquery.fancybox-1.3.1.js';

$joiner = new FileJoiner($fileList);
$joiner->compression = false;
$joiner->join('js/allscripts.js');
unset($joiner);


?>
Return current item: File Joiner