如何在此PHP脚本中构建XML?

时间:2014-02-21 22:09:23

标签: php xml

我有this PHP script因为我想要自动化的Amazon CloudFront CDN中的文件无效。

部分使用XML,其中添加了文件路径。

$xml = <<<EOD
<InvalidationBatch>
    <Path>/index.html</Path>
    <Path>/blog/index.html</Path>
    <CallerReference>{$distribution}{$epoch}</CallerReference>
</InvalidationBatch>
EOD;

我想用这样的命令的XML格式输出替换这部分:

find /srv/domain.com/wp-content/uploads/ -user www-data

这是为了在使用cron脚本优化新图像文件上传后使其无效。

为了使问题更复杂,路径只需要从wp-content目录开始包含,因此XML最终将会是这样的:

$xml = <<<EOD
<InvalidationBatch>
    <Path>/wp-content/uploads/2014/02/ED_Wedluxe-CuveeRose-364x400.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINE_PROMOTION_1-165x213.jpg</Path>
    <Path>/wp-content/uploads/2014/02/ED_Wedluxe-CuveeRose-165x220.jpg</Path>
    <Path>/wp-content/uploads/2014/02/ED_Wedluxe-CuveeRose-371x495.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINE_PROMOTION_1-471x609.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINE_PROMOTION_1.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINES14-WEB_banner-794x4761-687x412.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINES14-WEB_banner-794x4761-300x180.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINES14-WEB_banner-794x4761.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINE_PROMOTION_1-150x150.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINES14-WEB_banner-794x4761-687x477.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINE_PROMOTION_1-110x142.jpg</Path>
    <Path>/wp-content/uploads/2014/02/ED_Wedluxe-CuveeRose-500x432.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINE_PROMOTION_1-624x432.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINES14-WEB_banner-794x4761-471x282.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINES14-WEB_banner-794x4761-150x150.jpg</Path>
    <Path>/wp-content/uploads/2014/02/VALENTINES14-WEB_banner-794x4761-364x400.jpg</Path>
    <Path>/wp-content/uploads/2014/02/ED_Wedluxe-CuveeRose-110x146.jpg</Path>
    <CallerReference>{$distribution}{$epoch}</CallerReference>
</InvalidationBatch> EOD;

我正在和IRC上的一些人交谈,有人建议我使用这样的东西,而不是通过php执行shell命令:

<?php
$path     = isset($argv[1]) ? $argv[1] : './';
$owner    = isset($argv[2]) ? $argv[2] : 'www-data';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));


$paths = array();


foreach ($iterator as $result) {
   $path = $result->getPath() . '/' . $result->getFilename();
   if (posix_getpwuid(fileowner($path))['name'] == $owner) {
      $paths[] = $path;
   }
}

然而,无论我尝试过什么都行不通。

1 个答案:

答案 0 :(得分:1)

好的,你走了。你很亲密;我基本上只是留下你所拥有的东西,并添加了一点。

<?php

$path = isset($argv[1]) ? $argv[1] : './';
$owner = isset($argv[2]) ? $argv[2] : 'www-data';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));


foreach ($iterator as $result) { 

    $path_info = $result->getPath().'/'.$result->getFilename();

    $owner_info_array = posix_getpwuid(fileowner($path_info));

    // CHECK TO SEE IF THE OWNER IS www-data AND THAT THE FILE NAME DOES NOT START WITH '/.'
    if (($owner_info_array['name'] == $owner) && (!preg_match('/\/\./', $path_info))){
        $path_info = preg_replace('/'.preg_quote('/srv/domain.com', '/').'/', '', $path_info);
        $path_array[] = $path_info;
    }

}


$xml = "<InvalidationBatch>";

foreach ($path_array AS $item) {
    $xml .= "\n    <Path>".$item."</Path>";
}

$xml .= "\n    <CallerReference>{$distribution}{$epoch}</CallerReference>
</InvalidationBatch>";



print $xml;

当然,您希望将/srv/domain.com替换为您要删除的路径部分。如果这对您有用,请告诉我。