Thursday, October 16, 2008

Two powerful classes developed by me in php!

Hello Guys,

Are u a PHP programmer, understand OOP. Here is a mysql database connection file for you which I developed into a class and a photo upload class just copy the codes into a php file or script named "c_common.php" and paste the codes, save the file.

This codes have been so helpful to me as I just reuse them and don't need to rewrite them...of course you know how OOP works!!!

NOTE: You need PHP 5 to run this code

So how do you use them simply call an object, something like this:
To upload a photo
require_once("c_common.php") //attach the class file
$upl=new uploadfile()
$filename=$upl->pushfile("photos",$_FILES['upload']); //the folder name is the first parameter required and the second is the name of the photo upload control

Here's the code pasted here for you.

//************the connection class to the database is created here
class connection{
var $server;
var $uname;
var $pword;
var $dbase;
function __construct($s,$u,$p,$db){
//define the mysql connection variables
function opendatabase(){
$exp="Error connecting to database:
$exp .= mysql_errno(). ": ".mysql_error();
throw new exception($exp);
} else {
return $db;
} catch(exception $e){ echo $e->getmessage();
} }

}//********End of the connection class

//*************the file upload class**********
class uploadfile{
function __construct(){ }
//Code handler for the file Upload function
pushfile($folder, $controlname){
if(isset($controlname['name']) && $controlname['size']>0) {
$upload_dir = $folder . '/';
if (!@move_uploaded_file($tmpName, $upload_dir . $fileName )) {
//echo "Error moving the file to its destination, ";
//"please try again here.";
if(!get_magic_quotes_gpc()) {
} return $fileName; }
}//********file upload class ends here

so try it guys, see if its works fine!
Pls do let me know your comments and tell me how this code works for you and let me know if you need help with some other programmer problems!!!

No comments:

Post a Comment

Please leave your comments, thanks!