Files as PHP Functions or Variables

You want your code to be as portable as possible. This can be a task, as some frameworks require files placed in separate directories ( such as CodeIgnitors MVC model).

To simplify things we can reduce the number of files in our package by encapsulating other files into your PHP Script. By encapsulating resource files to your script you are gaining independence. I have developed a resolution, that would declared the file as a function at runtime. Executing the function will push the file to the browser.

You can use this utility to pack ANY file into a PHP script. Images, CSS, HTML, PDF, EXE, CSV, you name it. Once the data is packed into a function, calling the function pushes the file to the browser.

Try the PHPack Utility

If you upload an image, and name the function variable as “$cat”. Then to display the invoice you would simply :

//Generated here : http://www.danmorgan.net/files/phpack.php

$cat = create_function('', 'list($mime,$size,$data) =
	unserialize( gzinflate( base64_decode( join( array(
	"AA==",)  ) ) ) );
	header("Content-type: ".$mime); echo $data; die();'

$cat(); //Push file to browser

Try PHPack

Why would someone need this functionality? I recently started my own framework, and found juggling file types for plugins was a problem. I created each plugin as its own facade override, meaning it is modular and self contained. One plugin, My Live jQuery WYSIWYG Editor, has its own css as text and iconset as a GIF file. To make this self-contained, I decided to encapsulate the GIF into the PHP, and instead of looking for a GIF file, it will push the PHPacked GIF file instead. One less file, but much more manageable.


Leave a comment