Jump to content

How do I form XML from bash command in PHP script?


Recommended Posts

I've got this PHP script for invalidating files in the Amazon CloudFront CDN, which I want to automate.  I got it from GitHub.


Part of it uses XML, where the file paths are added.

$xml = <<<EOD

I have no idea how to go about this, but what I want to do is change this part so that file paths are added from the output of the bash find command:

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> EOD;


I'd really appreciate any help on how to go about this.  Thanks.

Edited by jason404
Link to comment
Share on other sites

A guy apparently from Google gave me this help on IRC:

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

/* Do what you want with $paths */

I am trying to understand it.

Link to comment
Share on other sites

That stuff is the replacement for the `find` command - done entirely in PHP code. After it is done you'll have an array of absolute $paths to all the required files. You can actually do away with the $paths variable entirely:

1. Start the XML with the

2. Go with the above code up until it adds $path to the $paths array

3. Instead of that, add $path to the XML in a . Note that $path has the whole absolute path (I think) so if you only want the path starting with /wp-content/ you'll need to get that

4. Use the rest of the code

5. Finish the XML

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.