【问题标题】:PHP - Loop through files on server and run script for each filePHP - 遍历服务器上的文件并为每个文件运行脚本
【发布时间】:2009-12-17 17:20:01
【问题描述】:

我会尽量解释我想要做什么。

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

每次 1 个 XML 文件都可以正常工作,但我想遍历每个 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];
}
?>

这是将内容推送到 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);

?>

感谢您的帮助!

【问题讨论】:

标签: php xml simplexml


【解决方案1】:

要遍历 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];
}

【讨论】:

  • 谢谢凯文,我也会试试你的代码。并感谢您提供的有关功能的提示,这些提示对您有很大帮助。
【解决方案2】:

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

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

告诉我。

编辑:

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

【讨论】:

  • 谢谢!我会试试这个,让你知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多