PHP - Php Xml From The Contents Of A Directory
I have this script below that makes an XML which displays the contents of a directory which contains image files.
After running the .php, the XML output looks like this: Code: [Select] <gallery> <content src="./uploaded/images/Desert.jpg"/> <content src="./uploaded/images/Penguins.jpg"/> <content src="./uploaded/images/tux.png"/> <content src="./uploaded/images/Jellyfish.jpg"/> <content src="./uploaded/images/Chrysanthemum.jpg"/> <content src="./uploaded/images/Tulips.jpg"/> <content src="./uploaded/images/Koala.jpg"/> </gallery> I would like the output to be like: Code: [Select] <gallery> <photos> <photo> <content src="./uploaded/images/Desert.jpg"/> </photo> <photo> <content src="./uploaded/images/Penguins.jpg"/> </photo> <photo> <content src="./uploaded/images/tux.png"/> </photo> <photo> <content src="./uploaded/images/Jellyfish.jpg"/> </photo> <photo> <content src="./uploaded/images/Chrysanthemum.jpg"/> </photo> <photo> <content src="./uploaded/images/Tulips.jpg"/> </photo> <photo> <content src="./uploaded/images/Koala.jpg"/> </photo> </photos> </gallery> Here is the script: Code: [Select] <?php $path_to_image_dir = "./uploaded/images"; // path to your image directory $xml_string = <<<XML <?xml version="1.0" encoding="UTF-8"?> <gallery> </gallery> XML; $xml_generator = new SimpleXMLElement($xml_string); if ( $handle = opendir( $path_to_image_dir ) ) { while (false !== ($file = readdir($handle))) { if ( is_file($path_to_image_dir.'/'.$file) ) { $image = $xml_generator->addChild('content'); $image->addattribute('src', $path_to_image_dir.'/'.$file); } } closedir($handle); } header("Content-Type: text/xml"); echo $xml_generator->asXML(); ?> Similar TutorialsI am trying to use the following code to include a directories contents and was wondering what im doing wrong, because its not working? Code: [Select] if ($handle = opendir('../configs')) { while (false !== ($FILEZ = readdir($handle))) { include "$FILEZ"; } closedir($handle); } Seems this should be easy, but I'm not finding the solution. Functions like scandir, readdir, and glob that I've been looking at all seem to need me to actually know more about my directory path than I can ahead of time. Here's what I want to do: 1. expand a .tar file (consists of directory, subdirectories, files, and is variable) that I've uploaded to the server 2. look through that expanded directory for a subdirectory named ".pn" -- (note that all my .tar files will have this folder deep in several subdirectories that will be variable in number and have different names depending on the original .tar file) 3. copy the entire contents of the .pn folder to another folder already on my server The hosting company, no doubt, has likely blocked some of the functions I'll need as well, but if someone can point me in the right direction to start, it would be greatly appreciated. Thanks! Satre Hi guys, I've been working on a script for a while now, and I'm sure it doesn't look great and all, and it's probably really messed up.. But right now I've finally got it working! There's only 1 thing I'd really like to add.. Searching through & listing of remote directories! The directories I'm trying to list have directory listings enabled, and I think it *should* be possible. I just have no clue how. Here's my current code in a beautiful mix of HTML and PHP: <? $border_size = "0"; function returner($what) { $what=explode("/",$what); $tps=count($what); $what=$what[$tps-1]; return $what; } $page_url= ""; $home_url=returner(__FILE__); if(isset($_GET['q'])) { $qtext=$_GET['q']; } else { $qtext=""; } function getdirsize($directory, $format=FALSE) { $size = 0; if(substr($directory,-1) == '/') { $directory = substr($directory,0,-1); } if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) { return -1; } if($handle = opendir($directory)) { while(($file = readdir($handle)) !== false) { $path = $directory.'/'.$file; if($file != '.' && $file != '..') { if(is_file($path)) { $size += filesize($path); } elseif(is_dir($path)) { $handlesize = getdirsize($path); if($handlesize >= 0) { $size += $handlesize; } else { return -1; } } } } closedir($handle); } if($format == TRUE) { if($size / 1048576 > 1) { return round($size / 1048576, 1).' MB'; } elseif($size / 1024 > 1) { return round($size / 1024, 1).' KB'; } else { return round($size, 1).' bytes'; } } else { return $size; } } if(isset($_GET['type'])){ $type=$_GET['type']; } else { $type="new"; } $textures=0; $models=0; $avatars=0; $seqs=0; $sounds=0; foreach (glob("textures/*.jpg") as $texture){ $textures++; } foreach (glob("models/*.zip") as $model){ $models++; } foreach (glob("avatars/*.zip") as $avatar){ $avatars++; } foreach (glob("seqs/*.zip") as $seq){ $seqs++; } foreach (glob("sounds/*.zip") as $sound){ $sounds++; } ?> <!DOCTYPE html> <html> <head> <title>ObjectPath Search</title> <style type="text/css"> #wrapper { width: 850px; margin: 30px auto 30px auto; padding: 10px; } body { color:#C6C6C6; background:#1E1E1E; /* margin:0; padding:0; */ overflow-x:hidden; } #tabs { font: 85% "Trebuchet MS", sans-serif; } .left { float: left; } .right { float: right; } a:link, a:visited, a:active { color: #3DB015; text-decoration: none; } a:hover { color: #00E0FF; } h2 { color: #3DB015; padding-bottom: 0.2em; font-size: 110%; } ul#icon {margin: 0; padding: 0;} ul#icon li {margin: 1px; position: relative; padding: 1px 0; cursor: pointer; float: left; list-style: none;} ul#icon span.ui-icon {float: left; margin: 0 1px;} </style> <link type="text/css" href="http://objects.jk-hosting.com/search/css/black-tie/jquery-ui-1.8.2.custom.css" rel="stylesheet" /> <script type="text/javascript" src="http://objects.jk-hosting.com/search/js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="http://objects.jk-hosting.com/search/js/jquery-ui-1.8.2.custom.min.js"></script> <script type="text/javascript"> function formHandler(form){ var URL = document.form.site.options[document.form.site.selectedIndex].value; window.location.href = URL; }; $(function(){ // Tabs $('#tabs').tabs(); }); </script> </head> <body> <div id="wrapper"> <div id="tabs"> <!-- Tabs start --> <ul> <li><a href="#tab-search">Search</a></li> <li><a href="#tab-list">List Objects</a></li> <li><a href="#tab-info">OP info</a></li> </ul> <div id="tab-search"><!-- Searchtab start --> Please enter a string to search for, and choose a folder to search in. <br /><br /> <form name="Search"> <input type='hidden' value='search' name='type'> <input value='<? print $qtext; ?>' type='text' name='q'> <select name='map'> <option selected='selected' value='models'>Models</option> <option value='avatars'>Avatars</option> <option value='textures'>Textures</option> <option value='seqs'>Seqs</option> <option value='sounds'>Sounds</option></select> <input type='submit' value='Search'> </form> </div> <!-- Searchtab end --> <div id="tab-list"><!-- Listtab start --> Please pick a folder to browse. <br /><br /> <form name="form"> <select name="site" onChange="javascript:formHandler()"> <option value="#">Look in folder...</option> <option value="<? print $page_url; ?>?type=list&map=models">Models</option> <option value="<? print $page_url; ?>?type=list&map=avatars">Avatars</option> <option value="<? print $page_url; ?>?type=list&map=textures">Textures</option> <option value="<? print $page_url; ?>?type=list&map=seqs">Seqs</option> <option value="<? print $page_url; ?>?type=list&map=sounds">Sounds</option> </select> </form> </div> <!-- Listtab end --> <div id="tab-info"><!-- Info tab start --> The OP currently contains: <br /><br /> <table> <tr><td><b><? echo $models; ?></b></td> <td>Models</td></tr> <tr><td><b><? echo $avatars; ?></b></td> <td>Avatars</td></tr> <tr><td><b><? echo $textures; ?></b></td> <td>Textures</td></tr> <tr><td><b><? echo $seqs; ?></b></td> <td>Seqs</td></tr> <tr><td><b><? echo $sounds; ?></b></td> <td>Sounds</td></tr> </table> </div> <!-- Info tab end --> </div> <!-- Tabs end --> </div> <!-- Start PHP generated content --> <? if($type=="search" || $type=="list") { $M=$_GET['map']; if($type=="search") { $Q=$_GET['q']; $empty="Nothing found with <b>\"" . $Q . "\"</b> in it's name.<br />\nPlease make a more general search query, or try a different folder.\n\n"; } else { $Q=""; $empty='This folder is empty'; } if($M=="textures") { $ext="jpg"; } else { $ext="zip"; } $i=0; print "<hr>\n"; $endfile=array(); $endsize=array(); $endsize2=array(); foreach (glob($M."/*".$Q."*.".$ext) as $filename) { $filename = explode(".", $filename); $filename=$filename[0]; $filename = explode("/", $filename); $filename=$filename[1]; $i++; $endfile[$i]=$filename; if($ext=="jpg") { $endfile[$i]="<a name='".$endfile[$i]."' href='".$pageurl."?type=view&name=".$endfile[$i]."&folder=".$M."&from=".$type."&addon=".$Q."'>".$endfile[$i]."</a>"; } $endsize[$i]=$size; $endsize2[$i]=$size2; } if($i != 1) { print "<b>".$i."</b> items were found.\n<hr>\n"; } else { print "<b>".$i."</b> item was found.\n<hr>\n"; } echo("<table width='100%' border='" . $border_size . "' cellspacing='0' cellpadding='0' >\n"); if($i!=0) { for ($t = 1; $t < $i; $t++) { $thumbfile = $M."/".$endfile[$t].'.jpg'; if(file_exists($thumbfile)) { $thumbnail = "<a name='".$endfile[$t]."' href='".$page_url."?type=view&name=".$endfile[$t]."&folder=".$M."&from=".$type."&addon=".$Q."'><ul id='icon'><li class='ui-state-default ui-corner-all' title='".$endfile[$t]."'><span class='ui-icon ui-icon-image'></span></li></ul></a>"; } else { $thumbnail = ""; } if($t=="1") { echo("<tr><td width='10%'>Number</td><td width='3%'><ul id='icon'><li class='ui-state-default ui-corner-all' title='".$endfile[$t]."'><span class='ui-icon ui-icon-image'></span></li></ul></td><td width='60%'>Name</td></tr>\n"); } echo("<tr><td>" . $t . "</td><td>".$thumbnail."</td><td>" . $endfile[$t] . "</td></tr>\n"); flush(); } $thumbfile = $M."/".$endfile[$t].'.jpg'; if(file_exists($thumbfile)) { $thumbnail = "<a name='".$endfile[$t]."' href='".$page_url."?type=view&name=".$endfile[$t]."&folder=".$M."&from=".$type."&addon=".$Q."'><ul id='icon'><li class='ui-state-default ui-corner-all' title='".$endfile[$t]."'><span class='ui-icon ui-icon-image'></span></li></ul></a>"; } else { $thumbnail = ""; } echo("<tr><td>" . $t . "</td><td>".$thumbnail."</td><td>" . $endfile[$t] . "</td></tr>\n"); } print "</table>\n"; if($i=="0") { print $empty; } } elseif($type=="view") { $filename=$_GET['name']; $folder=$_GET['folder']; if($_GET['from']=="list"){ $addon="?type=list&map=".$folder."#".$filename; } if($_GET['from']=="search"){ $addon="?type=search&q=".$filename."&map=".$folder."#".$filename; } print"<center><a href='".$home_url."'>Home</a></center>"; print "<hr>\n<center><img src='".$folder."/".$filename.".jpg'></img></center>\n<hr>\n<br />\n<a href='".$page_url."".$addon."'>Previous Page</a>\n"; } $htmlshow=""; if($_GET['type']=="returnOPfile") { if(isset($_GET['split'])) { $splitter=$_GET['split']; } else { $splitter=" | "; } if(isset($_GET['html'])) { $htmlshow="<br />"; } foreach (glob("textures/*.jpg") as $texture){ if(isset($_GET['size'])) { $size=$splitter.filesize($texture); } $texture = explode("/", $texture); $texture=$texture[1]; print "textures".$splitter.$texture.$size."\n".$htmlshow; } foreach (glob("models/*.zip") as $model){ if(isset($_GET['size'])) { $size=$splitter.filesize($model); } $model = explode("/", $model); $model=$model[1]; print "models".$splitter.$model.$size."\n".$htmlshow; } foreach (glob("avatars/*.zip") as $avatar){ if(isset($_GET['size'])) { $size=$splitter.filesize($avatar); } $avatar = explode("/", $avatar); $avatar=$avatar[1]; print "avatars".$splitter.$avatar.$size."\n".$htmlshow; } foreach (glob("seqs/*.zip") as $seq){ if(isset($_GET['size'])) { $size=$splitter.filesize($seq); } $seq = explode("/", $seq); $seq=$seq[1]; print "seqs".$splitter.$seq.$size."\n".$htmlshow; } foreach (glob("sounds/*.zip") as $sound){ if(isset($_GET['size'])) { $size=$splitter.filesize($sound); } $sound = explode("/", $sound); $sound=$sound[1]; print "sounds".$splitter.$sound.$size."\n".$htmlshow; } } ?> <!-- End PHP generated content --> </body> </html> So right now my question to you PHP freaks is, can you please help me edit my script so I can search through a remote directory? *This* is one of the directories I wish to be able to search through & list.. Thanks in advance. Edit; It might help if you know what the site currently looks like. *Click* i have made an delete files script which works for only one directory but not sub directory so i want to delete files of same extention from directory and subdirectory. My current code is Code: [Select] <? $dir = 'hmm/'; function scanr($dir){ $arr = glob($dir.'/*.jpg'); foreach($arr as $vv){ //check if $vv is a file if(is_file($vv)){ //if file, get the filename $vx=explode('/',$vv); $file=$vx[count($vx)-1]; // if no extension delete the file unlink($vv); // print the deletion message echo $vv." deleted!<br>";}else{ // if $vv is a dir then scan it again for files scanr($vv); }} } scanr($dir); ?> I'm trying to echo the directory and sub directory only. I am not looking to show the files contained - only folders. Hello friends, if i have the following $path = "http://*site*.com"; why can't i write the following $ORGtext= file_get_contents('$path'); it works only if i write $ORGtext= file_get_contents('http://*site*.com'); but i want to put $path instead of the url at file_get_contents is it possible and if yes then how ? thank you I want to parse some tiny lines from a webpage. I have few questions: 1. What is faster and better: cURL or file_get_contents? 2. Is it better to run preg_match commands form every line I want OR first get a shorter part (e.g. between <head> tag) then running preg_match for the lines? 3. Actually, I need to load the whole page before parsing. Is there a way to stop loading the whole page. For example, when I want something between <head> tag; stop loading <body>? Thank you in advance! I used this code $limit = 120; if (strlen($summary) > $limit) $summary = substr($summary, 0, strrpos(substr($summary, 0, $limit), ' . . .')); This code ok in English Language it counts letters fine but it is not work fine if I insert Arabic text. It counts Arabic encoding characters. Hi all
So I'm back to being a php newbie after not touching the stuff for years....and straight back to being pretty sure I'm just incredibly stupid.
I want to grab the contents of a url onto a string so that I can search the string and use the contents. I think this should work:
<?php $homepage = file_get_contents('http://www.example.com/'); echo "Homepage = " . $homepage; ?>But it doesn't. What am I doing wrong, or could there be an issue with my server that means it's not able to perform this maybe? (It's working fine for everything so far) Or is there another way of achieving this? Thanks!! What is the proper way to output the contents of a class? (We will assume that Getters and Setters are evil...) Let's say I have the following class... class FormHandler2 { // Define Variables. private $myFormArray; // Constructor. public function __construct($param){ $this->myFormArray = $param; } } ...and I want to be able to output its contents either via a variable dump OR by printing something to the screen. How would I do that? TomTees Hello all, I've run in to a very strange problem that I've never seen before. in my "config.php" file I have something like this... <?php // the URL of the script. NO trailing slash. $xconfig['url'] = $r['cfg_site_url']; // the site name $xconfig['title'] = $r['cfg_site_title']; // the YouTube user name to populate the site with. $xconfig['youtube_user'] = $r['cfg_site_youtube']; // the video to show on the homepage $xconfig['homepage_video'] = $r['cfg_homepage_video']; ?> and in my index.php page, I have <?php include "config.php"; ?> but, also in index.php, I have this <?php echo $xconfig['title'] ; ?> but it won't echo! None of the values in $xconfig[] output anything. I've even done put this in index.php <?php var_dump($xconfig); exit; ?> and that shows the array with the contents I expected. So, why does the array have the correct contents, but will not echo? Thanks a ton! Hey there, I have an image gallery running on my website right now and I want to give the site admins access to add/delete the images from the different gallery directories. But to be honest I'm totally lost on how to do it lol I know my way around PHP but I've never had to code something to pull up the dir contents with an option to delete the files. I've found scandir which will get the contents of a dir and unlink which deletes a file but I have no clue how to get them to work together, anyone got any ideas or a different function if I'm looking in the wrong direction with those functions. Any help would be much appreciated. Thank you! Hi guys. This is my first post here, so excuse me if i am posting this in the wrong forums. I am making a website, and having a huge problem. The script is used for orders of different services. And i am using sessions to store the information through serveral pages. But my problem is that on my final page, where the script sends an email with i can not get it to view the contents of one session. I am using the same session on a different page and it works like a charm. When i try debugging the session it won't print the contents, so i am guessing something is very wrong? My code of the page is bellow, let me know if you need anything else: http://pastebin.com/YHh3bGWS Please help me How does a web server, with a database connect to the emails it receives? How does the web server get the contents of the emails it receives so I can parse it? Thanks Hey, I need a bit of help replacing some variables in a string. I know its done with preg_replace. Here is the string: Code: [Select] Header 1 <|header 2|> header 3 <b>header 4</b> I would like to get ONLY <|header 2|> from the above string. But "header 2" is variable so please its useless if the preg_replace isn't flexible to find whatever word is in there regardless. Thanks you. I have a purchase order displaying on a site that the user can edit quantities for. I want them to be able to hit submit and it takes that form and submits it to some code that auto creates a PDF with it. The PDF part is easy, I use DOMPDF to create that and it works fine, I just can't seem to get the values passed through. I can easily pass the values of just the input boxes through the $_POST but I don't want to have to recreate the PDF setup after the post. I'd rather just use some Javascript or something to grab the innerHTML of a <div> element and somehow make it available after the form is submitted so the PHP code can use it to send to DOMPDF. The form submits to itself and I have a piece of code at the top of the page which will create the PDF, I just need the <div> contents and I'm done. How do I get it? I could set it into a hidden variable but the reason I'm not doing that is because for some reason when I assign the big table which contains the whole purchase order into the hidden variable and then try to print it, it comes out all messed up. I don't know why and I'd rather just find another way than attempt to figure that out. I have made a file explorer for my site so i can upload files, delete them and rename them. Everythings working fine except when i want to delete a folder. I realise you cant use ftp_delete, coz' that only works for files. I have tried rmdir, and even though I am getting no errors, it seems to do nothing :s So what is the best way to delete a folder and its contents using PHP FTP commands? Thanks guys Hi guys! Could you give me advice about this issue: index.php Code: [Select] <?php $page = $_GET['page']; if ($page == NULL) { $page = "main"; } ?> <div>Header</div> <div> <?php require("view/$page.php");?> </div> <div>Footer</div> main.php Code: [Select] <?php require "func.php"; ?> <form method="post" action="view/add.php"> <input type="text" name="from" value=""><br /> <input type="submit" value="Add"> </form> add.php Code: [Select] <?php require "../func.php"; $from = $_POST['from']; $a = add($from); echo $a; ?> func.php Code: [Select] <?php function add($a) { $c = $a+100; return $c; } ?>I need output result (echo $a) in new windows (target= _blank) and output must be same like with main.php. In other words content of add.php output in index.php This code is just simplified of real code. Thank you in advice eg http://www.xxxxxxx.com/index.php?action=viewarti&artid=5 How can I write the content of this link into file. Hello all, I'm sure something like this has been brought up before but I think it's quite a unique problem and I didn't get anywhere searching... so here's my first post. Yay. Right, so, basically I have a database table that looks like this: Code: [Select] +----------+-------+ | name | value | +----------+-------+ | settingA | true | | settingB | false | | settingC | false | +----------+-------+ And I have been racking my brains trying to figure out how to get that data into an array formatted like so: Code: [Select] Array ( [settingA] => true [settingB] => false [settingC] => false ) The best I have been able to do, using nested foreach() statements, is this: Code: [Select] Array ( [0] => Array ( [name] => settingA [value] => true ) [1] => Array ( [name] => settingB [value] => false ) [2] => Array ( [name] => settingC [value] => false ) ) ...which is not very practical, or at least it isn't for what I want to do. Any ideas on what the best method might be for achieving this? I am using ADOdb Lite but even an example using PHP's native MySQL functions could help me figure out what to do. Thanks! |