lunes, 31 de mayo de 2010

PHP problems uploading large files

It took me a while to realize that the reason why some PDFs I wanted to upload using a PHP scrip I developed to the Thesis Library I am building would not make it to the server. The symptoms where funny:
  1. Small PDFs would be uploaded fine. That should have immediately ringed a bell
  2. The PHP script getting the file plus some other information appeared as if no data was passed to it.
My web server based on XAMPP has an default limitation on the size of both uploaded files (2 MB) and POST data (8 MB). I was like to get to the Eddie on Everything blog where I could find how to fix this. Very simple instructions:
  • Locate your php.ini file. Most probably at /opt/lampp/etc/php.ini
  • Edit it and replace the following configurations:
    file_uploads = On
    upload_max_filesize = 30M
    post_max_size = 30M

sábado, 16 de enero de 2010

Trying to measure the total time used by a process in GNU/Linux...

Are you trying to measure how long (in CPU, and in total) does it take a process you are running in Linux? Yeah, you do that from time to time to see the performance of your machine, and you always execute the time command. Something like:

[host]$ time somecomnand
...

real 0m1.127s
user 0m0.002s
sys 0m0.015s

great! That is what we wanted... except that we would probably like to run this automatically and get the output to some log file. So you try to redirect the output and it doesn't work. Therefore, you run man time to see if you find something there and you see the -o outputfile parameter. Yeah! That's what you need, so you try:

[host]$ time -o time.log somecommand
-bash: -o: command not found

real 0m0.002s
user 0m0.000s
sys 0m0.002s

Ups! What happened?

Well, the problem here is that you are really using time reserved word in the bash shell. And the solution is to run the time program in your linux distribution by specifying the full path to it:

[host]$ /usr/bin/time -o time.log yes
...
[host]$ cat time.log
4288.24user 80.40system 1:53:44elapsed 64%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (277major+178073minor)pagefaults 0swaps

The format is slightly different, but you have everything you need.

lunes, 11 de enero de 2010

Sorting tables in HTML using javascript

You may have seen those tables out there which are able to sort themselves by clicking on some of their column headers. How do you do that? If you search around you may see that one of the first solutions is the sorttable by Stuart Langridge. Well, it is really good and has quite some features. But according to what I was looking for it lacks two features:
  1. The column headers are clickable, but you have no way to know it unless you click on them. This can be solved by simply adding the following few lines to the CSS style file and by using <th> at the column headers:


    table.sortable th {
    cursor:pointer;
    }
    table.sortable th.sorttable_nosort {
    cursor:auto;
    }

  2. The rows do not show alternated background colors. Solving this one is a bit more tricky and requires a couple of inserts in the javascript file Stuart provides. Part of the code come from this other good example from Joost de Valk. The first modification happens on line 159, just before:
    delete row_array;


    At that point insert the following line:

    sorttable.alternate(this.sorttable_tbody);


    After that you need to insert the following code around line 254, just before the line saying "/* sort functions":


    alternate: function(tbody) {
    var tableRows = tbody.getElementsByTagName("tr");
    for (var j = 0; j < tableRows.length; j++) {
    // Check if j is even, and apply classes for both possible results
    if ( (j % 2) == 0 ) {
    if ( !(tableRows[j].className.indexOf('odd') == -1) ) {
    tableRows[j].className = tableRows[j].className.replace('odd', 'even');
    }
    else if ( tableRows[j].className.indexOf('even') == -1 ) {
    tableRows[j].className += " even";
    }
    }
    else {
    if ( !(tableRows[j].className.indexOf('even') == -1) ) {
    tableRows[j].className = tableRows[j].className.replace('even', 'odd');
    } else if ( tableRows[j].className.indexOf('odd') == -1 ) {
    tableRows[j].className += " odd";
    }
    }
    }
    },


    Then, you might change the background color (or any other aspect of the row) for the even and odd rows by adding some styles in your CSS file. For example:

    .odd {
    background-color: #ddd;
    }
    .even {
    background-color: #222;
    }



One more tip. For some font types the up and down arrows that are shown in the header when a column is sorted may look small. The rendering can be improved by tweaking the CSS file again. For example:


#sorttable_sortrevind, #sorttable_sortfwdind {
font-size: 14px;
}

Welcome

This is the starting point of a blog which will be targeted at tricks I use on my day by day life while programming various different things. I routinely use some of the most common programming languages in a variety of environments: C++, java, javascript, php, python, shell (bash/tcsh),... and a few other languages which can vaguely considered that way: HTML, LaTeX, etc. The idea is to give back to the community some of the tricks I learnt by googling. Hope someone benefits.