PHP - 循环访问服务器上的文件并为每个文件运行脚本

时间:2009-12-17 17:20:02

标签: php xml simplexml

我会尝试尽可能地解释我想要做的事情。

我在服务器上有一个包含大约100个xml文件的文件夹。这些xml文件是包含文本的内容页面,以及服务器上附件文件名的引用,这些文件名将通过API推送到Wiki。

一次只能处理1个XML文件,但我想循环遍历每个文件并在其上运行我的发布脚本。

我尝试使用opendir和readdir,尽管它没有错误,但它只会选择一个文件。

有人可以告诉我我必须做什么。我是PHP的新手,这是我的第一个PHP项目,所以我的代码可能不是很漂亮!

到目前为止,这是我的代码。

从XML文件获取XML内容的函数:

<?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];
}
?>

以下是将内容推送到维基的主要发布脚本:

<?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);

?>

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

要遍历XML文件目录,您可以这样做:

$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);
}

我还建议您对代码进行微调,以便simplexml不需要解析同一个文件四次以获得所需的属性:

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];
}

答案 1 :(得分:1)

尝试更改你的while循环,看看是否有更好的帮助:

while (false !== ($xmlfile = readdir($dir_handle))) 

让我知道。

编辑:

通过使用旧方法,可能有一个目录名称可以评估为false并停止循环,我建议的方式被认为是在使用readdir取代时循环目录的正确方法here