Archive for the ‘PHP’ Category
Read XML with PHP
The XMLReader class in php can be used to read XML (though it is still quite limited in its function…).
The manual can be found here and I will show a short example of how to use this class:
$xml = new XMLReader(); $xml->open("simple.xml"); while ($xml->read()) { $tagname = $xml->name; if($tagname="image") { $filename = $xml->getAttribute("filename"); $width = $xml->getAttribute("width"); $height = $xml->getAttribute("height"); $xpos = $xml->getAttribute("xpos"); $ypos = $xml->getAttribute("ypos"); } } $xml->close(); |
The corresponding XML-file was:
<?xml version="1.0" encoding="UTF-8"?> <pdffile> <image xpos="20" ypos="100" width="500" height="334" filename="old_window.jpg"> <border size="2" margin="3" color="#336699" /> </image> <text value="Hier steht ein Fließtext \nmit Umlauten und Zeilenumbrüchen \neben auch um zu Testen ob das geht.\n"> <prop xpos="30" ypos="30" width="500" height="0" spacing="9"/> <format> <style value="bold" start="0" end="3"/> <style value="italic" start="0" end="20"/> <color value="#000033" start="7" end="10"/> <fontfamily value="Times" start="12" end="18"/> <fontsize value="18" start="15" end="23"/> </format> </text> </pdffile> |
PDF with PHP
Creating a PDF with PHP can be very simple with the FPDF library, but unfortunately it is still quite limited…
Anyway, here is a short howto.
First download the package here, unpack it and put the directory where you have full access.
Here is an example of how to use it.
require_once("path/to/fpdf16/fpdf.php"); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',12); $pdf->Text($xpos, $ypos, $Text); $pdf->Output("MissingFiles.pdf", "F"); |
Please have a good look at the manual.
Zip and unzip with PHP
To open and extract a zip archive, use the ZipArchive class in PHP:
$zip = new ZipArchive; if($zip->open($zip_file) == 1) { for ($i=0; $i<$zip->numFiles; $i++) { $ArchiveFileName=$zip->getNameIndex($i); } $zip->extractTo($target_path); $zip->close(); } |
To create a new archive, have a look at the following example:
$zip = new ZipArchive; if ($zip->open("Download.zip", ZipArchive::CREATE)===TRUE) { $zip->addFile($filename, $fileNameInArchive); $zip->close(); } |
To add files to an existing archive, use the same addFile() function, but don’t open the archive in the CREATE mode. Check out the manual for more details.
You are currently browsing the archives for the PHP category.
