Monday, October 12, 2009

Using SimpleXML To Read & Parse XML in PHP

Using PHP we can read and execute the XML files. We can use the XML files to store the information as we do with the database.

PHP provides all the methods needed for the reading, writing and executing the XML files.Before that we need to know the XML.

An XML document comprises elements, attributes, processing instructions, comments, and entities

Element: Text delimited by an opening and a closing tag. A tag is a name enclosed within angle brackets.

Attribute: A piece of qualifying information for an element. An attribute consists of a name, an equals sign, and an attribute value delimited by either single-quotes or double-quotes.

Processing instruction: The software that is reading an XML document is referred to as a processor. A processing instruction is additional information embedded in the document to inform the processor and possibly change its behaviour.

Comment: An XML comment begins with the characters: less-than, exclamation mark, minus, minus; and ends with the characters: minus, minus, greater-than. Any text within a comment is intended for a human reader and is ignored by the processor.

Entity: An entity is a compact form that represents other text. Entities are used to specify problematic characters and to include slabs of text defined elsewhere. An entity reference consists of an ampersand, a name, and a semi-colon.

A simple XML file we will

<?xml version="1.0" encoding="utf-8" ?>

<people title="students"
>

<name1> student name 1
</name1>

<name2> student name 2
</name2>

<name3> student name 3
</name3>

</people>

Steps for reading this simple XML file is:

$xml = simplexml_load_file('names.xml');
This step will load the xml files. It Interprets an XML file into an object.Returns an object of class SimpleXMLElement with properties containing the data held within the XML document. On errors, it will return FALSE.

To read the data from the simple xml file we write code as
$xml = simplexml_load_file('names.xml');
print $xml->name1;
# Seperates outputs (easier to read & understand)
print "

";
# You call an attribute just like you would with an array: $array['arrayname/number']
print $xml['title'];

outputs : student name 1 ## Title output is -- name1

For an XML with multiple Items we write code as

<?xml version="1.0" encoding="utf-8" ?>
<student title="students">

<item id="1">
<name>
<first>ram</first>
<last>test</last>
</name>
<ageᡢ</age>
</item>

<item id="2">
<name>
<first>prince</first>
<last>kumar</last>
</name>
<ageᡃ </item>

<item id="3">
<name>
<first>raju</first>
<last>ramesh</last>
</name>
<ageᡍ</age>
</item>

</student>



# Load the test.xml file
$xml = simplexml_load_file('test.xml');
# Start a foreach loop. Translation: for every in the xml file put it into the var $item.
# now the $item can display all the elements inside the
foreach($xml->item as $item) {
# These three print's will display the attribute of the (ID), display the first and last name joined together
# and then the age. The
and

are for spacing out the results
print "ID: " . $item['id'] . "
";
print "Name: " . $item->name->first . " " . $item->name->last . "
";
print "Age: " . $item->age . "

";
}

If we want to provide the conditions when executing the XML files then the code will be

# Load the test.xml file
$xml = simplexml_load_file('test.xml');
# Start a foreach loop. Translation: for every in the xml file put it into the var $item.
# now the $item can display all the elements inside the
foreach($xml->item as $item) {
$age = $item->age;
if ($age >= 10) {
if ($age <= 21) {
# These three print's will display the attribute of the (ID), display the first and last name joined together
# and then the age. The
and

are for spacing out the results
print "ID: " . $item['id'] . "
";
print "Name: " . $item->name->first . " " . $item->name->last . "
";
print "Age: " . $item->age . "

";
}
}
}

Leia Mais…

Monday, October 5, 2009

Download functionality in PHP

The common usable component in any of the website is to provide the user with the download option. when providing the download option we cannot restrict the user with one the specific type like doc or ppt or pdf... We need to provide the most convenient way to the user so that the user can easily download the files.

Below is such script where we can have the multiple options provided.

$filename is the file name which we want to download

$file_extension = strtolower(substr(strrchr($filename,"."),1));

$folderpath="foldername/".$filename;

if( $filename == "" )
{
echo "No File Found";
exit;
} elseif ( ! file_exists( $folderpath ) )
{
echo "File Not Found";
exit;
};

// If file exits then it is ready for download.

switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}

// Decides the header... which type if download you are providing to the user.

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$folderpath");
exit();


Copy the above code if you want to download any type of the file in the given options

Leia Mais…