jason404 Posted February 20, 2014 Share Posted February 20, 2014 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. Link to comment https://forums.phpfreaks.com/topic/286351-how-do-i-form-xml-from-bash-command-in-php-script/ Share on other sites More sharing options...
jason404 Posted February 20, 2014 Author Share Posted February 20, 2014 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. Link to comment https://forums.phpfreaks.com/topic/286351-how-do-i-form-xml-from-bash-command-in-php-script/#findComment-1469717 Share on other sites More sharing options...
requinix Posted February 20, 2014 Share Posted February 20, 2014 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 https://forums.phpfreaks.com/topic/286351-how-do-i-form-xml-from-bash-command-in-php-script/#findComment-1469723 Share on other sites More sharing options...
jason404 Posted February 20, 2014 Author Share Posted February 20, 2014 @requinix Thanks, but it will take a while before I can understand what you are saying. What is the advantage of doing that instead of adding $path to the $paths array? Link to comment https://forums.phpfreaks.com/topic/286351-how-do-i-form-xml-from-bash-command-in-php-script/#findComment-1469728 Share on other sites More sharing options...
requinix Posted February 20, 2014 Share Posted February 20, 2014 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. Link to comment https://forums.phpfreaks.com/topic/286351-how-do-i-form-xml-from-bash-command-in-php-script/#findComment-1469738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.