Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 500521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:05:45+00:00 2026-05-13T06:05:45+00:00

I will try to explain as well as possible what I’m trying to do.

  • 0

I will try to explain as well as possible what I’m trying to do.

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.

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.

I tried with opendir and readdir and although it doesn’t error it only picks up the one file anyway.

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!

Here’s my code so far.

The functions that gets the XML content from the XML file:

<?php
function gettitle($file)
{
    $xml = simplexml_load_file($file);
    $xmltitle = $xml->xpath('//var[@name="HEADLINE"]/string');
    return $xmltitle[0];
}

function getsummary($file)
{
    $xml = simplexml_load_file($file);
    $xmlsummary = $xml->xpath('//var[@name="summary"]/string');
    return $xmlsummary[0];
}

function getsummarymore($file)
{
    $xml = simplexml_load_file($file);
    $xmlsummarymore = $xml->xpath('//var[@name="newslinetext"]/string');
    return $xmlsummarymore[0];
}

function getattachments($file)
{
    $xml = simplexml_load_file($file);
    $xmlattachments = $xml->xpath('//var[@name="attachment"]/string');
    return $xmlattachments[0];
}
?>

Here’s the main publish script which pushes the content to the wiki:

<?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, '<p><a>');
$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->At('deki');

//authenticate with the following details
$authResult = $Plug->At('users', 'authenticate')->WithCredentials($username, $password)->Get();
$authToken = $authResult['body'];
$Plug = $Plug->With('authtoken', $authToken);

// Upload the page content - http://developer.mindtouch.com/Deki/API_Reference/POST:pages//%7Bpageid%7D//contents
$Plug_page = $Plug->At('pages', '=Development%252f' . $pagetitle, 'contents')->SetHeader('Expect','')->Post($pagecontent);

// Upload the attachments - http://developer.mindtouch.com/MindTouch_Deki/API_Reference/PUT:pages//%7Bpageid%7D//files//%7Bfilename%7D
for($i = 0; $i < count($attachments); $i++){
    $Plug_attachment = $Plug->At('pages', '=Development' . $pagepath, 'files', '=' . $attachments[$i])->SetHeader('Expect','')->Put($pagefileurl . $attachments[$i]);
}

}

//Close the XMl file folder
closedir($dir_handle);

?>

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:05:45+00:00Added an answer on May 13, 2026 at 6:05 am

    To traverse a directory of XML files you can just do:

    $files = glob("$path/*.xml");
    
    foreach($files as $file)
    {
        $xml = simplexml_load_file($file);
    
        $xmltitle = gettitle($xml);
        $xmlsummary = getsummary($xml);
        $xmlsummarymore = getsummarymore($xml);
        $xmlattachments = getattachments($xml);
    }
    

    I also recommend you make a minor adjustment to your code so simplexml doesn’t need to parse the same file four times to get the properties you need:

    function gettitle($xml)
    {
        $xmltitle = $xml->xpath('//var[@name="HEADLINE"]/string');
        return $xmltitle[0];
    }
    
    function getsummary($xml)
    {
        $xmlsummary = $xml->xpath('//var[@name="summary"]/string');
        return $xmlsummary[0];
    }
    
    function getsummarymore($xml)
    {
        $xmlsummarymore = $xml->xpath('//var[@name="newslinetext"]/string');
        return $xmlsummarymore[0];
    }
    
    function getattachments($xml)
    {
        $xmlattachments = $xml->xpath('//var[@name="attachment"]/string');
        return $xmlattachments[0];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 251k
  • Answers 252k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In Java, layout managers are used which determine the way… May 13, 2026 at 9:37 am
  • Editorial Team
    Editorial Team added an answer select name, max(userid) as max_userid from users group by name… May 13, 2026 at 9:37 am
  • Editorial Team
    Editorial Team added an answer It is generally a good idea to declare one class… May 13, 2026 at 9:37 am

Related Questions

Ok, Last time I posted this (last week), I didn't describe the problem correctly.
How can a WriteableBitmap from Silverlight be Saved onto the File System, I am
In one of my projects, I have an application that manages several clients (or
I'm trying to figure out how to best lay this out. I'll explain what
When writing a Java program, do I have influence on how the CPU will

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.