I've got this PHP script for invalidating files in the Amazon CloudFront CDN, which I want to automate.
Part of it uses XML, where the file paths are added.
$xml = <<<EOD
<InvalidationBatch>
<Path>/index.html</Path>
<Path>/blog/index.html</Path>
<CallerReference>{$distribution}{$epoch}</CallerReference>
</InvalidationBatch>
EOD;
I want to replace this part with XML formatted output of a command like this:
find /srv/domain.com/wp-content/uploads/ -user www-data
This is to invalidate new image file uploads after they have been optimised using a cron script.
To further complicate matters, the path needs to only include from the wp-content directory onwards, so the XML would end up something like this:
$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;
I was talking to some people on IRC and someone suggested that I use something like this, instead of executing shell command through php:
<?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;
}
}
However, whatever I have tried does not work.