Monday, July 12, 2010
Parent Class | Child Class PHP
class A{
var $a;
public function example(){
$this->a = "I am from Class A";
echo $this->a;
}
}
class B extends A{
var $b;
public function example(){
$this->b="I am from class B";
echo $this->b;
echo "
";
parent::example();
}
}
$a = new A;
$a->example();
echo "\n\n\n";
$b= new B;
$b->example();
?>
Tuesday, June 29, 2010
File Transfer Using Curl
File Transfer Using Curl
The Form File from where the Curl Request is Sent
<?php
if($_POST){
echo "<pre>";
print_r($_POST);
echo "</pre>";
$localfile = $_FILES['txtUpload']['tmp_name'];
$transFile = chunk_split(base64_encode(file_get_contents($localfile)));
$_POST['txtUpload'] = $transFile ;
$_POST['fileName'] = str_replace(" ", "_",$_FILES['txtUpload']['name']);
//$curlData = array_merge($curlData, array("file"=>"@".$_POST['txtUpload'].$_POST['fileName']));
$curlData = $_POST;
$postCurlStr = '';
foreach($curlData as $key=>$val){
$postCurlStr .= $key.'='.urlencode($val).'&';
// $postCurlStr .= $key.'='.$val.'&';
}
$postCurlStr = substr($postCurlStr, 0, -1);
echo "<br/>".$postCurlStr."</br/>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, 'http://biraj.dev/biraj-practice/curl/curlpage.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postCurlStr);
//$postresponse = curl_exec ($ch);
$data = curl_exec($ch);
curl_close($ch);
}
?>
<html>
<head>
<title>Curl Post</title>
<style type="text/css">
body{
background-color: #FFFFFF;
font-family:"Verdana";
margin: 10px;
padding: 0px;
color: #222222;
font-size:10px;
}
td{
height:50px;
vertical-align:top;
font-size:12px;
}
input{
font-size:12px;
}
</style>
</head>
<body>
<h2>Post</h2>
<form name="frmCurlForm" method="post" enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="0" width="80%">
<tr>
<td width="40%">Name ::</td>
<td width="60%"><input type="text" name="txtName"></td>
</tr>
<tr>
<td>Description ::</td>
<td><textarea name="txtDescription"></textarea></td>
</tr>
<tr>
<td>Status</td>
<td><input type="radio" name="rdoStatus" value="1">Active <input type="radio" name="rdoStatus" value="0">Inactive</td>
</tr>
<tr>
<td>Category</td>
<td><input type="checkbox" name="chkCategory" value="ES">Equity Share <input type="checkbox" name="chkCategory" value="D">Debenture</td>
</tr>
<tr>
<td>Mode</td>
<td><select name="cmbMode">
<option value="Publish">Publish</option>
<option value="Draft">Draft</option>
</select>
</td>
</tr>
<tr>
<td>Attachment</td>
<td><input type="file" name="txtUpload"></td>
</tr>
<tr>
<td> </td>
<td><input type="Submit" value="Go"></td>
</tr>
</table>
</form>
</body>
</html>
The File that exists in the Server. [receives curl request]
<?php
$host = "localhost";
$username = "root";
$password = "336677";
$db = "test";
mysql_connect($host, $username, $password) or die("Hostname not found. ".mysql_error());
mysql_select_db($db) or die("Database not found. ".mysql_error());
$name = $_REQUEST["txtName"];
$description = $_REQUEST["txtDescription"];
$status = $_REQUEST["rdoStatus"];
$category = $_REQUEST["chkCategory"];
$mode = $_REQUEST["cmbMode"];
$query = "INSERT INTO tb_curl (name, description, status, category, mode) VALUES('".$name."', '".$description ."', '".$status ."', '".$category."', '".$mode."')";
mysql_query($query) or die(mysql_error());
$img = base64_decode($_POST['txtUpload']);
$fileName = $_POST['fileName'];
//$fp = "uploadfiles/".$fileName;
$fp = $fileName;
$handle = fopen($fp,"w+");
fwrite($handle,$img);
?>
Saturday, June 19, 2010
A Simple Class to Generate Dynamic PHP Pages
<?php
class GeneratePage {
var $title;
var $pageContent;
function Display( ) {
echo "<html>\n<head>\n";
$this->displayTitle();
echo "\n</head>\n<body>\n";
echo $this->pageContent;
echo "\n</body>\n</html>\n";
}
function displayTitle() {
echo "<title>" . $this->title . "</title>\n";
}
function SetContent($Data) {
$this->pageContent = $Data;
}
}
//call the class to generate the page
$newPage = new GeneratePage;
$mypageContent = "The quick brown fox jumps over the lazy dog.";
$newPage->title = "A Sample Page.";
$newPage->SetContent($mypageContent);
$newPage->Display();
?>
class GeneratePage {
var $title;
var $pageContent;
function Display( ) {
echo "<html>\n<head>\n";
$this->displayTitle();
echo "\n</head>\n<body>\n";
echo $this->pageContent;
echo "\n</body>\n</html>\n";
}
function displayTitle() {
echo "<title>" . $this->title . "</title>\n";
}
function SetContent($Data) {
$this->pageContent = $Data;
}
}
//call the class to generate the page
$newPage = new GeneratePage;
$mypageContent = "The quick brown fox jumps over the lazy dog.";
$newPage->title = "A Sample Page.";
$newPage->SetContent($mypageContent);
$newPage->Display();
?>
Saturday, May 29, 2010
How to change the salt value in cake php
/* A quick way of making a salt value for CakePHP */
$random_string = rand();
$xtra_salt = "ABDCDEFGHIKLMNOPQRSTUVWXYZ";
echo sha1($random_string.$xtra_salt);
?>
You have to run that script for getting the new salt value and paste it to
core.php and debugger.php
Friday, February 5, 2010
Create FTP in local server using XAMPP
Create FTP in local server using XAMPP
- Start Apache
- Start FileZilla
- Go to C:\xampp\FileZillaFTP and start FileZilla Server Interface.exe
- By Default there is 2 existing users "Annonomous" and "newusers". Add a new user and grant access to the specified folder you want.
- now the specified folder in the localhost will allow to transfer files.
here is a following code that will connect the ftp server
<?php
$ftpUser = "biraj";
$ftpPassword= "password";
$ftpServer = "127.0.0.1";
$connId = ftp_connect($ftpServer);
if(@ftp_login($connId, $ftpUser, $ftpPassword)){
echo "FTP login successful";
}else{
print "\nError: FTP server found but unable to login.";
ftp_quit($connId);
exit;
}
ftp_mkdir($connId, $uploadDirectory);
ftp_close($connId);
?>
Tuesday, February 2, 2010
Creating a Code Search Engine with PHP and MySQL
Creating a Code Search Engine with PHP and MySQL
http://www.developer.com/db/article.php/3671021/Creating-a-Code-Search-Engine-with-PHP-and-MySQL
Monday, January 25, 2010
Calling Methods of PHP Class Globally
<?php
class saveData{
private $filelocation, $dataSaved;
public function __construct($file, $data){
$this->filelocation = $file;
$this->dataSaved = $data;
if(!$data || strlen($data)>=256){
throw new Exception("Text shold not be more than 256 characters.");
}
if(!file_exists($file)){
throw new Exception("File missing in the location specified.");
}
}
public function saveData(){
if(!$fp = fopen($this->filelocation, 'a+')){
throw new Exception("Error in opeining the target file.");
}
if(!fwrite($fp, $this->dataSaved)){
throw new Exception("Error in writing in the target file.");
}
fclose($fp);
}
public function __toString(){
return "Data Successfully Saved.";
}
}
?>
<html>
<head>
<title>Save Data | Calling Methods of a Class Globally</title>
</head>
<body>
<form name="frm1">
<textarea name="typeValue" cols="20" rows="8"/></textarea>
<input type ="submit"/>
</form>
<?php
if($_REQUEST){
if($_REQUEST["typeValue"]){
try{
$dataSaver = new saveData("document.txt", $_REQUEST["typeValue"] );
$dataSaver->saveData();
echo $dataSaver;
}
//catch exception
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}
}
}
?>
</body>
</html>
class saveData{
private $filelocation, $dataSaved;
public function __construct($file, $data){
$this->filelocation = $file;
$this->dataSaved = $data;
if(!$data || strlen($data)>=256){
throw new Exception("Text shold not be more than 256 characters.");
}
if(!file_exists($file)){
throw new Exception("File missing in the location specified.");
}
}
public function saveData(){
if(!$fp = fopen($this->filelocation, 'a+')){
throw new Exception("Error in opeining the target file.");
}
if(!fwrite($fp, $this->dataSaved)){
throw new Exception("Error in writing in the target file.");
}
fclose($fp);
}
public function __toString(){
return "Data Successfully Saved.";
}
}
?>
<html>
<head>
<title>Save Data | Calling Methods of a Class Globally</title>
</head>
<body>
<form name="frm1">
<textarea name="typeValue" cols="20" rows="8"/></textarea>
<input type ="submit"/>
</form>
<?php
if($_REQUEST){
if($_REQUEST["typeValue"]){
try{
$dataSaver = new saveData("document.txt", $_REQUEST["typeValue"] );
$dataSaver->saveData();
echo $dataSaver;
}
//catch exception
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}
}
}
?>
</body>
</html>
Saturday, January 23, 2010
Simple example with __toString() - PHP
__toString()
__toString() is a magic function, and allows you to set a string value for the object that will be used if the object is ever used as a string.
<?php
class TotalPrice{
protected $price, $quantity, $total;
public function __construct($productPrice, $productQuantity){
$this->price = $productPrice;
$this->quantity = $productQuantity;
$this->calculatePrice();
}
protected function calculatePrice(){
$this->total = ($this->price * $this->quantity);
}
public function __toString(){
return "Total Price :: ".$this->total;
}
}
$totalPrice = new TotalPrice("2.00", "6");
echo $totalPrice;
?>
Wednesday, January 20, 2010
Exception Handling in PHP | try | throw | catch
Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown".
Throw - This is how you trigger an exception. Each "throw" must have at least one "catch".
Catch - A "catch" block retrieves an exception and creates an object containing the exception information.
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ...
refer http://www.tutorialspoint.com/php/php_error_handling.htm
<?php
function displayDetails($text){
if($text<=10){
throw new Exception("Value should be greater than 10.");
}else{
for($i=0; $i<$text; $i++){
echo $i."<br/>";
}
}
}
?>
<html>
<head>
<title>Check Try | Throw | Catch</title>
</head>
<body>
<form name="frm1">
<input type="text" name="typeValue" value="Type text and hit go"/>
<input type ="submit"/>
</form>
<?php
if($_REQUEST){
if($_REQUEST["typeValue"]){
try{
displayDetails($_REQUEST["typeValue"]);
}
//catch exception
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}
}
}
?>
</body>
</html>
Throw - This is how you trigger an exception. Each "throw" must have at least one "catch".
Catch - A "catch" block retrieves an exception and creates an object containing the exception information.
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ...
- An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block.
- Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions.
- Exceptions can be thrown (or re-thrown) within a catch block.
refer http://www.tutorialspoint.com/php/php_error_handling.htm
A simple example on Exception Handling in PHP | try | throw | catch
<?php
function displayDetails($text){
if($text<=10){
throw new Exception("Value should be greater than 10.");
}else{
for($i=0; $i<$text; $i++){
echo $i."<br/>";
}
}
}
?>
<html>
<head>
<title>Check Try | Throw | Catch</title>
</head>
<body>
<form name="frm1">
<input type="text" name="typeValue" value="Type text and hit go"/>
<input type ="submit"/>
</form>
<?php
if($_REQUEST){
if($_REQUEST["typeValue"]){
try{
displayDetails($_REQUEST["typeValue"]);
}
//catch exception
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}
}
}
?>
</body>
</html>
Tuesday, January 19, 2010
Increase count in column while updating a field in MySQL
Increase count in column while updating a field in MySQL
UPDATE <tablename> SET <thiscolumn>=<thiscolumn>+1 Where id=500
Subscribe to:
Posts (Atom)