-->

placing PHP variable inside of shell_exec( 

2019-09-08 03:28发布

问题:

When the url .../print.php loads, I want the name of the latest file uploaded to the target directory to be printed and for that same file to simultaneously be sent to the printer connected to the server. With this code here, I am able to do everything that I want ONLY when the shell_exec(lsp /file/path) refers directly to a file.

I have a function set up that determines the latest file uploaded and also the filepath, but I want to do something like shell_exec(lsp $filepath).

<?php
$path = "/var/www/html/work/uploads";

$latest_ctime = 0;
$latest_filename = '';
$output = shell_exec('lpr'.$filepath);

$d = dir($path);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path}/{$entry}";
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
    $latest_ctime = filectime($filepath);
    $latest_filename = $entry;
  }
}

echo "$output Printing: $latest_filename";

?>

I've seen many different suggestions online for how to make this work, but they were all specifying a slightly different process and none of them worked for me. Can anybody please help?