title: Is it possible to capture the output of a CLI command (exec) to a PHP script variable?
body: <p>I have an external library with a bunch of executables (DCMTK).  Normally, those can be executed on the CLI, with output sometimes showing in the terminal.  I want to use some of those in PHP scripts, and I was able to do that with one.</p>
  
  <p>Essentials are:</p>
  
  <pre><code>// path to the executables.
  
  private static $dcmtk_path = '/usr/local/opt/dcmtk/bin/';
  
  //method to execute the dcmtk executable.
  
  public static function dcmtk_command($command) {
      //--logfile dcmlogfile.cfg
      echo exec(self::$dcmtk_path . $command); // $outputarray, 2nd arg ?
      }
  }
  
  // working example that converts a text file to a dcm worklist file
  
  file_put_contents (self::$MWL_PATH . "samplephp.txt", $template); // text file for MWL.
  echo '[{"status":"Sent to PATH"}]';
  self::dcmtk_command('dump2dcm ' . self::$MWL_PATH . "samplephp.txt " . self::$MWL_PATH . "samplephp.wl");
  </code></pre>
  
  <p>I don't know if this is even possible.  But, I am uploading multiple files via a $_POST and get:  </p>
  
  <pre><code> $file_tmp = $files['tmp_name'];
  </code></pre>
  
  <p>That is saved to a path on the server:</p>
  
  <pre><code> $success = move_uploaded_file($file_tmp, $upload_path);
  </code></pre>
  
  <p>and then I want to execute another command:</p>
  
  <pre><code>self::dcmtk_command('dcmdump ' . $upload_path . $file_name );
  // dcmdump +P StudyInstanceUID IM-0001-0004.dcm for specific tag
  </code></pre>
  
  <p>When executed from the CLI that prints out a bunch of text to the terminal (?STDOUT).  What I want to do is capture that output in the PHP script so that I can process the output.  I have tried a few things, like using an output buffer, the $outputarray from the exec command, etc.</p>
  
  <p>Seems like that should be possible.  The files are getting saved on the server in the path that is configured, so they should be at $upload_path . $file_name.  And, I do not see any errors in the PHP console / error log.  I actually do not have a good way to check if the command was even successful.</p>