<?xml version="1.0"?>
<rss version="2.0"><channel><title>Devplace</title><link>http://www.devplace.nl/blog</link><description>Webdevelopment blog by Joris van de Sande</description><lastBuildDate>Sat, 11 Aug 2012 16:06:05 +0200</lastBuildDate><generator>Habari 0.8 http://habariproject.org/</generator><item><title>Creating a Debian / Ubuntu package for a PHP PECL extension</title><link>http://www.devplace.nl/blog/creating-a-debian-ubuntu-package-for-a-php-pecl-extension</link><description>Today I wanted to use the &lt;a href="http://www.php.net/manual/en/book.inotify.php"&gt;Inotify&lt;/a&gt; functions that are supplied by the &lt;a href="http://pecl.php.net/package/inotify"&gt;PECL Package Inotify&lt;/a&gt;. It turns out that this PHP extension is not available in the (default) Debian or Ubuntu repositories. Instead of using the PECL command to install the extension, I wanted to create a native package that I could use to install/uninstall the package on multiple servers. This blogpost describes how I did this:&#xD;
&#xD;
&#xD;
&lt;h3&gt;1. Downloading and unpacking the extension&lt;/h3&gt;&#xD;
Note that I will create a package for the Inotify extension in this blogpost, but this guide can be applied to any PHP (PECL) extension.&#xD;
&#xD;
&lt;ul&gt;&#xD;
  &lt;li&gt;&#xD;
    Create a working directory:&#xD;
    &lt;pre class="brush: bash"&gt;&#xD;
mkdir php-notify&#xD;
cd php-notify&#xD;
    &lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;Download the extension: &lt;pre class="brush: bash"&gt;wget 'http://pecl.php.net/get/inotify-0.1.6.tgz'&lt;/pre&gt;&lt;/li&gt;&#xD;
  &lt;li&gt;Unpack the extension: &lt;pre class="brush: bash"&gt;tar -zxvf inotify-0.1.6.tgz&lt;/pre&gt;&lt;/li&gt;&#xD;
  &lt;li&gt;The current directory should now contain the extension archive (inotify-0.1.6.tgz) a package.xml file and a directory containing the source of the extension (i.e. inotify-0.1.6). You can verify this with: &lt;pre class="brush: bash"&gt;ls -las&lt;/pre&gt;&lt;/li&gt;&#xD;
&lt;/ul&gt;&#xD;
&#xD;
&#xD;
&lt;h3&gt;2. Preparing the extension for packaging&lt;/h3&gt;&#xD;
The source code should be in place. Now it's time to prepare the extension for packaging.&#xD;
&lt;ul&gt;&#xD;
  &lt;li&gt;&#xD;
    Make sure you have the needed tools installed to build the extension:&#xD;
    &lt;pre class="brush: bash"&gt;&#xD;
sudo aptitude update&#xD;
sudo aptitude install build-essential checkinstall&#xD;
&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
  Change directory to the extension source directory:&#xD;
  &lt;pre class="brush: bash"&gt;cd inotify-0.1.6&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
     Run phpize to prepare the build environment for the extension:&#xD;
    &lt;pre class="brush: bash"&gt;phpize&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    Configure, compile and test the extension:&#xD;
    &lt;pre class="brush: bash"&gt;&#xD;
./configure&#xD;
make&#xD;
make test&#xD;
&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
&lt;/ul&gt;&#xD;
&#xD;
These steps are also documented in the PHP manual at &lt;a href="http://php.net/manual/en/install.pecl.phpize.php"&gt;Compiling shared PECL extensions with phpize&lt;/a&gt;.&#xD;
&#xD;
&#xD;
&lt;h3 id="creating-a-package"&gt;3. Creating a package&lt;/h3&gt;&#xD;
Now that we have compiled the extension, lets create a package for it! Creating a package is really easy now, all you have to do is issue the checkinstall command, the --install=no parameter prevents the package from being installed immediately. You should run this command as root, because otherwise files could end up having the wrong permissions.&#xD;
&#xD;
&lt;pre class="brush: bash"&gt;&#xD;
sudo checkinstall --install=no&#xD;
&lt;/pre&gt;&#xD;
&#xD;
You can answer with "y" to the question about the ./doc-pak directory that does not exist. After that you can input a description for your extension. Then you're given an overview of the package to be build, you can change any of the properties. Change at least the name of the package, I changed it to "php5-inotify-custom". Hit ENTER to build the package now.&#xD;
&#xD;
The name of the created package is printed by the checkinstall script (php5-inotify-custom_0.1.6-1_amd64.deb in my case). Now you can install the package with dpkg:&#xD;
&#xD;
&lt;pre class="brush: bash"&gt;sudo dpkg -i php5-inotify-custom_0.1.6-1_amd64.deb&lt;/pre&gt;&#xD;
&#xD;
After installation you can enable the module by creating a new file "/etc/php5/conf.d/inotify.ini" containing the text "extension=inotify.so" (make sure to restart your webserver if needed). If you don't want to create this file manually after installation of the package, read the &lt;a href="#creating-a-package-that-enables-the-extension"&gt;next chapter&lt;/a&gt;.&#xD;
&#xD;
&lt;pre class="brush: bash"&gt;&#xD;
sudo echo 'extension=inotify.so' &gt; /etc/php5/conf.d/inotify.ini&#xD;
sudo service apache2 reload&#xD;
&lt;/pre&gt;&#xD;
&#xD;
The package can be uninstalled with dpkg too:&#xD;
&lt;pre class="brush: bash"&gt;sudo dpkg -P php5-inotify-custom&lt;/pre&gt;&#xD;
&#xD;
&#xD;
&lt;h3 id="creating-a-package-that-enables-the-extension"&gt;4. Creating a package that enables the extension&lt;/h3&gt;&#xD;
I wanted to create a package that would enable the the new module straight away. This is how I did this:&#xD;
&#xD;
&lt;ul&gt;&#xD;
  &lt;li&gt;I created a config file for inotify.ini in the /etc/php5/conf.d directory. This directory is automatically scanned for (additional) configuration files for PHP in Debian and Ubuntu:&#xD;
&lt;pre class="brush: bash"&gt;&#xD;
sudo echo 'extension=inotify.so' &gt; /etc/php5/conf.d/inotify.ini&#xD;
&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    To include this file in the package, we must create a text file containing the path to this file:&#xD;
     &lt;pre class="brush: bash"&gt;echo '/etc/php5/conf.d/inotify.ini' &gt; checkinstall_files.txt&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    Now run checkinstall like this and follow the steps that are outlined in the &lt;a href="#creating-a-package"&gt;previous chapter&lt;/a&gt;:&#xD;
&lt;pre class="brush: bash"&gt;&#xD;
sudo checkinstall --install=no --include=checkinstall_files.txt&#xD;
sudo rm /etc/php5/conf.d/inotify.ini&#xD;
&lt;/pre&gt;&#xD;
You can skip the last step to enable the module. &#xD;
  &lt;/li&gt;&#xD;
&lt;/ul&gt;&#xD;
&#xD;
&lt;strong&gt;If you run into errors or warnings when installing this package via dpkg, you should read &lt;a href="#checkinstall-configfiles-bug"&gt;Checkinstall configfiles bug&lt;/a&gt; at the bottom of this post.&lt;/strong&gt;&#xD;
&#xD;
&lt;h3&gt;Why did I build a package?&lt;/h3&gt;&#xD;
So why did I build a package instead of installing the extension with the &lt;a href="http://php.net/manual/en/install.pecl.pear.php"&gt;PECL command&lt;/a&gt;? I did this because there are quite a few advantages in creating a (custom) package:&#xD;
&#xD;
&lt;ul&gt;&#xD;
  &lt;li&gt;It can be installed and uninstalled via the system's own package manager (dpkg).&lt;/li&gt;&#xD;
  &lt;li&gt;When you're running your own package repository it's really easy to install the package on multiple servers (via apt-get/aptitude).&lt;/li&gt;&#xD;
  &lt;li&gt;When you're using a tool such as &lt;a href="http://puppetlabs.com/"&gt;Puppet&lt;/a&gt; to manage your servers you can add this to the list of packages that must be installed.&lt;/li&gt;&#xD;
  &lt;li&gt;The system is aware that the package is installed and which version of the package is installed. You can also define the package dependencies for your custom packages.&lt;/li&gt;&#xD;
&lt;/ul&gt;&#xD;
&#xD;
&#xD;
&lt;h3 id="checkinstall-configfiles-bug"&gt;Checkinstall configfiles bug&lt;/h3&gt;&#xD;
After adding the config file (/etc/php5/conf.d/inotify.ini) to the list of files that should be included in the package I started getting warnings when installing (and removing) the package. After some searching, I found a blog entry called &lt;a href="http://muzso.hu/2009/01/14/bug-in-checkinstall-regarding-conffiles"&gt;Bug in checkinstall regarding conffiles&lt;/a&gt;, that explains the warnings and provides a solution for this problem.</description><pubDate>Sat, 11 Aug 2012 16:06:05 +0200</pubDate><guid isPermaLink="false">tag:www.devplace.nl,2012:creating-a-debian-ubuntu-package-for-a-php-pecl-extension/1344685225</guid></item><item><title>Updating Hyper-V drivers after a kernel upgrade on CentOS</title><link>http://www.devplace.nl/blog/updating-hyper-v-drivers-after-kernel-upgrade-on-centos</link><description>A few days ago I was updating a &lt;a href="http://www.centos.org/"&gt;CentOS&lt;/a&gt; machine that happens to be running on &lt;a href="http://www.microsoft.com/windowsserver2008/en/us/hyperv-main.aspx"&gt;Microsoft's Hyper-V platform&lt;/a&gt;. Everything went very smooth. So I tried to reboot the machine, because I wanted to make sure that everything worked fine after a reboot. &#xD;
&#xD;
&lt;h3&gt;The reboot&lt;/h3&gt;&#xD;
&#xD;
Apparently the kernel was updated too and the system did not boot with the new kernel. Since I have no access to the Hyper V environment itself, I needed to call my hosting provider (I only have shell access to the server). The restarted the machine and chose to boot the old kernel (which still worked). This was only a temporary solution because after a reboot the machine would still try to boot the new kernel. &#xD;
&#xD;
&lt;h3&gt;Linux Integration Components&lt;/h3&gt;&#xD;
&#xD;
After some investigation I found out that I needed to install the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c299d675-bb9f-41cf-b5eb-74d0595ccc5c&amp;displaylang=en"&gt;Linux Integration Components&lt;/a&gt; for Hyper-V in the new kernel. It turns out that these integration components must be installed &lt;strong&gt;for each kernel that you want to boot into&lt;/strong&gt;. There is one problem however, the integration components will only be installed in the currently running kernel by default. Since I have no access to the Hyper V environment and the machine has no ethernet connection without the components, I needed to find another way to install the components into the new kernel. I called my hosting provider again and they wanted to help me, but they would charge me around &#x20AC; 90,- for this (simple) task. I decided to find another solution myself.&#xD;
&#xD;
&lt;h3&gt;Installing the Linux Integration Components into a non-running kernel&lt;/h3&gt;&#xD;
&#xD;
There are a &lt;a href="http://www.howtoforge.com/installing-hyper-v-linux-integration-components-on-centos-5"&gt;few&lt;/a&gt; &lt;a href="http://blogs.zdnet.com/perlow/?p=10830"&gt;good&lt;/a&gt; &lt;a href="http://blog.allanglesit.com/Blog/tabid/66/EntryId/36/Hyper-V-Guests-Linux-Integration-Components-on-RHEL-and-CentOS.aspx"&gt;tutorials&lt;/a&gt; for installing the Linux Integration Components in the currently running kernel, but I couldn't find proper documentation. It turns out that the scripts (and makefiles) that install the components make intensive use of the "&lt;strong&gt;uname -r&lt;/strong&gt;" command to find out for which kernel the integration components should be installed. This means taht by default it's not possible to install the components into another kernel. I found the following solution: &#xD;
&#xD;
&lt;ul&gt;&#xD;
  &lt;li&gt;&#xD;
    Create a checkpoint for your Hyper-V machine (if you can), so that you can always restore the machine to the current state in case something goes wrong.&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;Find the kernel that you want to boot in by listing the installed kernels.&#xD;
     &lt;pre class="brush: bash"&gt;rpm -q kernel&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;In my case I got the following output:&lt;pre class="brush: bash"&gt;&#xD;
kernel-2.6.18-128.el5&#xD;
kernel-2.6.18-128.7.1.el5&#xD;
kernel-2.6.18-164.9.1.el5&#xD;
&lt;/pre&gt;&#xD;
In my case the running kernel was "2.6.18-128.7.1.el5" and I wanted to boot into the newest kernel "2.6.18-164.9.1.el5". &#xD;
&lt;/li&gt;&#xD;
  &lt;li&gt;Backup your linux integration components setup directory:&#xD;
     &lt;pre class="brush: bash"&gt;cp -R Linux_IC_V2 Linux_IC_V2-backup&lt;/pre&gt;&#xD;
    And change directory to the Linux integration components setup directory (in my case the components are installed in /root/Linux_IC_V2):&#xD;
    &lt;pre class="brush: bash"&gt;cd Linux_IC_V2&lt;/pre&gt;&#xD;
&lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
  &lt;li&gt;&#xD;
    The setup script assumes that you've already booted the new kernel when you are installing the new drivers.&#xD;
    I think that it would be a lot easier if the kernel version could be passed as an argument to the script. &#xD;
    &#xD;
    But this is not the case so we need to adjust some files in the setup directory, you can find out which files need to be changed by using grep:&#xD;
&#xD;
    &lt;pre class="brush: bash"&gt;grep -iR "uname -r" *&lt;/pre&gt;&#xD;
    &#xD;
    In my case the following files where found:&#xD;
    &lt;pre class="brush: bash"&gt;&#xD;
drivers/dist/blkvsc/Makefile&#xD;
drivers/dist/blkvsc/Makefile&#xD;
drivers/dist/vmbus/Makefile&#xD;
drivers/dist/vmbus/Makefile&#xD;
drivers/dist/tools/scripts/updategrub.pl&#xD;
drivers/dist/tools/scripts/updateinitrd.p&#xD;
drivers/dist/Makefile&#xD;
drivers/dist/Makefile&#xD;
drivers/dist/Makefile&#xD;
drivers/dist/storvsc/Makefile&#xD;
drivers/dist/storvsc/Makefile&#xD;
drivers/dist/netvsc/Makefile&#xD;
drivers/dist/netvsc/Makefile&#xD;
drivers/dist/makefile.common&#xD;
setup.pl&#xD;
&lt;/pre&gt;&#xD;
&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    Now we need to edit all those files (you can use your favorite editor) and make sure that we replace "uname -r" with the new kernel version. So in my case I replaced&#xD;
&#xD;
   &lt;pre class="brush: text"&gt;shell uname -r&lt;/pre&gt;&#xD;
   with:&#xD;
   &lt;pre class="brush: text"&gt;shell echo "2.6.18-164.9.1.el5"&lt;/pre&gt;&#xD;
    in the Makefiles. &#xD;
&#xD;
    And in the perl scripts you can replace &lt;pre class="brush: text"&gt;`uname -r`&lt;/pre&gt; with: &lt;pre class="brush: text"&gt;"2.6.18-164.9.1.el5"&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    Now it is time to install the integration components in the kernel:&#xD;
    &lt;pre class="brush: bash"&gt;./setup.pl drivers&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    If the previous step was successful, we can check /boot/grub/menu.lst to make sure that we are booting the new kernel on reboot. &#xD;
  &lt;/li&gt;&#xD;
   &lt;li&gt;&#xD;
     We should now be able to boot into the new kernel! &#xD;
    &lt;pre class="brush: bash"&gt;reboot&lt;/pre&gt;&#xD;
  &lt;/li&gt;&#xD;
&lt;/ul&gt;&#xD;
&#xD;
I hope this post was useful for you, because I have spent quite some hours tackling this specific problem. I have learned a lot about the Hyper-V platform in those hours though...</description><pubDate>Sat, 26 Dec 2009 10:54:52 +0100</pubDate><guid isPermaLink="false">tag:www.devplace.nl,2009:updating-hyper-v-drivers-after-kernel-upgrade-on-centos/1261818044</guid></item><item><title>Contributing to the PHP manual - an introduction</title><link>http://www.devplace.nl/blog/contributing-to-the-php-manual</link><description>There are many ways to contribute to the &lt;a href="http://www.php.net"&gt;PHP&lt;/a&gt; programming language. You can contribute by writing code, reporting bugs, helping other users on the &lt;a href="http://news.php.net"&gt;mailing lists&lt;/a&gt;, writing documentation etc. This article is a guide to start documenting for PHP. I think that one of the reasons that PHP is so widely used is because of the quality and completeness of the &lt;a href="http://www.php.net/manual/"&gt;PHP manual&lt;/a&gt;. &#xD;
&#xD;
The documentation team can't keep up with the code that is being written for PHP, so a lot of functionality that is available in PHP (or its extensions) is still undocumented. This is especially true for functions and modules that were added to PHP in the last couple of years. I think that it is important to create proper documentation, because otherwise the new functionality will only be used by experienced PHP developers that are aware of the existence of the undocumented functionality.&#xD;
&#xD;
&lt;h3&gt;How to start?&lt;/h3&gt;&#xD;
&#xD;
To start writing documentation you should think about what PHP functions, classes or extensions that you want to write documentation for. I think one can have several motives to start writing documentation for PHP. This list might help you decide what you want to document about, but is by no means complete.&#xD;
&#xD;
&lt;ul&gt;&#xD;
  &lt;li&gt;&#xD;
     You've used an extension in PHP that you've gotten pretty familiar with, but the documentation of this extension is incomplete.&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;You've encountered one or more bugs in the current documentation that you want to correct.&#xD;
  &lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    You want to contribute to PHP because you use PHP a lot, but you are not comfortable with programming in C. In this case the pages &lt;a href="http://doc.php.net/php/undoc_functions.php"&gt;undocumented functions in the PHP manual&lt;/a&gt; or &lt;a href="http://doc.php.net/php/missing_examples.php"&gt;missing examples in the PHP manual&lt;/a&gt; are a good place to start.&#xD;
  &lt;/li&gt;&#xD;
&lt;/ul&gt;&#xD;
&#xD;
After you've decided what you want to document, it's time to read the &lt;a href="http://wiki.php.net/doc/howto"&gt;PHP documentation howto&lt;/a&gt;. This howto is not a definitive guide to start writing documentation for PHP, but it should be sufficient to get you started. The documentation team has switched to SVN a few months ago so there might be some references to CVS in the howto. &#xD;
&#xD;
After you've read the basic principles of the PHP documentation process, you can introduce yourself on the &lt;a href="http://news.php.net/php.doc"&gt;phpdoc mailinglist&lt;/a&gt; and start submitting patches to this list. &#xD;
&#xD;
&lt;h3&gt;Karma&lt;/h3&gt;&#xD;
&#xD;
You need to earn some karma before you will receive direct write access to the phpdoc SVN Repository. To earn karma you can join the discussions on the phpdoc mailinglist and start submitting patches to the documentation on the list. After you've earned some karma you can apply for a &lt;a href="http://www.php.net/svn-php.php"&gt;SVN account&lt;/a&gt;.&#xD;
&#xD;
&lt;h3&gt;Translating the manual&lt;/h3&gt;&#xD;
&#xD;
The PHP manual is available in many different languages. If you're not a developer, you can still contribute to the PHP documentation by translating the manual into your own language. You can start by posting a message to the &lt;a href="http://news.php.net/php.doc"&gt;phpdoc mailinglist&lt;/a&gt; in which you introduce yourself. It is also possible to subscribe yourself to a language specific mailing list. All documentation related mailing lists start with "php.doc.", you can view the available lists at &lt;a href="http://news.php.net/"&gt;news.php.net&lt;/a&gt;. Additional information can be found in the PHP wiki item "&lt;a href="http://wiki.php.net/doc/howto/translations"&gt;Working with translations&lt;/a&gt;".&#xD;
&#xD;
&lt;h3&gt;What you should know about the PHP Manual&lt;/h3&gt;&#xD;
&#xD;
Note that this information can also be found in the PHP wiki and elsewhere on the net, but I thought it could be useful to summarize it here.&#xD;
&#xD;
&lt;ul&gt;&#xD;
  &lt;li&gt;The PHP manual is written in &lt;a href="http://www.docbook.org/whatis"&gt;docbook&lt;/a&gt; XML format with a few enhancements that are PHP manual specific.&lt;/li&gt;&#xD;
&#xD;
  &lt;li&gt;The PHP documentation source resides in the "phpdoc" SVN module from the &lt;a href="http://svn.php.net"&gt;PHP SVN Repository&lt;/a&gt;. &lt;/li&gt;&#xD;
&#xD;
  &lt;li&gt;&lt;a href="http://wiki.php.net/doc/phd"&gt;PhD&lt;/a&gt; is the PHP tool that converts the docbook xml files to the various output formats of the PHP manual (currently PHP, HTML, PDF, CHM and man pages can be generated). Information about using PhD on windows can be found in an excellent blog post called "&lt;a href="http://elizabethmariesmith.com/2009/02/setting-up-phd-on-windows/"&gt;Setting up PhD on Windows&lt;/a&gt;" by Elizabeth Marie Smith.&lt;/li&gt;&#xD;
&#xD;
  &lt;li&gt;The &lt;a href="http://www.php.net/manual/"&gt;PHP manual&lt;/a&gt; is generated on a weekly base. The version that's used by the documentation team is updated four times a day and can be found at &lt;a href="http://docs.php.net/"&gt;http://docs.php.net/&lt;/a&gt;.&lt;/li&gt;&#xD;
  &lt;li&gt;&#xD;
    If you happen to work for a hosting provider, you can help the PHP Community by &lt;a href="http://php.net/mirroring.php"&gt;mirroring the PHP Manual&lt;/a&gt; for your country.&#xD;
  &lt;/li&gt;&#xD;
&lt;/ul&gt;</description><pubDate>Sun, 08 Nov 2009 16:26:06 +0100</pubDate><guid isPermaLink="false">tag:www.devplace.nl,2009:contributing-to-the-php-manual/1255553880</guid></item><item><title>Storing large values in MySQL fields with PHP</title><link>http://www.devplace.nl/blog/storing-large-values-in-mysql-fields-with-php</link><description>Today a colleague at work ran into a problem when trying to store file contents into a MySQL database table. We are using this solution for quite some time now and it has always worked pretty good for documents and files up to 15 MB. The main reason for us to put files in the database is that it is easier to migrate a website to another server because the database contains all the data that we need. &#xD;
&#xD;
The problem appeared at a customers' webserver that is running Fedora. First we thought there must be something wrong with our PHP code. But as the code worked fine on our servers (Debian) we soon found out that we couldn't store more than 1 MB in our content field in the database. After a little more investigation the only solution to the problem seems to be raising the mysql system variable &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet"&gt;max_allowed_packet&lt;/a&gt; to a value that is bigger than the largest content that should go into a single field. &#xD;
&#xD;
For Debian the default max_allowed_packet size is 16 MB, but since the max_allowed_packet is not set for Fedora, MySQL defaults to 1 MB. We have no access to the MySQL server configuration so we had to solve problem this on the client side (our PHP code). An example of how this can be done is given here.&#xD;
&#xD;
&lt;pre class="brush: php"&gt;&#xD;
$fileName = '/file/name/of/uploaded/file.bin';&#xD;
$fileSize = filesize($fileName);&#xD;
&#xD;
// $db holds the (ADODB) database connection&#xD;
// Fetch current packet size&#xD;
$packetSize = (int) $db-&gt;GetOne('SELECT @@max_allowed_packet');&#xD;
if ($packetSize &amp;lt; $fileSize + 2048) {&#xD;
	$sql = sprintf('SET @@max_allowed_packet=%d', $fileSize + 2048);&#xD;
	$db-&gt;Execute($sql);&#xD;
}&#xD;
&#xD;
$db-&gt;Execute('INSERT INTO `file`(`filename`, `filesize`) VALUES(?, ?)', array($fileName, $fileSize));&#xD;
$fileId = $db-&gt;Insert_ID();&#xD;
&#xD;
// Here you can store the file contents in database&#xD;
$handle = fopen($fileName, 'r');&#xD;
while (($data = fread($handle, 524288)) &amp;&amp; strlen($data) &gt; 0) {&#xD;
	// Update record with chunks of 512K, until the file is completely in the database&#xD;
	// This prevents PHP from running out of memory&#xD;
	$sql = sprintf(&#xD;
		'UPDATE `file` &#xD;
		SET `contents` = CONCAT(`contents`, ?) &#xD;
		WHERE `id` = ?');&#xD;
	$this-&gt;db-&gt;Execute($sql, array($db-&gt;Quote($data), $fileId));&#xD;
}&#xD;
fclose($handle);&#xD;
&#xD;
// now set the  max_allowed_packet back to old value&#xD;
$sql = sprintf('SET @@max_allowed_packet=%d', $packetSize);&#xD;
$db-&gt;Execute($sql);&#xD;
&lt;/pre&gt;&#xD;
&#xD;
It is possible to store values up to 1 GB in a field in MySQL (as &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet"&gt;1 GB is currently the maximum allowed packet size&lt;/a&gt;). It is however not advisable to use the above method using &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat"&gt;CONCAT&lt;/a&gt; to put large files in the database, &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat"&gt;CONCAT&lt;/a&gt; gets very slow when the size of the blob gets bigger. If you want to put large files in the database you probably want to take a look at the &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_load-file"&gt;LOAD_FILE&lt;/a&gt; function. This is however more complex if MySQL doesn't run on the same server as your PHP code is running on.&#xD;
&#xD;
As you can see in the above PHP code the following MySQL queries can be used to retrieve and set the max_allowed_packet variable for the current connection.&#xD;
&#xD;
&lt;pre class="brush: sql"&gt;&#xD;
-- Fetch maximum allowed packet size (in bytes)&#xD;
SELECT @@max_allowed_packet;&#xD;
&#xD;
-- Set maximum allowed packet size (in bytes)&#xD;
SET @@max_allowed_packet=33554432;&#xD;
&lt;/pre&gt;&#xD;
&#xD;
We use the following table definition to store the files (I have left out some unimportant fields). &#xD;
&#xD;
&lt;pre class="brush: sql"&gt;&#xD;
CREATE TABLE `file` (&#xD;
  `id` int(10) unsigned NOT NULL auto_increment,&#xD;
  `filename` varchar(255) NOT NULL,&#xD;
  `filesize` varchar(50) NOT NULL,&#xD;
  `contents` longblob NOT NULL,&#xD;
  PRIMARY KEY  (`id`)&#xD;
) ENGINE=InnoDB CHARSET=utf8&#xD;
&lt;/pre&gt;</description><pubDate>Tue, 29 Sep 2009 22:55:00 +0200</pubDate><guid isPermaLink="false">tag:www.devplace.nl,2009:storing-large-values-in-mysql-fields-with-php/1254255396</guid></item><item><title>Loading SyntaxHighlighter on demand</title><link>http://www.devplace.nl/blog/loading-syntaxhighlighter-on-demand</link><description>SyntaxHighlighter is a great JavaScript script, but it lacks one important feature in my opinion.&#xD;
&#xD;
For this blog, I needed a syntax highlighter for the code examples that will be shown here. I soon found &lt;a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter"&gt;SyntaxHighlighter&lt;/a&gt; as I was looking for an unobtrusive JavaScript solution. But there's one thing that I didn't like about it. You need to load all JavaScript files in advance even if you don't need syntax highlighting on a given page. This is not a problem when you're building static pages, because then you can just include the needed JavaScript files yourself. For this blog however I wanted SyntaxHighlighter to be loaded on demand without specifying for which page the SyntaxHighlighter should be loaded.&#xD;
&#xD;
So I came up with the nifty JavaScript solution below. It uses &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; because jQuery is already loaded on all pages of this blog. But it shouldn't be so hard to do the same without jQuery.&#xD;
&#xD;
&lt;pre class="brush: javascript"&gt;&#xD;
$(document).ready(&#xD;
	function() {&#xD;
&#xD;
		// only load syntax hilighter when needed&#xD;
		// So check for any HTML &amp;lt;pre&amp;gt; elements for which the class attribute starts with 'brush'&#xD;
		if ($(&amp;quot;pre[class^=brush]&amp;quot;).length &amp;gt; 0) {&#xD;
	&#xD;
			var jsPath = themeUrl + '/js/hilighter/';&#xD;
			var cssPath = themeUrl + '/css/hilighter/';&#xD;
	&#xD;
			// Defines which languages should be supported&#xD;
			var brushes = ['Bash','Php','Plain','JScript', 'Sql'];&#xD;
	&#xD;
			var jsBrushes = '';&#xD;
			for (var i = 0; i &amp;lt; brushes.length; i) {&#xD;
				jsBrushes  = '&amp;lt;script src=&amp;quot;' + jsPath + 'shBrush' + brushes[i] + '.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;';&#xD;
			}&#xD;
	&#xD;
			// Append SyntaxHighlighter scripts to the HTML head&#xD;
			$(&amp;quot;head&amp;quot;).append(&#xD;
				'&amp;lt;script src=&amp;quot;' + jsPath + 'shCore.js&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;' +&#xD;
				  jsBrushes +&#xD;
				  '&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;' + cssPath + 'shCore.css&amp;quot; type=&amp;quot;text/css&amp;quot;&amp;gt;' +&#xD;
				  '&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;' + cssPath + 'shThemeDefault.css&amp;quot; type=&amp;quot;text/css&amp;quot;&amp;gt;'&#xD;
			);&#xD;
			// Apply SyntaxHilighlighter&#xD;
			SyntaxHighlighter.all();&#xD;
		}&#xD;
	}&#xD;
);&#xD;
&lt;/pre&gt;&#xD;
&#xD;
I think the script is pretty much self-explanatory. First we check if there are one or more PRE-elements having a classname that starts with "brush". If one or more of such PRE-elements exist in our HTML code, the appropriate SyntaxHighlighter CSS and JavaScript files will be loaded by adding script tags to the HTML head.</description><pubDate>Tue, 01 Sep 2009 22:12:07 +0200</pubDate><guid isPermaLink="false">tag:devplace.vandesande.no-ip.org,2009:loading-syntaxhighlighter-on-demand/1251835910</guid></item></channel></rss>
