Posts

Showing posts with the label PHP

Function Oriented File Upload PHP

Image
Put the Following Function here: <?php function uploadimgfile($index,$folder="other",$prefix="other") { $target_dir  = "upload_directory_path/";  // try to put full physical path  // identity accstatement address advtimg other $uploadOk = 1; $senddata = array(); $senddata['data'] = "NILL"; $notallowed = array("php","js","css","html");  // defined here the extesion not to upload $shownotallowed = "PHP, JS, CSS, HTML fie is not allowed to upload."; $extension = explode(".",basename($_FILES[$index]["name"])); $extension = end($extension); ECHO "<hr/>" . $realfilnename = basename($_FILES[$index]["name"]); $datetofolder = date("Y/M/d"); $datetofolder = strtolower($datetofolder); $checkdirectory = $targ

Remote File Download and upload directly to Server using PHP

Image
Sometime we require to Download some files from the remote location and upload it to our server, which is a time taking process. Here We are describing how to download and upload the file directly to our server using PHP. For this  to work , we will take the http location of the files and using the curl method , we will copy it to our server, so our downloading and uploading time will be saved, also it will be more fast , due to its directly copy the file from one remote server to another server. On this code , we have created a form and its take only HTTP url of the file. Here is the PHP code to do this: <html> <head> </head> <body> <?php if(isset($_POST['zip_file'])) { if(!is_dir("folder")) { mkdir("folder", 0755); } $url = $_POST['zip_file']; $newname = explode('/',$_POST['zip_file']);

Replace a specific keyword on all files name in a folder

Image
Suppose, we have to change some keyword within a folder for all files name. For that I have written a small program, Its take a folder name and take all the files's name of the folder in the array, and we replace the specific keyword with the word, we want there.  Follow the program for the code, you just need to change the first three variable of the code. See Live Example here <?php $dir = 'rename4folder';   // folder path within this file, If are nor sure about the location, just use the full Physical URL of the folder $replacekeyword  = 'replace';  // name to be replaced $replacewith  = 'with';  // name with this the name replacerd $files = scandir($dir); if(is_array($files)) { foreach($files as $single) { $newsingle = str_replace($replacekeyword,$ replacewith,$single); if(($single!=".")&&($single!=" .."))

Converting the character case in PHP

Sometime we need to convert case of the word o variable we got in PHP. PHP have a built in function for converting the case of the word, Which can be use as below code. Some example are, we save all user name in database and after calling from database, we don't know what will be the output of name in the sense of character case, as user can enter his name all in capital, all in small or can insert into first character capitcal. So we will manipulate these as follow: First we convert in to lower case, suppose we are getting  variable i.e.  $username, so we do $username = strtolower($username);  Then we convert it to according to our need as follow: If want only first character capital:       $username =  ucfirst( $username ); If want only first character capital:       $username =  strtoupper ( $username ); We can also do it by one line as : $username =  ucfirst ( strtolower($username) ); $username =  strtoupper ( strtolower($username) ); ############  Code ###

Get Factorial of a Number using PHP

Image
In mathematics, the  factorial  of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, The value of 0! is 1, according to the convention for an empty product. Read full about  factorial    here  on  wikipedia. We will use For loop for getting  factorial   of  a number, so we just have to decrease value by one and multiply with previous holding temporary value, We have to initiate temporary value with 1. The Following will be code: Function oriented way: <?php // function defination function GetFactorial($value) { $tempvalue = 1;  for ( $i=$value ; $i>0 ;  $i-- ) {   $tempvalue *=  $i ; // this is same as the   $tempvalue =  $tempvalue * $i ; } return $tempvalue; } // function defination // getting value by calling echo "<br/>Factorial of 1 is : ".GetFactorial(3); echo "<br/>Factorial of 2 is : ".GetFactorial(3); echo

What is PHP ???

A Basic PHP Program

Image
PHP is a server side language, which means it execute on the server machine and send output to the client machine. so Server is like a computers which store information on it memory and display data on the machine connected to Internet. This system is setup via assigning a Fixed IP to that machine, so that the file on the machine can executed anywhere in world, The system we are using will called as client machine, Which ask data from the server machine to display the information on the screen or output device like Printer. So PHP is sever side languages, which actually create the HTML with some numeric or Theoretical calculation done by the programmer. so here are a Basic PHP Program , which simply print hello in the webpage. The PHP start with <?php and end with ?> This sign tell the server to execute the PHP code between these sign. The Following is the code:  (Use spaces and tab, so the programm is easily readable for future reading/ reviewing of the code) ##

Visitor Counter with cookies in PHP

Image
Here we are describing how we can check visitor in a website with he help of Cookies in PHP. We will setup a cookies in the user Browser for one year. So we will first check about that particular cookies present in User's Browser. If the cookies is present in the User's Browser (Client Computer) , we have to do nothing on that condition, as the user is old for the that website. If the cookies is not present in the user's Browser, we will setup the cookies in the user's Browser and update the details on the database for total count for that website, we are using the visitor counter. So the first condition to check we have is: is the cookies is present in the Browser:  For this we have the function on php, which is isset(),  This function is used for checking the variable is set in the Program or not.   The cookies, we will create with the name of "isvisitor" So the code will be as follow: <?php if(isset($_COOKIES)) ?>

Common Mistake in Echo statement

Image
I am describing a little problem, Normally face by a programmer. This is a common Problem, Which can Produce in a large program a wrong output. So, You know the use of echo in PHP, which used for displaying data or some result in the webpage. Example are as follow: <?php //   (1)  for displaying some paragraph; echo "I am writing a code."; // it will display the line on the webpage:   I am writing a code. //    (2) For addition of Numbers echo 3+3; // It will display on the webpage: 6 //   (3)  Paragraph and addition of numbers echo "The Sum is : 3 + 3 :". 3 + 3; // it will display on the webpage:   6 ?>

Make Image Thumbnail on Upload Time.

 Today, we will talk about how to Make a Thumbnail  Image of Image uploaded via a File Upload using PHP. For this I have created a script, which we have to upload anywhere in the website, where you want to upload the files. Now use this code and save as createimage.php *********************** Code *********************** <?php if(!isset($_GET['fileurl'])) { exit(); }  $fileurl = $_GET['fileurl']; // actual script begins here $type=false; function open_image ($file) {     //detect type and process accordinally     global $type;     $size=getimagesize($file);     switch($size["mime"]){         case "image/jpeg":             $im = imagecreatefromjpeg($file); //jpeg file         break;         case "image/gif":             $im = imagecreatefromgif($file); //gif file       break;       case "image/png":           $im = imagecreatefrompng($file); //png file       break;     defau

Upload File Using PHP - Example For Image

Create The HTML Form Create an HTML form, so you can choose file want to upload: *********************** Code *********************** <form action="upload.php" method="post" enctype="multipart/form-data">   Please choose a file: <input type="file" name=" uploadFile " accept="image/*" /> <br>   <input type="submit" value="Upload File" /> </form> *********************** End of Code *********************** On the Above code  accept is use for accepting only Image file as a File Input. If no accept is specify, All File are accepted. Create a upload.php file , whose code will be as Follow: *********************** Code *********************** ?php $target_dir = "uploads/"; $target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]); $uploadOk=1; // Check if file already exists if (file_exists($

Multiple Page on a single page using PHP with Switch Condition

Here we will learn to use Switch Statement to Manage Multiple page on a Single Page. We will use GET Variable for checking the condition, which page we have to show. Following is the code: <?php // make condition if(isset($_GET['page'])) { $pagetoshow = $_GET['page']; } else { $pagetoshow = "index"; } // make condition // execute condition switch($pagetoshow) { case "about": // content for about  echo "This is the about page."; // content for about break; case "contact": // content for contact  echo "This is the contact page."; // content for contact break; default: // content for index  echo "This is the index page."; // content for index break; } // execute condition ?> Following will be the O/P of the code: Save page as index.php index.php This is the index page. index.php?page=about Th

Save HTML Content into Database

    Generally we have required to save HTML Content i.e. a HTML page or a Block of content into Database. But if we Directly save it to Database, Its throw error due to Special Character and Quotation in the Content.     So First we have to Convert it to database supported Value. We can do it by Decoding the html. In PHP we have a Function "htmlspecialchars" and for Decoding the Function is "htmlspecialchars_decode". So Here is the Code: Create a Form into a File i.e. "form.php". In this Example, we will get only one input on textarea. Code will be as Follow: ######################## Code for form.php ######################## <html> <head> <title> Form For HTML Element Save into Database. </title> </head> <body> <div style="width:250px;margin:0px auto;background:#666666;padding: 20px;"> <h4> Save HTML Content into Database </h4>

How to Work with Select Box after POST Data in PHP

Everytime We create a form, we used Fields like Text-BOX, Select Box, Radio and Checkbox, Here we will understand a Function to POST data to be selected on database. PHP Code  <?php function isselected($value,$optionvalue)                         {                                  if ( $value == $optionvalue )                                                          {                                                                   return "selected='selected'";                                                          } else {                                                                return false;                                                           }                         } echo "<form method="POST">";             if(isset($_POST['selectbox']))                      {                                  $valueselected = $_POST['selectbox'];                       }             echo "&l

A Program to Printing a Box from For loop.

A Program to Printing a Box from For loop. #################################### PHP Code #################################### <?php echo "<table cellspacing= '0' cellpadding= '0'><tr>"; for ($i=1;$i<=30;$i++) { echo "<td>"; for($j=1;$j<$i;$j++ ) { echo "*"; echo "<br/>"; } echo "</td>"; } for ($i=1;$i<=30;$i++) { echo "<td>"; for($j=30;$j>=$i;$j-- ) { echo "*"; echo "<br/>"; } echo "</td>"; } echo "<table><tr>"; ?> #################################### O/P of the Code #################################### * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

PHP function call of a another class within another class

################################ PHP CODE ################################ <?php // function call of a another class class class1 { function function1() { return 123; } function function2() { return 259; } } class class2 { function function1() { $object = new class1(); echo $object->function1()+$object->function2(); } } $obj = new class2(); $obj->function1(); // function call of a another class echo "<hr/>"; echo "Above Exapmle Basic : " . (123+259); echo "<hr/>"; ?> ################################ Output of the  CODE ################################ 382 Above Example Basic : 382

PHP Function definition within in a class

######################## PHP CODE ######################## <?php // function definition within in a class class common { function addition($a,$b) { echo "<br/> Sum of the " . $a . " and  " . $b . " is : " . ( $a + $b ) ; } function multiplication($a,$b) { echo "<br/> Multiplication of the " . $a . " and  " . $b . " is : " . ( $a * $b ) ; } } // function defination within in a class // object creation for class $com1 = new common(); // object creation for class // function call via class's object $com1->addition(2,3); $com1->multiplication(2,3); // function call via class's object ?>                    ########################                        OUTPUT of the CODE                     ######################## Sum of the 2 and 3 is : 5 Multiplication of the 2 a

Function definition with returning some value - PHP

############################ PHP Code ################### ######## <?php         // function definition with returning some value  function sum($a,$b)   {      $sum = $a + $b;   return $sum;  }  function minus($a,$b)  {    $minus = $a - $b;  return $minus;  }  // function definition with returning some value  // use of return function as a variable  echo "Sum of 2 and 3 is : ".sum(2,3);  // use of return function as a variable  // function definition with returning some value  ?> ############################ Output of the Code ############################ Sum of 2 and 3 is : 5