Monday, December 3, 2007

AJAX File Upload with Progress Bar (+PHP)

This post maybe the result of my ignorance of perhaps my lack of google-art-of-search to find any good solution around the challenge of doing an AJAX File Upload with a Progress bar, written entirely on Javascript and PHP on the server.


After breaking my head against a wall trying to find any guru around that 'could' have done a good solution to this matter, I found a couple of interesting links such as the jqUploader JQuery plugin by lussumo (with a SWF object for upload), the tesUpload object (with a Perl script to get the bytes uploaded info) by Tomas, SWFUpload object (another flash object) and the closest one to what I am wondering about but requires PHP to be patched to make use of upload_progress_meter_get_info.

I wonder if it is possible to actually do what I am saying and if there is any solution out there that I missed on my googlexperience. I have seen some scripts around there that promise such solution but when I tried, poof!, it didn't work as expected. So the challenge is here... I am going to try a solution that actually checks the actual file size of a temporary file after an uploaded form has been posted... Anyone for the challenge? or anyone knows any 'guru' out there to get me to the right direction?

---



Friday, November 30, 2007

P.O.R.K (Modified to work with PHP 4)

For those who don't know what is PORK, it's an Object-Relation mapper for PHP 5 that attempts to be easy, fast, lightweight, uses optimized database queries and provides an easy Find() function very loosely based on rails'.

In short, the O/R mapper handles all your basic Select/Insert/Update/Delete/Join statements for you so you won't have to write the same SQL over and over. You just create PHP objects that are linked to a row in the database.

The above description and original work is from mr. Jelle Ursem (see http://www.schizofreend.nl/ for more info). I came accross his work when I was about to create one Library to do exactly like PORK does. The only problem I had to work with it was that my servers runned PHP4, so I modified the code to fit my requirements.

So, again, I posted PORK for those that still work with PHP4 and have a need for an O/R mapper like this one.

Jell Ursem have plenty of more detailed info related to the functionality of this object (for PHP 5) and also an PORK.generator (A PHP5 based database analyzer that automatically sees the different types of relations in your (MySQL) database and creates linked dbObjects for it). So check out its site for more detailed info.

Differences from PHP5 version and this PHP4 version
Object Creation
Check the following object declaration example, it is an O/R mapper that will work with table tbl_note, we call it Note object (differences with the object in PHP5 are clear if you check Jell examples on http://www.schizofreend.nl/Pork.dbObject/Examples/)

class Note extends dbObject
{
function Order($ID=false) {
$this->__setupDatabase('tbl_note', // database table
array( 'n_ID' => 'ID', // database field => object property
'o_ID' => 'orderID',
'n_note' => 'note',
'n_date' => 'date'),
'n_ID', // primary table key
$ID); // value of primary key to init object

// Add one-to-one relationship
$this->addRelation('Order'); // Order is another object and addRelation function will connect both of them
}
// This function is a *MUST* dbObject requires it
function toString()
{
return 'Note';
}
}

I have a included a mandatory function toString to the objects, so we can overcome the lack of a function such as get_class($object); in PHP4 PORK.dbObject.

Accessing Object Properties
We all know that PHP4 objects do not have the _set and _get capabilities of PHP5, therefore, in order to access object properites we will have to do it in this way:

$note = new $note();
$note->__set('note','my note'); // PHP5 $note->note = 'my note';
echo $note->__get('note'); // PHP5 $note->note;
$note->Save(); // saving to database

I have compiled a set of files on a zip file, if you are good code reader you will find out how to use very easily. There a file with an example of its use, but I did not include any SQL file to create the tables nor a proper test application. The test.php file included explain most of the steps and Jell on its site will pinpoint you to the right direction if you are missing something.




Wednesday, November 21, 2007

Cookies Anyone?

Looking back in my code I found an object that may be useful for some of you out there. This object is made to work with Cookies (and a cofee cup too! -stupid joke I know).

We all know (Javascript programmers) how easy is to work with Cookies and for most of you this object is nothing spectacular to develop but if it saves programming time for you I have reach my goal.

No more talking, here's the code:


var Cookie = {
set: function(name, value, daysToExpire) {
var expire = '';
if (daysToExpire != undefined) {
var d = new Date();
d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
expire = '; expires=' + d.toGMTString();
}
return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
},
get: function(name) {
var cookie = document.cookie.match(new RegExp('(^|;)\s*' + escape(name) + '=([^;\s]*)'));
return (cookie ? unescape(cookie[2]) : null);
},
erase: function(name) {
var cookie = Cookie.get(name) || true;
Cookie.set(name, '', -1);
return cookie;
},
accept: function() {
if (typeof navigator.cookieEnabled == 'boolean') {
return navigator.cookieEnabled;
}
Cookie.set('_test', '1');
return (Cookie.erase('_test') === '1');
}
};







Thursday, November 15, 2007


The content of this blog is published under a Creative Commons License | RSS Feed
Powered By Blogger