2 days ago I was thinking what should I write next.. I was on w3schools web page and I read that XML is for storing data. I already knew it but it gave me an idea to write a tutorial how to store images in XML files.
I really hope you like it helped you somehow and you enjoy reading it. ^^
So.. let's start.
Let's create our image.xml file
<?xml version="1.0" ?> <database> </database>
First we are going to build our class and add a function in it. First function will be image uploading form.
<?php
class img_to_xml{
function upload_form(){
echo "<form action='index.php?act=upload' method='post' enctype='multipart/form-data'>"
."Select your file:<br />"
."<input type='file' name='image' /> "
."<input type='submit' value='Upload' />"
."</form>";
}It will look like this:

<?php - Starts our PHP code
class img_to_xml{ - Starts our class
function upload_form(){ - Starts our function
echo "<form action='index.php?act=upload' method='post' enctype='multipart/form-data'>"
."Select your file:<br />"
."<input type='file' name='image' /> "
."<input type='submit' value='Upload' />"
."</form>"; - This is our form. It has a file input and submit button
}
Now when it's done.. We must create another function.. this will upload your image into XML file. But first let's declare our variables.
function process_upload(){
$filename = addslashes(htmlentities(htmlspecialchars($_FILES['image']['name'])));
$filesize = $_FILES['image']['size'];
$tmpfile = $_FILES['image']['tmp_name'];
$filetype = $_FILES['image']['type'];
$date = date("Y-m-d H:i:s");
$maxsize = "512000";
$atypes = array("image/jpeg", "image/png", "image/gif");function process_upload(){ - This starts our function
$filename = addslashes(htmlentities(htmlspecialchars($_FILES['image']['name']))); - This gets the file's name
$filesize = $_FILES['image']['size']; - It get's the file size
$tmpfile = $_FILES['image']['tmp_name']; - It shows us where our image is temporary uploaded
$filetype = $_FILES['image']['type']; - This gets the file's type
$date = date("Y-m-d H:i:s"); - It gets current date
$maxsize = "512000"; - This shows how big your file can be. 512000 bytes is 500kb
$atypes = array("image/jpeg", "image/png", "image/gif"); - This shows what file types are allowed
Now let's check our uploaded file. What type it is and is it allowed to upload this type of image.
if(!in_array($filetype, $atypes)){
die("You must upload correct image. This image type is not allowed!");
}else{if(!in_array($filetype, $atypes)){ - It checks is this type in array that we created
die("You must upload correct image. This image type is not allowed!"); - If it's not.. You will see an error message
}else{ - But if it is.. let's continue..
Now let's check image's size.
if($filesize>$maxsize){
die("Your file is too big! Please upload smaller file.");
}else{if($filesize>$maxsize){ - If our file size is bigger than it's allowed..
die("Your file is too big! Please upload smaller file."); - .. let's display an error
}else{ - If it's not.. let's continue
Now since everything is fine.. let's begin uploading. First we must open our temporary file and get the content of our image.
$open = fopen($tmpfile, "r");
$read = base64_encode(fread($open, $filesize));
if(!$read){
die("There has been a problem!");
}else{ $open = fopen($tmpfile, "r"); - It opens our temporary file for reading
$read = base64_encode(fread($open, $filesize)); - This reads and encodes this file for XML file
if(!$read){ - If we can't read it..
die("There has been a problem!"); - .. let's display an error
}else{ - If everything is fine.. let's continue
Now it's time for inserting your image into XML file.
$xml = new SimpleXMLElement("image.xml", null, true);
$id = count($xml->image)+1;
$content = "<image id=\"$id\">
<name>$filename</name>
<date>$date</date>
<type>$filetype</type>
<content>
$read
</content>
</image>
";
$filename = "image.xml";
$open = fopen($filename, "r");
$contents = fread($open, filesize($filename));
fclose($open);
$row = explode("\n", $contents);
$num = count($row)-2;$xml = new SimpleXMLElement("image.xml", null, true); - It creates a XML object
$id = count($xml->image)+1; - Let's count now many images we have and raise ID by 1 number. It will be our image's ID
$content = "<image id=\"$id\">
<name>$filename</name>
<date>$date</date>
<type>$filetype</type>
<content>
$read
</content>
</image>
"; - This is template for our image. This is what will be added to xml file.
$filename = "image.xml"; - This is our XML file
$open = fopen($filename, "r"); - Now let's open it ..
$contents = fread($open, filesize($filename)); - .. and let's get everything that's in it
fclose($open); - Let's close this file
$row = explode("\n", $contents); - This chops our file into pieces where is break
$num = count($row)-2; - Let's count all rows and remove 2 lines from the end
Now let's close our XML that will go to this XML file.
for($i=0; $i<=$num; $i++){
$header .= $row[$i]."\n";
}
$cont .= $header."".$content;
$cont .= "</database>";for($i=0; $i<=$num; $i++){
$header .= $row[$i]."\n";
} - Let's get all rows that we need
$cont .= $header."".$content; - Now let's put everything in row ..
$cont .= "</database>"; - .. and let's end our image database
Everything is almost done..
unlink("image.xml");
$open = fopen("image.xml", "a");
if(fwrite($open, $cont)){
echo "Your file has been successfully uploaded. <a href='view.php?id=".$id.">View</a>";
fclose($open);
}else{
echo "Can not upload your image.";
}unlink("image.xml"); - Let's delete our old image.xml file ..
$open = fopen("image.xml", "a"); - .. and let's open a new one for writing
if(fwrite($open, $cont)){ - If everything is good and file was written ..
echo "Your file has been successfully uploaded. <a href='view.php?id=".$id.">View</a>";
It looks like this:

fclose($open); - .. let's show success message
}else{ - But if something was wrong ..
echo "Can not upload your image."; - .. let's display an error
} - Close else
Now let's close our elses and ifs and put our code to work. Everything is done.. That was so simple ^^. Here's the full code:
<?php
class img_to_xml{
function upload_form(){
echo "<form action='index.php?act=upload' method='post' enctype='multipart/form-data'>"
."Select your file:<br />"
."<input type='file' name='image' /> "
."<input type='submit' value='Upload' />"
."</form>";
}
function process_upload(){
$filename = addslashes(htmlentities(htmlspecialchars($_FILES['image']['name'])));
$filesize = $_FILES['image']['size'];
$tmpfile = $_FILES['image']['tmp_name'];
$filetype = $_FILES['image']['type'];
$date = date("Y-m-d H:i:s");
$maxsize = "512000";
$atypes = array("image/jpeg", "image/png", "image/gif");
if(!in_array($filetype, $atypes)){
die("You must upload correct image. This image type is not allowed!");
}else{
if($filesize>$maxsize){
die("Your file is too big! Please upload smaller file.");
}else{
$open = fopen($tmpfile, "r");
$read = base64_encode(fread($open, $filesize));
if(!$read){
die("There has been a problem!");
}else{
$xml = new SimpleXMLElement("image.xml", null, true);
$id = count($xml->image)+1;
$content = "<image id=\"$id\">
<name>$filename</name>
<date>$date</date>
<type>$filetype</type>
<content>
$read
</content>
</image>
";
$filename = "image.xml";
$open = fopen($filename, "r");
$contents = fread($open, filesize($filename));
fclose($open);
$row = explode("\n", $contents);
$num = count($row)-2;
for($i=0; $i<=$num; $i++){
$header .= $row[$i]."\n";
}
$cont .= $header."".$content;
$cont .= "</database>";
unlink("image.xml");
$open = fopen("image.xml", "a");
if(fwrite($open, $cont)){
echo "Your file has been successfully uploaded. <a href='view.php?id=".$id.">View</a>";
fclose($open);
}else{
echo "Can not upload your image.";
}
}
}
}
}
}
$action = new img_to_xml();
$act = addslashes(htmlentities(htmlspecialchars($_GET['act'])));
if($act != "" && $act == "upload"){
$action->process_upload();
}else{
$action->upload_form();
}
?>Regards,
Jaan

Sign In
Register
Help



MultiQuote