Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
How to read the .docx files in PHP

How to read the .docx files in PHP

Hi, In This tutorial you will learn about "How to read the .docx files in PHP". Generally .docx files will be opened in the MS-OFFICE, But we are able to open the .docx file in PHP we have to convert the .docx file into text then we can easily display the content in the web browser.

<?php  
 function read_file_docx($filename){  
      $striped_content = '';  
      $content = '';  
      if(!$filename || !file_exists($filename)) return false;  
      $zip = zip_open($filename);  
      if (!$zip || is_numeric($zip)) return false;  
      while ($zip_entry = zip_read($zip)) {  
      if (zip_entry_open($zip, $zip_entry) == FALSE) continue;  
      if (zip_entry_name($zip_entry) != "word/document.xml") continue;  
      $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));  
      zip_entry_close($zip_entry);  
      }// end while  
      zip_close($zip);  
      $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);  
      $content = str_replace('</w:r></w:p>', "\r\n", $content);  
      $striped_content = strip_tags($content);  
      return $striped_content;  
 }  
 $filename = "sample.docx";// or /var/www/html/file.docx  
 $content = read_file_docx($filename);  
 if($content !== false) {  
      echo nl2br($content);  
 }  
  else {  
      echo 'Couldn\'t the file. Please check that file.';  
           }  
 ?>  

* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff
How to save an HTML5 Canvas as Image on a server using PHP

How to save an HTML5 Canvas as Image on a server using PHP

Hi, In this tutorial, I am going to explain, How to save an HTML5 Canvas as Image on a server using PHP. Before going to this topic please refer "How to convert the HTML content into an image using jquery" in my previous post then you will get an idea how to generate the div content as an image.

Step 1: First Convert canvas image to URL format (base64)
var dataURL = canvas.toDataURL();
Step 2: After Send it to your server via Ajax
$.ajax({
  type: "POST",
  url: "script.php",
  data: { 
     imgBase64: dataURL
  }
}).done(function(o) {
  console.log('saved'); 
});
Step 3: Now Save base64 on your server as an image using php code
<?php  
      // requires php5  
      define('UPLOAD_DIR', 'images/');  
      $img = $_POST['imgBase64'];  
      $img = str_replace('data:image/png;base64,', '', $img);  
      $img = str_replace(' ', '+', $img);  
      $data = base64_decode($img);  
      $file = UPLOAD_DIR . uniqid() . '.png';  
      $success = file_put_contents($file, $data);  
      print $success ? $file : 'Unable to save the file.';  
 ?>  
Here you can download the Full Source code and check the demo.

Download Demo
* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff
How to Refresh a page after specific time automatically in PHP

How to Refresh a page after specific time automatically in PHP

Hi, In This tutorial you will know about How to Refresh a page after specific time automatically in PHP. Generally, we did this type of implementation in the javascript or jquery but sometimes javascript is disabled in the user's side (i.e users disabled the javascript in their browsers).

At this situation, we need to implement that code in PHP
it is very simple first we need to fix the specific time for the refresh the website.
In general, the header is used to redirect the web page in PHP by using the location. But here we will use the header in the Refresh tag to set the time and web page URL to refresh the web page.

 //getting the current web page url
 $page = $_SERVER['PHP_SELF'];
 //set the time in seconds
 $sec = "10";
 header("Refresh: $sec; url=$page");
* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff
How to store and retrieve array in MySQL and PHP

How to store and retrieve array in MySQL and PHP

This is the most important technique for all Php developers because of Mysql can't store the array values directly.
So we need to convert the PHP Array to string by using the serialize().

This mechanism is very useful in the e-commerce cart functionality. For example, we can store the customer purchase items list and price can be stored in the associative array and convert into string and store in the MySQL.
This way we can reduce the logic of the code get the accurate result also.

MySql Code
CREATE TABLE `students` (
  `id` int(11) NOT NULL,
  `language` varchar(50) NOT NULL,
  `student_names` mediumtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for table `students`
--
ALTER TABLE `students`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
You need to connect the database create the dbconfig.php file
 <?php  
 $servername = "localhost";  
 $username = "root";  
 $password = "";  
 $dbname = "techiesbadi";  
 // Create connection  
 $con = new mysqli($servername,$username, $password, $dbname);  
 // Check connection  
 if ($con->connect_error) {  
 die("Connection failed: " . $con->connect_error);  
 }  
 ?> 
Now Store the PHP Array in MySql create the store-array.php file
 <?php  
 include "dbconfig.php";  
 $students = array("ramu", "ravi", "gopal", "krishna");  
 //Converting array to String  
 $student_names=mysqli_real_escape_string($con, serialize($students));  
 $sql = "INSERT INTO `students` (`id`, `language`, `student_names`) VALUES (NULL, 'php', '$student_names')";  
 $result = $con->query($sql);  
 if($result === TRUE){  
      echo "Record inserted successfully";  
 }  
 ?>
Now retrieve the PHP Array from MySQL create the retrieve-array.php file
<?php  
 include "dbconfig.php";  
 $getStudnets = $con->query("SELECT * FROM `students` LIMIT 1");   
 $studentsData = $getStudnets->fetch_object();  
 $language = $studentsData->language;  
 //Converting string to array  
 $students = unserialize($studentsData->student_names);  
 //printing students array  
 print_r($students);  
 echo "<br />";  
 foreach ($students as $student) {  
      echo $student." learning ".$language." language.<br/>";  
 }  
 ?>
Here you can download the Full Source code and check the demo.

Download Demo
* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff

How to Generate QR Code using Google Chart API in PHP

Hi, In this tutorial you will learn How to Generate QR Code using Google Chart API in PHP. Before going to the topic you need to know about what is QR Code?
QR Code is a machine-readable code, It consisting of an array of black and white squares. It is typically used for storing the information like general text, contact information, URL, image and SMS this information is reading by the smartphone cameras and QRcode Scanner Devices.

If you want to generate the QRcode in PHP you shall need to load the lots of libraries to your application.But we use this  Google chart API to generate the QRcode. Here there is no need to load the libraries to your application just we use the Google Chart API and CURL.

The Script allows you to create the dynamic QRcodes for the content like  TEXT, URL, EMAIL, CONTACT, SMS and other details.
You can save the generated QR code as a PNG file.

qrcode.php
<?php  
 class QrCode{  
   // Google Chart API URL  
   private $apiUrl = 'http://chart.apis.google.com/chart';  
   private $data;  
   // It generates URL type of Qr code  
   public function URL($url = null){  
     $this->data = preg_match("#^https?\:\/\/#", $url) ? $url : "http://{$url}";  
   }  
    // It generate the Text type of Qr code  
   public function TEXT($text){  
     $this->data = $text;  
   }  
   // It generates the Email type of Qr code  
      public function EMAIL($email = null, $subject = null, $message = null) {  
           $this->data = "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;";  
      }  
   //It generates the Phone numner type of Qr Code  
   public function PHONE($phone){  
     $this->data = "TEL:{$phone}";  
   }  
   //It generates the Sms type of Qr code  
      public function SMS($phone = null, $msg = null) {  
           $this->data = "SMSTO:{$phone}:{$msg}";  
      }  
   //It generates the VCARD type of Qr code  
      public function CONTACT($name = null, $address = null, $phone = null, $email = null) {  
           $this->data = "MECARD:N:{$name};ADR:{$address};TEL:{$phone};EMAIL:{$email};;";  
      }  
   // It Generates the Image type of Qr Code  
      public function CONTENT($type = null, $size = null, $content = null) {  
           $this->data = "CNTS:TYPE:{$type};LNG:{$size};BODY:{$content};;";  
      }  
   // Saving the Qr code image   
      public function QRCODE($size = 400, $filename = null) {  
           $ch = curl_init();  
           curl_setopt($ch, CURLOPT_URL, $this->apiUrl);  
           curl_setopt($ch, CURLOPT_POST, true);  
           curl_setopt($ch, CURLOPT_POSTFIELDS, "chs={$size}x{$size}&cht=qr&chl=" . urlencode($this->data));  
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
           curl_setopt($ch, CURLOPT_HEADER, false);  
           curl_setopt($ch, CURLOPT_TIMEOUT, 30);  
           $img = curl_exec($ch);  
           curl_close($ch);  
           if($img) {  
                if($filename) {  
                     if(!preg_match("#\.png$#i", $filename)) {  
                          $filename .= ".png";  
                     }  
                     return file_put_contents($filename, $img);  
                } else {  
                     header("Content-type: image/png");  
                     print $img;  
                     return true;  
                }  
           }  
           return false;  
      }  
 }  
 ?>
To create the QR code you need to include the qrcode.php file in your index.php file
<?php  
 include "qrcode.php";   
 // Create QRcode object   
 $qc = new QRCODE();   
 // create text QR code   
 $qc->TEXT("TechiesBadi");     
 // render QR code  
 $qc->QRCODE(400,"qrcode.png");  
 ?>  
Like this you can generate the remainig Qrcodes.
// URL QR code 
$qc->URL('www.techiesbadi.in');

// EMAIL QR code 
$qc->EMAIL('info@techiesbadi.in', 'SUBJECT', 'MESSAGE');

// PHONE QR code 
$qc->PHONE('PHONENUMBER');

// SMS QR code 
$qc->SMS('PHONENUMBER', 'MESSAGE');

// CONTACT QR code 
$qc->CONTACT('NAME', 'ADDRESS', 'PHONE', 'EMAIL');
Here you can download the Full Source code and check the demo.

Download Demo
* If you like this post please don't forget to subscribe TechiesBadi - programming blog for more useful stuff
How to get the geolocation details of the user during a website visit

How to get the geolocation details of the user during a website visit

Hi, You will learn in this tutorial how to get the geolocation details of the user during a website visit. Basically, we need the visitor's location details to analyze the site webmasters to know which country users are visiting the site and also some area wise reports and interested post published on the website, For which post gains more traffic from the particular country or region. And also this is very useful when a website is a done in the country wise website like

http://in.example.com/
http://us.example.com/
http://fr.example.com/
http://de.example.com/

In this situation also we need to collect the visited user's geolocation. Because we collect the user's geolocation then we will redirect the user to based on the country website.

Suppose if a user is visited the http://www.example.com/ the user is coming from the united states then we will redirect to the http://us.example.com/

To do this by using a simple plugin i.e geo plugin. By using this plugin we can get the visited user's information like city, region, Country Name, Country Code, Currency Code, etc..

You need to download the geoplugin library and call it on your page.
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps
 <?php  
 require_once('geoplugin.class.php');  
 $geoplugin = new geoPlugin();  
 //locate the IP  
 $geoplugin->locate();  
 echo "Geolocation results for {$geoplugin->ip}: <br />\n".  
      "City: {$geoplugin->city} <br />\n".  
      "Region: {$geoplugin->region} <br />\n".  
      "Area Code: {$geoplugin->areaCode} <br />\n".  
      "DMA Code: {$geoplugin->dmaCode} <br />\n".  
      "Country Name: {$geoplugin->countryName} <br />\n".  
      "Country Code: {$geoplugin->countryCode} <br />\n".  
      "Longitude: {$geoplugin->longitude} <br />\n".  
      "Latitude: {$geoplugin->latitude} <br />\n".  
      "Currency Code: {$geoplugin->currencyCode} <br />\n".  
      "Currency Symbol: {$geoplugin->currencySymbol} <br />\n".  
      "Exchange Rate: {$geoplugin->currencyConverter} <br />\n";  
 ?>  
Here you can download the Full Source code and check the demo.

Download Demo
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff