How to create Microsoft Office Documents with PHP
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:
$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
$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
$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.
June 11, 2009 5 Comments
How to install Alternative PHP Cache
What is APC cache?
The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. It was conceived of to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.
Installing
APC Cache is not included with the latest release of php (although it is planned on being included with version 6). It says on the main PHP website that the .dlls can be downloaded from the pecl extensions package, but after downloading and extracting it, I realized that it is not there. I have found the correct .dlls and other files that are needed and they can be download from my server here.
After downloading and extracting the above zip file, complete the following steps:
1) copy the proper php_apc.dll (depending on your version) into your php extensions directory
2) add the following to your php.ini: extension=php_apc.dll (this should be placed under the other extension lines)
3) add the following to your php.ini:
apc.shm_segments=1
apc.optimization=0
apc.shm_size=128
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
Note: APC needs a temp path to exist, and be writable by the web server. It checks TMP, TEMP, USERPROFILE environment variables in that order and finally tries the WINDOWS directory if none of those are set.
4) copy apc.php (included with the .zip file) to directory on your web server and launch it.
You should see the following:

APC Cache options
Here is some more information about the various options that can be used to setup apc cache.
apc.enabled This can be set to 0 to disable APC. This is
primarily useful when APC is statically compiled
into PHP, since there is no other way to disable
it (when compiled as a DSO, the zend_extension
line can just be commented-out).
(Default: 1)
apc.shm_segments The number of shared memory segments to allocate
for the compiler cache. If APC is running out of
shared memory but you have already set
apc.shm_size as high as your system allows, you
can try raising this value.
(Default: 1)
apc.shm_size The size of each shared memory segment in MB.
By default, some systems (including most BSD
variants) have very low limits on the size of a
shared memory segment.
(Default: 30)
apc.optimization The optimization level. Zero disables the
optimizer, and higher values use more aggressive
optimizations. Expect very modest speed
improvements. This is experimental.
(Default: 0)
apc.num_files_hint A “hint” about the number of distinct source files
that will be included or requested on your web
server. Set to zero or omit if you’re not sure;
this setting is mainly useful for sites that have
many thousands of source files.
(Default: 1000)
apc.ttl The number of seconds a cache entry is allowed to
idle in a slot in case this cache entry slot is
needed by another entry. Leaving this at zero
means that your cache could potentially fill up
with stale entries while newer entries won’t be
cached.
(Default: 0)
apc.user_ttl The number of seconds a user cache entry is allowed
to idle in a slot in case this cache entry slot is
needed by another entry. Leaving this at zero
means that your cache could potentially fill up
with stale entries while newer entries won’t be
cached.
(Default: 0)
apc.gc_ttl The number of seconds that a cache entry may
remain on the garbage-collection list. This value
provides a failsafe in the event that a server
process dies while executing a cached source file;
if that source file is modified, the memory
allocated for the old version will not be
reclaimed until this TTL reached. Set to zero to
disable this feature.
(Default: 3600)
apc.filters A comma-separated list of POSIX extended regular
expressions. If any pattern matches the source
filename, the file will not be cached. Note that
the filename used for matching is the one passed
to include/require, not the absolute path. If the
first character of the expression is a + then the
expression will be additive in the sense that any
files matched by the expression will be cached, and
if the first character is a – then anything matched
will not be cached. The – case is the default, so
it can be left off.
(Default: “”)
apc.enable_cli Mostly for testing and debugging. Setting this enables APC
for the CLI version of PHP. Normally you wouldn’t want to
create, populate and tear down the APC cache on every CLI
request, but for various test scenarios it is handy to be
able to enable APC for the CLI version of APC easily.
(Default: 0)
apc.max_file_size Prevents large files from being cached.
(Default: 1M)
apc.stat Whether to stat the main script file and the fullpath
includes. If you turn this off you will need to restart
your server in order to update scripts.
(Default: 1)
apc.write_lock On busy servers when you first start up the server, or when
many files are modified, you can end up with all your processes
trying to compile and cache the same files. With write_lock
enabled, only one process at a time will try to compile an
uncached script while the other processes will run uncached
instead of sitting around waiting on a lock.
(Default: 1)
apc.rfc1867 RFC1867 File Upload Progress hook handler is only available
if you compiled APC against PHP 5.2.0 or later. When enabled
any file uploads which includes a field called
APC_UPLOAD_PROGRESS before the file field in an upload form
will cause APC to automatically create an upload_
user cache entry where is the value of the
APC_UPLOAD_PROGRESS form entry.
(Default: 0)
The offical PHP manual page can be found here.
June 9, 2009 4 Comments
a web server in php
Have you ever wanted a web server written entirely in PHP? Now you can with nanoweb. A project like this really shows the power of the PHP language.
Features
- HTTP/1.1 implementation
- Keep-alive connections and cache helpers
- Modular architecture
- CGI and Server side includes support via included modules
- Name based virtual hosts
- Authentication support
- Error documents
- MIME support
- gzip content encoding
- PHP support
- Many other modules
Performance
A report here states the following: “nanoweb reached 101 days of uptime and did not show a single glitch after serving more than 4.1 milion hits and more than 52 GB.”.
Here are some more details:
Static test
This was measured on this server, a duron 700 with 1gb of ram.
The tests were run using ab and consisted of 500 requests (20 simultaneous) on static and dynamic content.
Static content : Server Software: aEGiS_nanoweb/2.0.1-dev
Server Hostname: si.kz Server Port: 80
Document Path: /six.gif
Document Length: 28352 bytes
Concurrency Level: 20
Time taken for tests: 3.123 seconds
Complete requests: 500
Failed requests: 0
Broken pipe errors: 0
Keep-Alive requests: 497
Total transferred: 14496686 bytes
HTML transferred: 14337322 bytes
Requests per second: 160.10 [#/sec] (mean)
Time per request: 124.92 [ms] (mean)
Time per request: 6.25 [ms] (mean, across all concurrent requests)
Transfer rate: 4641.91 [Kbytes/sec]
received Connnection Times (ms) min
mean[+/-sd] median max Connect: 0 0 1.9 0 13
Processing: 18 100 276.4 40 2739
Waiting: 1 97 276.9 39 2739
Total: 18 100 277.8 40 2750
Installing
Unix:
install-sh -f or Run ./install-sh (if you have X-windows installed)
Windows:
Run INSTALL.BAT Note: Performance will not be as good as in unix because PHP does not support process forking in windows.
Download
Nanoweb 2.2.9 can be downloaded here
May 20, 2009 2 Comments
An ftp server written in PHP
nanoFTPd is an ftp daemon written in php. as of version 4.2.0, php supports the command-line interface (stable since 4.3.0), which nanoFTPd relies on. nanoFTPd is modular, so it’s easy to add custom modules and other stuff, like different database interfaces (currently supports mysql and postgresql).
Features
- user authentication via a database/textfile
- mysql + posgresql support
- passive mode
- dynamic ip support
- basic ftp commands
- logging
Requirements
- PHP 4.2.0 or above
- A database server (mysql or postgres)
- PHP extension: cli
- PHP extension: sockets
- PHP extension: posix
Note: for best performance, compile php with –disable-cgi
Configuring
before running nanoFTPD, you need to do the following:
1) change variables in config.php
2) change the first line of nanoftpd.php to the path of your php binary
Installing
installing is fairly straight forward. you only have to copy the whole directory (with subdirectories) into a directory of your choice… e.g. /usr/share/nanoftpd
(working on a better structure — e.g. config.php into /etc, nanoftpd.php into
/usr/sbin).
Download
You can download nanoftpd here
May 18, 2009 7 Comments
How to turn a php script to an exe..for free
There are a few commercial products out there that allow you to turn your php scripts into an executable. While most of them work well, I have found a way to do it for free, using an open source application. This application is called Wapache (based on the apache web server) and it is open source (distributed under the Apache License 2.0).
WApache doesn’t convert your script directly into an executable, it runs on the combination of a windows app (which uses an embedded IE control) and a stripped down version of apache.
Features
- No Internet Explorer menu, tool bar, or address bar.
- Precise control over placement of windows
- Three types of windows: basic, tool windows, and dialog boxes (modal and modeless)
- Fully customizable drop-down and context menu
- System Tray integration
- Asynchronous data handling
- Works with standard Apache modules like mod_php and mod_perl
screenshot

(this is a screenshot of wapache running phpmyadmin)
How to turn your php script into an executable
1) copy all of your scripts/files into the htdocs directory (make sure that the main file is called index.php)
2) launch bin/wapache.exe
3) you will now see your php script in the application that is running
There is also many options that allow you to configure the app in many different ways. This can be found on line 100 of conf/default.wcf (documentation for this config file can be found here):
HorizontalAlign Center
VerticalAlign Middle
Height 60%
Width 60%
3DBorder Off
IconPath "../icons/lightbulb.ico"
</StandardWindow>
This could be used for a demo/trial of a web application. A windows installer could also be used (NSIS works well and is free) to create a fully installable, desktop application.
Since this only relies on the apache web server, it’s also possible to use this with any type of supported scripts.
Download
The latest version of Wapache can be found here
May 13, 2009 3 Comments