PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: October 2014

30 Oct 2014

Upload images to mysql database using php

                         In this method, You won't need to store images in mysql database. Just store image name in mysql database and store image in particular directory. Then you can retrieve image from particular directory using image name in database. You can done it using php.

Create table using mysql query:


                create table image(
                     no int(4) AUTO_INCREMENT, img_name varchar(30), 
                     PRIMARY KEY(no) )
Now the table 'image' is created. Then you can store values to mysql database using php.

Upload images using php and mysql:


                  The php script for upload images to mysql database is:


<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
       <input type="submit" name="submit" value="Upload">
</form>
<?php
 if(isset($_POST['submit']))
 { 
   mysql_connect('localhost','root','');
   mysql_select_db('new');
   $name=$_FILES['file']['name'];
   $type=$_FILES['file']['type'];
   if($type=='image/jpeg' || $type=='image/png' || $type=='image/gif' || $type=='image/pjpeg')
   {
if(file_exists(dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/image/'.$name))
     {
      echo'file is already present';
     }
     else
     {
      $up=move_uploaded_file($_FILES['file']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/image/'.$name);
      $q=mysql_query("insert into image values('','".$name."')");
  if($up && $q)
  {
   echo'image uploaded and stored';
  }
  elseif(!$up) 
  {
   echo'image not uploaded';
  }
  elseif(!$q)
  {
   echo'image not stored';
  }
 }
   }
   else
   {
    echo'Invalid file type';
   }
 }
?>
</body>
</html>


        where,
                   $_FILES['file']['type'] is used to return the type of uploaded file.
                   move_uploaded_file() is used to upload the file.
You want to know about upload files in php.
                    if($type=='image/jpeg' || $type=='image/png' || $type=='image/gif' || $type=='image/pjpeg' ) is used to check the file type whether it is image or not. If it is image, then it is stored. Otherwise, it is not upload and also stored.
                   Now you can see the directory, the image is stored here and also stored in mysql database.

29 Oct 2014

Upload files using PHP

                       You can see upload files at all social media sites to upload your photos. You can upload files using php. You can upload both text and binary files in php. You can upload any files such as .txt, .png, .jpg, .gif, .doc, .csv. At the same time you control the file extensions and file size while uploading files using php. For example, you allows the user can upload only .doc and docx file with particular file size.

PHP script for upload file:


                       Your form should be like this:

<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>

                where,
                          enctype="multpart/form-data  -  must be present in form. Otherwise file does not upload.
                         type="file" in <input>  tag is used to take input as a file.

The php script for upload file is:

<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
       <input type="submit" name="submit" value="Upload">
</form>
<?php
 if(isset($_POST['submit']))
 {
   $name=$_FILES['file']['name'];
   if(file_exists(dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/files/'.$name))
   {
    echo'file is already present';
   }
   else
   {
    move_uploaded_file($_FILES['file']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/files/'.$name);
     echo'file added';
}
 }
?>
</body>
</html>

  where,

  

$_FILES:


                   It is the php global variable. It keeps information about uploaded file like below.
  $_FILES['file']['name'] - name of uploaded file.
  $_FILES['file']['type'] - type of uploaded file.
  $_FILES['file']['size'] - size of uploaded file.
  $_FILES['file']['tmp_name'] - temporary name of uploaded file which is in server.
  $_FILES['file']['error'] - errors while uploadeding file.
 ' file' is the name of <input> tag
  $_SERVER['DOCUMENT_ROOT'] - return the directory.
  move_uploaded_file()
 - uploaded file to specified directory.

                  Now you can upload any file without any restriction using php. The file is stored in '/upload/file/' directory already if it is not present in the directory. Otherwise you'll get error message ' file is laready present'.
                  

13 Oct 2014

Parse / read data from XML file using PHP

                                 You can get value from XML using PHP in following two ways.

1. Get data from XML using simple_load_file() in PHP
2. Get data from XML using file_get_contents() and SimpleXMLElement() in PHP

Lets see below examples.
 If the XML file looks like below, then you can retrieve node values from XML file using following two ways.

<?xml version="1.0" encoding="utf-8"?>
 <userlist>
    <user>
       <name>Clark</name>
       <state>California</state>
       <country>USA</country>
   </user>
   <user>
      <name>Smith</name>
      <state>London</state>
      <country>United Kingdom</country>
   </user>
   <user>
      <name>Nathan</name>
      <state>Franche Comte</state>
      <country>France</country>
   </user>
   <user>
      <name>Nastya</name>
      <state>Moscow</state>
      <country>Russia</country>
   </user>
</userlist>

Get data from XML  using simple_load_file() in PHP


                                   Simple_load_file() in PHP is used to convert XML document into an object. The below example explain retrieve data from XML file.


<?php
$xml=simplexml_load_file('sample.xml');
 //print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>

Get data from XML file using children() with simplexml_load_file() in PHP

          
                       The children() find the children of the specific node. Consider below example to parsing XML values using PHP.


<?php
 $xml=simplexml_load_file('sample.xml');
 //print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml->children() as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>

Get data from XML using file_get_contents and SimpleXMLElement in PHP


                                  File_get_contents() is used to read the contents of a file into string. SimpleXMLElement is used to represents elements of XML document. The below example explain retrieve data from XML file.

<?php
$url=file_get_contents('sample.xml');
 $xml=new SimpleXMLElement($url);
// print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>


 When you give print_R() in above PHP file, you can see the output on screen like below.


  SimpleXMLElement Object (
  [user] => Array (
     [0] => SimpleXMLElement Object (
   [name] => Clark
[state] => California
[country] => USA )
[1] => SimpleXMLElement Object (
   [name] => Smith
[state] => London
[country] => United Kingdom )
[2] => SimpleXMLElement Object (
   [name] => Nathan
[state] => Franche Comte
[country] => France )
[3] => SimpleXMLElement Object (
   [name] => Nastya
[state] => Moscow
[country] => Russia )
 )
)



                You can get output as below

NameStateCountry
ClarkCaliforniaUSA
SmithLondonUnited Kingdom
NathanFranche ComteFrance
NastyaMoscowRussia
 
Now you can retrieve data from XML file. You can parsed data from XML using PHP.

8 Oct 2014

Parse / Retrieve data from json file using php

                       You know how to create JSON file using PHP. Then we are going know how to get data from json file.

                       That means,  The json file returns encoded values. You have to decode it to display at wherever you wants. You can get data string from JSON using php.

                       The JSON file returns value as an array format, object format or combined both. See my previous post How to create JSON file using PHP.
                       In previous post, It give output like this:

       [    
           {
              'tutorial':'PHP',
              'link':'http://www.phponwebsites.com/p/php.html'
           },
           {
              'tutorial':'Mysql',
              'link':'http://www.phponwebsites.com/p/mysql.html'
           },
           {
            'tutorial':'htaccess',
            'link':'http://www.phponwebsites.com/p/htaccess.html'
           }    
      ]

Get data from JSON file using curl in PHP


               Now, you retrieve data from JSON file using PHP. Consider the following PHP script for retrieve data from JSON file.

  <?php

    $ch = curl_init("http://www.phponwebsites.com/json.php"); // add your url which contains json file
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($content, true);
    //print_R($json);
    $count=count($json);
    echo'<table><th>Tutorial</th><th>Link</th>';
    for($i=0;$i<$count;$i++)
    {
      echo'<tr><td>'.$json[$i]['tutorial'].'</td><td>'.$json[$i]['url'].'</td></tr>';
    }
    echo'</table>';

?>

      where,
                  json_decode() - takes JSON encoded values and converts it to PHP variable
If you want to know about cURL, then visit php.net

Get data from JSON file using file_get_contents in PHP



 <?php
    $content=file_get_contents("http://www.phponwebsites.com/json-api.php");  // add your url which contains json file
    $json = json_decode($content, true);
   // print_R($json);
    $count=count($json);
    echo'<table><th>Tutorial</th><th>Link</th>';
    for($i=0;$i<$count;$i++)
    {
      echo'<tr><td>'.$json[$i]['tutorial'].'</td><td>'.$json[$i]['url'].'</td></tr>';
    }
   echo'</table>';
  ?>

           
       where,
                   file_get_contents() - reads whole values in file into a string

              When you print_r() the results, you will get output like this:

Array (
         [0] => Array ( [PHP] => Differences The Ranch House
                               [link] => http://www.phponwebsites.com/p/php.html
                             )
         [1] => Array ( [Mysql] => Kids Bus Kiss
                               [link] => http://www.phponwebsites.com/p/mysql.html
                            )
         [2] => Array ( [htaccess] => Beehive Hideout
                                [link] => http://www.phponwebsites.com/p/htaccess.html
                           )
          )

Finally, you will get output like this:

TutorialLink
PHPhttp://www.phponwebsites.com/p/php.html
Mysqlhttp://www.phponwebsites.com/p/mysql.html
htaccesshttp://www.phponwebsites.com/p/htaccess.html


Related Post:
Create XML file using PHP and MySQL
Create Excel file using PHP and MySQL
Create Sitemap using PHP and MySQL
Create RSS Feed using PHP and MySQL
Get / Parse data from XML Sitemap using PHP
Read data from RSS Feed using PHP
Parse data from XML file using PHP