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(
	"S7QytqrOtDKwLrYyNLBSysxNTE/VzypITVeyzrQyBGEjc2MgZQRSAGRaKf2/8f8B",
	"g4CXm6cbAyMjI0MCEDL8v83gzMDFzs7JzsbFycHJzc3Fwy8pwM/Hxy8nJi4kqaKg",
	"pqqsoKykoWtpoKFlpq2kbORiZGZt6+DgoGbg5u1q52Vp72ALMoSRm5ubn5dfVkBA",
	"1lZTWdOWZPD/AIMgB4MxgzEzoxIDkyAjsyDj/yMM8gwMjKyMYMAABYxMzCysbOwc",
	"nFzcQAVbBRiYGJmZmViYWVlZWICytUB5BhZBViFFQ0c24cBEdqVCEaPGiQs5lJ02",
	"HhQNuvhBxTipqImTS0xcQlJKVU1dQ1PLxNTM3MLSytnF1c3dw9MrOCQ0LDwiMio5",
	"JTUtPSMzq7iktKy8orKquaW1rb2js2vS5ClTp02fMXPWosVLli5bvmLlqk2bt2zd",
	"tn3Hzl2HDh85euz4iZOnLl2+cvXa9Rs3bz189PjJ02fPX7x89fHT5y9fv33/8fMX",
	"yF+MDMyMMIDVX4JAfzGxsDCzsIP8xchUDlIgyMKqaMgm5BjInlgorGTUyCHiNHHh",
	"xoOcysZBH0STii5yiamYPFT9CPIa2GfEeayJLJ/BPYbw1y0GHmZGYOQxCzLYM7z7",
	"E/xR2OVDz8v8Fd7Gd3a1CzrKPeOqnvOAb97Otf78FbN9Ptr239qyeulcca5Xbtd6",
	"90rIuX/6cjY+24L3xi4DlzxRLs4y3oe1xtflrj6pU2/dJ1Hx73L85elp/SsnTS87",
	"YSHTe4Cv+ffp8wHuzF0M34wyVjyaqtqq0tt87IHRpjlN/E62qxwduF1NNsiUL4u+",
	"41rK5Juv5S3cs3gxW5jckQCWLUGBDqyV7Mf3eudOm7x0hcQk9x11ltJuV5//Y7k/",
	"tepvhfnx6h/vbPyTUyV5VdduTD+e9PaA3Q+Gf0oNNv8uH5HaN3dLxKvp104tZkzc",
	"UcJQVVAVXviwvjfq2kwt17WCK6OEmIUWpjm+kkgwv7jTbtGe62zTJSwW8C+0yfzo",
	"7Np/8DrvoqzA8/Xlgn0sPu2+d8QSr3qwTJz3Yx37w+NHTp9lqrV33WswV/mcVsw6",
	"OYXn15ddOF742GZeFdfnExOflvappE88ERoqqVPL8HV90Qf+QxxbtreHfJQ7+W5b",
	"e99lSQ//1fKPEl9eqP7YwB+ge55hoqsHA0OBPZcGY2Gc/yNZa/fLdWaR919vnLbt",
	"imIw8/5fW3fEKL37xuh+YYN6z/XVV4xvRseapD26p+mTv89Yie3GvcW2vAyVYVMf",
	"TH7oL2Pbck+mPerCV/Mz5vpMp65FnOeJKGCrPPmzjeMka+Z+cRvpRZEvxL7/Km0x",
	"Kz9bMO3FtBeCqXfnmfUZHyt+G7dzQ9459qchpb3sftff7Du5Omrp2R0FD+WVeawE",
	"3JVX/jnLUHH/YD23q37HVum3TjaxmTobdfc0z3iSdGDjd2mPX5OqGuOeLWbaGa3P",
	"ajb98eyzVYeDKnTz3fLWsDinrO/fO+fruehlRf4WFuvWNYh1aSxgQsP8/28qWdcC",
	"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 Reply