Jump to content

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
<InvalidationBatch>
    <Path>/index.html</Path>
    <Path>/blog/index.html</Path>
    <CallerReference>{$distribution}{$epoch}</CallerReference>
</InvalidationBatch>
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>
<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'd really appreciate any help on how to go about this.  Thanks.

Edited by jason404

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

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


/* Do what you want with $paths */

I am trying to understand it.

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

Saves you a bit of code: rather than add stuff to $paths and then loop over it later, forget that second loop and just do whatever you need with the individual values the moment you get them.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.