Archive for the 'php' Category
How to create a zip archive using PHP
by Justin Silverton
The following is a library that allows you to generate zip file archives using php.
<?php
include("ziplib.php");
$zipfile = new Ziplib;
$zipfile->zl_add_file("This is a test file","path/to/file","g9");
//You can stream the ZIP file or write it in a file on your server
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"testfile.zip\"");
echo $zipfile->zl_pack("zip file comments");
?>
This script will dynamically create a zip archive using the files specified with zl_add_file and output it to the browser (the final zip file will be named: testfile.zip).
Options
zl_add_file allows you to specify the compression level of the file file that will be added to the archive.
- n (none)
- b (bzip)
- g (gzip)
Download
The php zip library can be downloaded Here
11 commentsHow to create Microsoft Office Documents with PHP
By Justin Silverton
There are two main ways to build Excel, Word, and PowerPoint documents using PHP. The first is by using the COM library (only if you are using a Windows server) and the other is by using a more standardized approach such as HTML or CSV.
Dynamically creating a word document:
<?php
$word = new COM("word.application");
$word->Visible = 0;
$word->Documents->Add();
$word->Selection->PageSetup->LeftMargin = '2"';
$word->Selection->PageSetup->RightMargin = '2"';
//Setup the font
$word->Selection->Font->Name = 'Verdana';
$word->Selection->Font->Size = 8;
//Write some text
$word->Selection->TypeText("This is a test document");
//Save the document as DOC file
$word->Documents[1]->SaveAs("c:\\docs\\test1.doc");
//quit and release COM resources
$word->quit();
$word->Release();
$word = null;
?>
Dynamically creating an excel document
<?php
$excel = new COM("excel.application");
$excel->Visible = 0;
//Create a new workbook
$wkb = $excel->Workbooks->Add();
$sheet = $wkb->Worksheets(1);
//This code adds the text 'myvalue' on row 2, column 4
$sheet->activate;
$cell = $sheet->Cells(2,4);
$cell->Activate;
$cell->value = 'myvalue';
$wkb->SaveAs("C:\docs\test.xls");
//close and free resources
$wkb->Close(false);
$excel->Workbooks->Close();
$excel->Quit();
?>
Dynamically creating a powerpoint presentation
<?php
$powerpnt = new COM("powerpoint.application");
//Creating a new presentation
$pres=$powerpnt->Presentations->Add();
//Adds the first slide. "12" means blank slide
$pres->Slides->Add(1,12);
//Adds another slide. "10" means a slide with a clipart and text
$pres->Slides->Add(2,10);
//Adds a textbox
$pres->Slides[1]->Shapes->AddTextbox(1,20,50,300,40);
//Save the document as PPT file
$powerpnt->Presentations[1]->SaveAs("C:\Docs\test1.ppt");
//free resources and quit powerpoint
$powerpnt->quit();
?>
How to find Word, Excel, and Powerpoint functions
The following will show you all of the functions that are possible when accessing Microsoft Office components through php:

- Open Microsoft Word, Excel, or Powerpoint
- Press Alt+F11 to start the Visual Basic Editor
- Press F2
- Find “ThisDocument” on the left. In the right frame you’ll see the available variables and functions that can be used with the COM object.
How to compile php scripts in ASP.net
By Justin Silverton
Phalanger is a new PHP implementation introducing the PHP language into the family of compiled .NET languages. It provides PHP applications an execution environment that is fast and extremely compatible with the vast array of existing PHP code. Phalanger gives web-application developers the ability to benefit from both the ease-of-use and effectiveness of the PHP language and the power and richness of the .NET platform taking profit from the best from both sides.
Using .net classes
Phalanger supports full interoperability with .NET. This means that you can access almost any .NET classes (written in C#, VB.NET and other managed languages) from your PHP applications. This requires adding several features to the PHP language that allows you to use .NET features like namespace (which are used to organize .NET classes) and generics (used for specifying type parameters of methods and classes). These language extensions are called PHP/CLR and are designed to retain dynamic PHP behavior (for more details see PHP/CLR Language Extensions).
Thanks to the PHP/CLR extensions you can easilly integrate existing PHP and ASP.NET applications, or use classes available for .NET Framework in your PHP application. This gives you for example the possibility to modify open-source PHP applications to use the standard ASP.NET 2.0 Membership (user management) system, which is very powerfull option for integrating web applications.
You can also develop new applications using PHP with the PHP/CLR language extensions and combine PHP and other .NET languages (for example C#) in one project. This gives you the possibility to leverage of the C# strictness in the application logic layer where the safety and strict object orientation is important, but use the simplicity and efficiency of PHP language for developing the presentation layer.
Features
- compiles PHP to the MSIL (Microsoft Intermediate Language), which is byte-code assembly used by the .NET CLR.
- Improves execution speed (because of just-in-time compilation)
- use any .NET object in a PHP application
- Visual studio integration: supports syntax highlighting for PHP source files and debugging
Benchmarks
How much faster is it than the standard version of PHP?

Download
The latest version of Phalanger can be found Here
4 comments5 cool things you can do with windows and php
by Justin Silverton
Many PHP examples out there are designed for a linux/unix operating system. I am going to give some examples of some interesting functionality that only works with php running in a windows environment (IIS or apache).
1) Eject the CD-ROM
//create an instance of Windows Media Player
$mp = new COM("WMPlayer.OCX");
//ejects the first cd-rom on the drive list
$mp->cdromcollection->item(0)->eject();
2) Read and write from/to the registry
function registry_read($folder, $key)
{
$WshShell = new COM("WScript.Shell");
$registry = "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $folder . "\\" . $key;
$result = $WshShell->RegRead($registry);
return($result);
}
$key = registry_read("RegisteredApplications","Firefox");
parameters:
- Folder name - (key path past HKEY_LOCAL_MACHINE\SOFTWARE\\)
- key - the key name to read from
function registry_write($folder, $key, $value,$type="REG_SZ")
{
$WshShell = new COM("WScript.Shell");
$registry = "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $folder . "\\" . $key;
$result = $WshShell->RegRead($registry);
$result = $WshShell->RegWrite($registry,$value, $type);
return($result);
}
parameters:
- Folder name - (key path past HKEY_LOCAL_MACHINE\SOFTWARE\\)
- key - the key name to write to
- value - value that will be written to the key
- type - key type (default: REG_SZ)
3) register and un-register phpscripts as a windows service
# registering a service
win32_create_service(array(
’service’ => ‘myservice’, # the name of your service
‘display’ => ’sample dummy PHP service’, # description
‘params’ => ‘c:\path\to\script.php run’, # path to the script and parameters
));
# un-registering a service
win32_delete_service(’myservice’);
# code run as a service
if ($argv[1] == 'run') {
win32_start_service_ctrl_dispatcher('myservice');
while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
# write script here
# as a general rule, keep it below 30 seconds through each loop iteration
}
}
This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:
- Download the main library (it’s included in the main PECL extension download from php.net) here
- extract php_win32service.dll to your ext directory (where your php extension .dlls are located)
- add the following line to your php.ini: extension=php_win32service.dll
4) print pages/data
#this is an example function that will format a host/printer name, for printing to shared printers over the network
function getPrinter($host,$SharedPrinterName) {
return “\\\\”.$host.”\\”.$SharedPrinterName;
}
#this opens the printer
$handle = printer_open(getPrinter(”my computer 2″,”my printer”));
An extensive list of functions for printing can be found here
#this is possible in *nix as well. Here is some example code
function lpr($string,$printer) {
$prn=(isset($printer) && strlen($printer))?”$printer”:C_DEFAULTPRN ;
$CMDLINE=”lpr -P $printer “;
$pipe=popen(”$CMDLINE” , ‘w’ );
if (!$pipe) {print “pipe failed.”; return “”; }
fputs($pipe,$string);
pclose($pipe);
}
This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:
- Download the main library (it’s included in the main PECL extension download from php.net) here
- extract php_printer.dll to your ext directory (where your php extension .dlls are located)
- add the following line to your php.ini: extension=php_printer.dll
5) List the current system processes
# list all the current processes running on the system
print_r(win32_ps_list_procs());
other related commands:
# Retrieves statistics about the global memory utilization
print_r(win32_ps_stat_mem());
# Retrieves statistics about the process with the process id pid (if no process id is given, the current process will be used)
print_r(win32_ps_stat_proc(int processid));
This uses the windows API Service DLL, which is not enabled by default. Here is how to install it:
- Download the main library (it’s included in the main PECL extension download from php.net) here
- extract php_win32ps.dll to your ext directory (where your php extension .dlls are located)
- add the following line to your php.ini: extension=php_win32ps.dll
The evolution of Delphi for PHP
By Justin Silverton
Recently, CodeGear announced the latest addition to their product line called Delphi for PHP. It is a great new tool that allows developers to easily create php applications with a drag-and-drop GUI interface. What many people do not know is that a few of the components are actually based on open source projects. I feel that this is a a good thing, because many of these projects have been in use for awhile, thoroughly bug tested, and full of many great features.
Here is a list of the open source projects:
1) Qooxdoo framework

Qooxdoo is the basis for many of the VCL controls. And not all of the properties of the qooxdoo controls that are used are available to the IDE Object Inspector. Learning the API can help you discover the other properties that you can set using JavaScript.
information about the API can be found here
2) VCL4PHP
This project is a class library of PHP components which replicate VCL for Win32. VCL for PHP is PHP framework to develop web applications, it’s OpenSource and it’s licensed using LGPL. It is the basis for the majority of the libraries within Delphi for PHP.
Features
- Fully OO applications
- Complete MVC model
- Properties, Methods and Events>
- I18N functionality
- Template Engines
- Ajax abstraction
- Database abstraction and data-aware components
- Input filtering using objects
- Webservices integration
Project available here
3) walterzorn vector graphics library
This JavaScript VectorGraphics library provides graphics capabilities for JavaScript: functions to draw circles, ellipses (ovals), oblique lines, polylines and polygons (for instance triangles, rectangles) dynamically into a webpage. Usage of this Vector Graphics library should be easy even if you don’t have JavaScript experience. Another goal during development of this JavaScript Draw Shapes Vector Graphics Library was to achieve optimized performance and cleanly arranged pixel stair-step patterns (pixel-optimization).
This library is available for download here
a nice animation example here
4) xajax - ajax library
The xajax PHP object generates JavaScript wrapper functions for the PHP functions you want to be able to call asynchronously from your application. When called, these wrapper functions use JavaScript’s XMLHttpRequest object to asynchronously communicate with the xajax object on the server which calls the corresponding PHP functions. Upon completion, an xajax XML response is returned from the PHP functions, which xajax passes back to the application. The XML response contains instructions and data that are parsed by xajax’s JavaScript message pump and used to update the content of your application.
Features
- xajax’s unique XML response / javascript message-pump system does the work for you, automatically handling the data returned from your functions and updating your content or state according to the instructions you return from your PHP functions. Because xajax does the work, you don’t have to write javascript callback handler functions.
- xajax is object oriented to maintain tighter relationships between the code and data, and to keep the xajax code separate from other code. Because it is object oriented, you can add your own custom functionality to xajax by extending the xajaxResponse class and using the addScript() method.
- xajax works in Firefox, Mozilla, probably other Mozilla based browsers, Internet Explorer, and Safari.
- In addition to updating element values and innerHTML, xajax can be used to update styles, css classes, checkbox and radio button selection, or nearly any other element attribute.
- xajax supports passing single and multidimensional arrays and associative arrays from javascript to PHP as parameters to your xajax functions. Additionally, if you pass a javascript object into an xajax function, the PHP function will receive an associative array representing the properties of the object.
- xajax provides easy asynchronous Form processing. By using the xajax.getFormValues() javascript method, you can easily submit an array representing the values in a form as a parameter to a xajax asynchronous function:
xajax_processForm(xajax.getFormValues(’formId’);
. It even works with complex input names like “checkbox[][]” and “name[first]” to produce multidimensional and associative arrays, just as if you had submitted the form and used the PHP $_GET array
Non-open source software worth mentioning
Qstudio

CodeGear partnered with qadram software to jointly develop Delphi for PHP. qadram software, the development team behind VCL for PHP, had been working on a pre-beta project called Qstudio that became the foundation of Delphi for PHP.Development of Delphi for PHP is being done by CodeGear and qadram software at CodeGear headquarters in Scotts Valley, CA.
Interested in Delphi for PHP? a free, 14-day trial (as opposed to the 1-day trial when it was first released) can be found here
No comments




