When we develop a web application without using a database, so it becomes very difficult to save any attributes which can be used later for show any item on page. XML could be better option to save data and read data where database is not available. We can write values in XML file and retrieve from that file.
XML database is document oriented database and fall in NoSql category of database. We don't go into concept of this database. Just look a example of saving data in XML database using PHP as well as how we can upload file in a folder and save its some attribute value in XML file/database.
Let see by an example.
First create a form that can upload a file with some its value.
<form method="post" action="profile.php" enctype="multipart/form-data">
<centet>
<input type="hidden" name="create_xml" value="true">
<label for="name">Article Title: </label>   <input type="text" name="name"><br><br>
<label for="textbox">Write something: </label>   <textarea name="textbox" rows="5" cols="40"></textarea><br><br>
<label for="fileToUpload">Upload a Article(PDF-file:) </label>  
<input type="file" name="file" id="file-select"><br><br>
<label for="filenane">Article File Name </label><input type="text" name="filename" placeholder="Enter the name of your Article">   <br><br>
<input type="submit" value="Submit Article" name="submit">
<center>
</form>
Now write PHP code to receive the form data and upload file to folder. (I assume that you are aware of sending and receiving values from form.)
Create a folder "upload" in your project root directory.
To upload folder -
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$textbox=$_POST['textbox'];
$filename=$_POST['filename'];
$file=basename( $_FILES['file']['name']);
$targetfolder = "upload/";
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
{
echo "<script>";
echo "alert('Your file is uploaded succesfully');";
echo " window.location.href='profile.php';";
echo "</script>";
echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
}
else {
echo "<script>";
echo "alert('Error !!!!! Please try again');";
echo " window.location.href='profile.php';";
echo "</script>";
}
}
Now PHP code which create XML file to write data in append mode.
$books = array();
$books [] = array(
'name' => $name,
'textbox' => $textbox,
'filename' => $filename,
'file'=> $file
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "textbox" );
$author->appendChild(
$doc->createTextNode( $book['textbox'] )
);
$b->appendChild( $author );
$title = $doc->createElement( "name" );
$title->appendChild(
$doc->createTextNode( $book['name'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "filename" );
$publisher->appendChild(
$doc->createTextNode( $book['filename'] )
);
$b->appendChild( $publisher );
$publisher1 = $doc->createElement( "file" );
$publisher1->appendChild(
$doc->createTextNode( $book['file'] )
);
$b->appendChild( $publisher1 );
$r->appendChild( $b );
}
$handle=fopen("data.xml","a+");
fwrite($handle,$doc->saveXML());
fclose($handle);
Note that this code has a demerit that it always add XML start declaration tag, when we repeatedly write the data on same file. So what is the solution to parse or read this file? Solution is to read this file using regular expression. Read this post.
XML database is document oriented database and fall in NoSql category of database. We don't go into concept of this database. Just look a example of saving data in XML database using PHP as well as how we can upload file in a folder and save its some attribute value in XML file/database.
Let see by an example.
First create a form that can upload a file with some its value.
<form method="post" action="profile.php" enctype="multipart/form-data">
<centet>
<input type="hidden" name="create_xml" value="true">
<label for="name">Article Title: </label>   <input type="text" name="name"><br><br>
<label for="textbox">Write something: </label>   <textarea name="textbox" rows="5" cols="40"></textarea><br><br>
<label for="fileToUpload">Upload a Article(PDF-file:) </label>  
<input type="file" name="file" id="file-select"><br><br>
<label for="filenane">Article File Name </label><input type="text" name="filename" placeholder="Enter the name of your Article">   <br><br>
<input type="submit" value="Submit Article" name="submit">
<center>
</form>
Now write PHP code to receive the form data and upload file to folder. (I assume that you are aware of sending and receiving values from form.)
Create a folder "upload" in your project root directory.
To upload folder -
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$textbox=$_POST['textbox'];
$filename=$_POST['filename'];
$file=basename( $_FILES['file']['name']);
$targetfolder = "upload/";
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
{
echo "<script>";
echo "alert('Your file is uploaded succesfully');";
echo " window.location.href='profile.php';";
echo "</script>";
echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
}
else {
echo "<script>";
echo "alert('Error !!!!! Please try again');";
echo " window.location.href='profile.php';";
echo "</script>";
}
}
Now PHP code which create XML file to write data in append mode.
$books = array();
$books [] = array(
'name' => $name,
'textbox' => $textbox,
'filename' => $filename,
'file'=> $file
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "textbox" );
$author->appendChild(
$doc->createTextNode( $book['textbox'] )
);
$b->appendChild( $author );
$title = $doc->createElement( "name" );
$title->appendChild(
$doc->createTextNode( $book['name'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "filename" );
$publisher->appendChild(
$doc->createTextNode( $book['filename'] )
);
$b->appendChild( $publisher );
$publisher1 = $doc->createElement( "file" );
$publisher1->appendChild(
$doc->createTextNode( $book['file'] )
);
$b->appendChild( $publisher1 );
$r->appendChild( $b );
}
$handle=fopen("data.xml","a+");
fwrite($handle,$doc->saveXML());
fclose($handle);
Note that this code has a demerit that it always add XML start declaration tag, when we repeatedly write the data on same file. So what is the solution to parse or read this file? Solution is to read this file using regular expression. Read this post.
Now complete working code -
<form method="post" action="profile.php" enctype="multipart/form-data">
<centet>
<br>
<input type="hidden" name="create_xml" value="true">
<label for="name">Article Title: </label>   <input type="text" name="name"><br><br>
<label for="textbox">Write something: </label>   <textarea name="textbox" rows="5" cols="40"></textarea><br><br>
<label for="fileToUpload">Upload a Article(PDF-file:) </label>  
<input type="file" name="file" id="file-select"><br><br>
<label for="filenane">Article File Name </label><input type="text" name="filename" placeholder="Enter the name of your Article">   <br><br>
<input type="submit" value="Submit Article" name="submit">
<center>
</form>
<?php
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$textbox=$_POST['textbox'];
$filename=$_POST['filename'];
$file=basename( $_FILES['file']['name']);
$targetfolder = "upload/";
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
{
$books = array();
$books [] = array(
'name' => $name,
'textbox' => $textbox,
'filename' => $filename,
'file'=> $file
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "textbox" );
$author->appendChild(
$doc->createTextNode( $book['textbox'] )
);
$b->appendChild( $author );
$title = $doc->createElement( "name" );
$title->appendChild(
$doc->createTextNode( $book['name'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "filename" );
$publisher->appendChild(
$doc->createTextNode( $book['filename'] )
);
$b->appendChild( $publisher );
$publisher1 = $doc->createElement( "file" );
$publisher1->appendChild(
$doc->createTextNode( $book['file'] )
);
$b->appendChild( $publisher1 );
$r->appendChild( $b );
}
$handle=fopen("data.xml","a+");
fwrite($handle,$doc->saveXML());
fclose($handle);
echo "<script>";
echo "alert('Your file is uploaded successfully');";
echo " window.location.href='profile.php';";
echo "</script>";
echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
}
else {
echo "<script>";
echo "alert('Error !!!!! Please try again');";
echo " window.location.href='profile.php';";
echo "</script>";
}
}
?>
XML file -
No comments:
Post a Comment