-1

Very simple code:

<?php
$last_line = system('pwsh -Command (Get-Location).path', $retval);

Returning string on html page: enter image description here

How is this possible and how to avoid it?

Libor P
  • 89
  • 7

2 Answers2

1

The PHP system() command executing a system command and print immediately the output out. To avoid that you can use exec().

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
0

You can use ob_start() to save the output of the command in a buffer. Then use ob_clean() in order to clear that buffer without printing it.

ob_start(); 
$last_line = system('pwsh -Command (Get-Location).path', $retval);
ob_clean();
Skatox
  • 4,237
  • 12
  • 42
  • 47
W S
  • 362
  • 3
  • 4
  • This is correct - but needs some words describing the solution. PHP docs for output buffers are here: https://www.php.net/manual/en/book.outcontrol.php – Peter Krebs Apr 07 '22 at 09:39
  • While it works, there are functions other than `system` that don't flush the output directly to begin with, which don't require this workaround… – deceze Apr 07 '22 at 09:40