Log in

View Full Version : PHP writes XML of jpegs in folder



atarichris
07-10-2008, 09:01 PM
Hi,
Im trying to write some PHP that writes an XML of jpegs in a folder BASED on the upload/modified date. I have a code that that does it in alpha-numeric order... can anyone help?



<?
$xml = '<?xml version="1.0"?'.'><images>';
$handle = opendir(".");
$doc = "gallery.xml";
$handle2 = fopen($doc, 'w') or die("can't open file");
while ( ($file=readdir($handle)) !== false ) {
if ( !is_dir($file) ){
$pic = @getimagesize($file);

if($pic != false && $pic[2] == 2){
$xml .= '<pic><image>/images/galleries/'.$file.'</image><width>'.$pic[0].'</width><height>'.$pic[1].'</height></pic>';
}
}
}
$xml .= "</images>";
fwrite($handle2, $xml);
fclose($handle2);
echo $xml;
?>

techietim
07-10-2008, 09:25 PM
<?php
$doc = "gallery.xml";
///////////
$xml = '<?xml version="1.0"?><images>';
foreach(glob('./*.jpg') as $file){
$pic = @getimagesize($file);
$xml .= '<pic><image>/images/galleries/'.substr($file, 2).'</image><width>'.$pic[0].'</width><height>'.$pic[1].'</height></pic>';
}
$xml .= "</images>";
file_put_contents($doc, $xml);
echo $xml;

atarichris
07-10-2008, 09:36 PM
does this code put the files in order of most recently added to the folder?

techietim
07-10-2008, 10:22 PM
No, but this one does:


<?php
$doc = "gallery.xml";
///////////
function sortMod($file1, $file2){
$file1 = filectime($file1);
$file2 = filectime($file2);
if($file1 == $file2)
return 0;
return $file1 < $file2 ? -1:1;
}
$xml = '<?xml version="1.0"?><images>';
$list = glob('./*.jpg');
usort($list, 'sortMod');
foreach($list as $file){
$pic = @getimagesize($file);
$xml .= '<pic><image>/images/galleries/'.substr($file, 2).'</image><width>'.$pic[0].'</width><height>'.$pic[1].'</height></pic>';
}
$xml .= "</images>";
file_put_contents($doc, $xml);
echo $xml;