Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Loop through files on server and run script for each file
    text
    copied!<p>I will try to explain as well as possible what I'm trying to do.</p> <p>I have a folder on a server with about 100 xml files. These xml files are content pages with text and references to attachment filenames on the server that will be pushed to a wiki through an API.</p> <p>It's all working fine 1 XML file at a time but I want to loop through each one and run my publish script on them.</p> <p>I tried with opendir and readdir and although it doesn't error it only picks up the one file anyway.</p> <p>Could someone give me an idea what I have to do. I'm very new to PHP, this is my first PHP project so my code is probably not very pretty!</p> <p>Here's my code so far.</p> <p><strong>The functions that gets the XML content from the XML file:</strong></p> <pre><code>&lt;?php function gettitle($file) { $xml = simplexml_load_file($file); $xmltitle = $xml-&gt;xpath('//var[@name="HEADLINE"]/string'); return $xmltitle[0]; } function getsummary($file) { $xml = simplexml_load_file($file); $xmlsummary = $xml-&gt;xpath('//var[@name="summary"]/string'); return $xmlsummary[0]; } function getsummarymore($file) { $xml = simplexml_load_file($file); $xmlsummarymore = $xml-&gt;xpath('//var[@name="newslinetext"]/string'); return $xmlsummarymore[0]; } function getattachments($file) { $xml = simplexml_load_file($file); $xmlattachments = $xml-&gt;xpath('//var[@name="attachment"]/string'); return $xmlattachments[0]; } ?&gt; </code></pre> <p><strong>Here's the main publish script which pushes the content to the wiki:</strong></p> <pre><code>&lt;?php // include required classes for the MindTouch API include('../../deki/core/dream_plug.php'); include('../../deki/core/deki_result.php'); include('../../deki/core/deki_plug.php'); //Include the XML Variables include('loadxmlfunctions.php'); //Path to the XML files on the server $path = "/var/www/dekiwiki/skins/importscript/xmlfiles"; // Open the XML file folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($xmlfile = readdir($dir_handle)) { if($xmlfile == "." || $xmlfile == ".." || $xmlfile == "index.php" ) continue; //Get XML content from the functions and put in the initial variables $xmltitle = gettitle($xmlfile); $xmlsummary = getsummary($xmlfile); $xmlsummarymore = getsummarymore($xmlfile); $xmlattachments = getattachments($xmlfile); //Build the variables for the API from the XML content //Create the page title - replace spaces with underscores $pagetitle = str_replace(" ","_",$xmltitle); //Create the page path variable $pagepath = '%252f' . str_replace("'","%27",$pagetitle); //Strip HTML from the $xmlsummary and xmlsummarymore $summarystripped = strip_tags($xmlsummary . $xmlsummarymore, '&lt;p&gt;&lt;a&gt;'); $pagecontent = $summarystripped; //Split the attachments into an array $attachments = explode("|", $xmlattachments); //Create the variable with the filenames $pagefilenames = '=' . $attachments; $pagefilenamefull = $xmlattachments; //Create the variable with the file URL - Replace the URL below to the correct one $pagefileurl = 'http://domain/skins/importscript/xmlfiles/'; //authentication $username = 'admin'; $password = 'password'; // connect via proxy $Plug = new DreamPlug('http://domain/@api'); // setup the deki api location $Plug = $Plug-&gt;At('deki'); //authenticate with the following details $authResult = $Plug-&gt;At('users', 'authenticate')-&gt;WithCredentials($username, $password)-&gt;Get(); $authToken = $authResult['body']; $Plug = $Plug-&gt;With('authtoken', $authToken); // Upload the page content - http://developer.mindtouch.com/Deki/API_Reference/POST:pages//%7Bpageid%7D//contents $Plug_page = $Plug-&gt;At('pages', '=Development%252f' . $pagetitle, 'contents')-&gt;SetHeader('Expect','')-&gt;Post($pagecontent); // Upload the attachments - http://developer.mindtouch.com/MindTouch_Deki/API_Reference/PUT:pages//%7Bpageid%7D//files//%7Bfilename%7D for($i = 0; $i &lt; count($attachments); $i++){ $Plug_attachment = $Plug-&gt;At('pages', '=Development' . $pagepath, 'files', '=' . $attachments[$i])-&gt;SetHeader('Expect','')-&gt;Put($pagefileurl . $attachments[$i]); } } //Close the XMl file folder closedir($dir_handle); ?&gt; </code></pre> <p>Thanks for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload