Archive for October 2008

Ubuntu Linux slow/bad internet connection problems

At my office I had been having a problem with my Ubuntu and Fedora boxen having problem connecting to external sites. Some would work, albeit slow, where as others would not work at all (e.g. trying to connect to reddit in Firefox would get enough of the page to display the <title></title> tags but it would be "forever loading" the actual content, with only a white screen).

The solution seemed to be that the router hated TCP Window Scaling. The fix that so far seems to work is to edit your /etc/sysctl.conf file and adding the line:

net.ipv4.tcp_window_scaling=0

And then running the command:

sudo sysctl -p

To reload the changes.

Serialize Python Variables To PHP

So I'm in the planning stages of a project where I'll probably be doing some heavy lifting in Python and serving up the output through PHP. Obviously this will entail transmitting data between Python and PHP and while I haven't had a chance to do performance testing to see if it's worth it to save to a database, the only other option was to serialize the data to a file that PHP could parse quickly. Obviously, the output from serialize() is going to be the fastest way to recieved the data.

While there already exists a Python set of classes that serializes (and serializes) Python data, it (a) didn't handle objects and (b) was licensed under the GPL (rant to follow later), meaning I couldn't use it for it's intended purpose due to the closed source nature of my project.

I decided this was a good enough time to play around with git so I created a github repo here.

It could probably still use some more TLC in the object area, maybe a few more supported types, and unserialization functions would be nice.

I do feel it wasn't bad for a few hours of hacking if I do say so myself.

The code for the module (should be in a phpserialize.py file) is as follows:

Continue reading ‘Serialize Python Variables To PHP’ »

PHP Bacon!

Unfortunately, Elizabeth Smith mentioned on twitter that PHP 5.3's new garbage collector is based on a paper written by a man named Bacon (pdf here). We now have Bacon's garbage collector...

Announcing PHP 5.3: Now With More Bacon!!

Spread the meme.

Outputting CSV as a Downloadable File in PHP

Nearly every application you could write in for the business sphere in PHP probably requires some sort of data export, most likely in the CSV format.

The easiest way to provide a downloadable file is by altering the headers and echo'ing the file content. In our case:

<?php
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="filename.csv"');

We want to set our applicable Content-Type so that the browsers associate the file properly. Just relying on the extension doesn't work, even in Windows. The magic is in the third header setting, "Content-Disposition," which informs the browser to download as a separate file (don't open a new window and display a blank page, just display the file download box) and tell the browser the filename is "filename.csv". This way rewrite rules like http://localhost/export/csv/ will result in a download box that declares the file "filename.csv" rather than a randomly assigned name or whatever the current url is.

Into the meat of the CSV export. At the very beginning we need to open up a stream to the PHP output (the same place where echo sends its string content, which is NOT stdout):

<?php
$outstream = fopen("php://output",'w');
 

Next we're going to assume you already have your data packed nicely into an array (or array of arrays) so long as we have a single array per row/line.

The magic comes into play using the build in PHP function fgetcsv(). fgetcsv() takes an array for a single row and outputs it, automatically escaping output according to column and enclosure delimiters!

fgetcsv() requires a file resource as its first parameter and the magic of PHP streams is they act like a file resource (actually a file resource is just a file stream), so we give it $outstream to make fputcsv() echo its output. We fill in the rest of the parameters according to the php.net documentation and voila we have:

<?php
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="filename.csv"');
 
$outstream = fopen("php://output",'w');
 
$test_data = array(
	array( 'Cell 1,A', 'Cell 1,B' ),
	array( 'Cell 2,A', 'Cell 2,B' )
);
 
foreach( $test_data as $row )
{
	fputcsv($outstream, $row, ',', '"');
}
 
fclose($outstream);

For more output stream craziness, zaemis from the #phpc IRC channel on freenode shared a code snipped that outputs CSV either to the output buffer OR will return it as a string using some clever streams hackery:

<?php
function exportCSV($data, $col_headers = array(), $return_string = false)
{
    $stream = ($return_string) ? fopen ('php://temp/maxmemory', 'w+') : fopen ('php://output', 'w');
 
    if (!empty($col_headers))
    {
        fputcsv($stream, $col_headers);
    }
 
    foreach ($data as $record)
    {
        fputcsv($stream, $record);
    }
 
    if ($return_string)
    {
        rewind($stream);
        $retVal = stream_get_contents($stream);
        fclose($stream);
        return $retVal;
    }
    else
    {
        fclose($stream);
    }
}