Better exec()

/** * Run command and get output and exit code. * See https://stackoverflow.com/a/9486083/2277053 * See https://stackoverflow.com/a/43657546/2277053 */ function run_command($command) { $output = []; exec($command, $output, $exitcode); // Trim, Remove empty and Reset keys. $output = array_values(array_filter(array_map(‘trim’, $output), function($string) { return

Read STDOUT and STDERR in non blocking way on proc_open

<?php // The command to run. $command = ‘ping 127.0.0.1’; $descriptors = [ 0 => [‘pipe’, ‘r’], // STDIN 1 => [‘pipe’, ‘w’], // STDOUT 2 => [‘pipe’, ‘w’], // STDERR ]; $process = proc_open($command, $descriptors, $pipes); if (is_resource($process)) {

Real time flushing in PHP on Nginx

Works perfectly on RunCloud’s Native Nginx Stack. @ini_set(‘zlib.output_compression’, 0); @ini_set(‘output_buffering’, ‘Off’); @ini_set(‘implicit_flush’, true); @ob_implicit_flush(1); @ob_end_clean(); header(‘Content-Encoding: none’); header(‘X-Accel-Buffering: no’);

Remove image sizes in WordPress

/** * Function to Remove image size. * @param [string] $size */ function remove_image_size_by_size($size) { $dimension = explode(‘x’, $size); global $_wp_additional_image_sizes; foreach ($_wp_additional_image_sizes as $key => $single) { if ($single[‘width’] == $dimension[0] && $single[‘height’] == $dimension[1]) { remove_image_size($key); break; }

Change default PHP CLI configuration on RunCloud

Runcloud allows you to change the PHP Settings for each application from their Web Panel. But if you’re like me who heavily uses the PHP-CLI then changing config from Web Panel won’t help you. The PHP configs from their Web

Fix CMD output when using shell_exec() from PHP

/** * Fix weird output encoding. */ function run_command($command) { $string = shell_exec($command); $string = mb_convert_encoding($string, ‘UTF-8’, ‘UTF-16LE’); return $string; } Source: https://www.reddit.com/r/PHPhelp/comments/de27u8/why_output_of_shell_exec_is_different/f2uwy7s

Get all system IPv4 addresses as array in PHP

/** * Get system ipv4 addresses. * Works on Linux only. * @return [array] */ function getSystemIPs() { $ips = shell_exec(‘hostname -I’); $ips = preg_split(‘/\s+/’, $ips, -1, PREG_SPLIT_NO_EMPTY); foreach ($ips as $ip) { if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE |