Question

I have a problem in using system(), passthru() and exec(), the example below will describe it better, I have a string x that I will use it as argument for system():

$x = ' second_string' .$array[$i];
system("cat $x "); // not working

system("cat ".$x); // not working

system("cat { $x }"); // not working
system('cat "' . $x . '" '); // not working
system("cat second_string_xx.txt" ); //working !! 

No correct solution

OTHER TIPS

Your question couldn't be more vague but one of your comments suggest that you expect an error message when you feed cat with an invalid file. Well, that's not how cat actually works. As most classic Unix commands, it writes error messages into standard error (stderr) rather than standard output (stdout). You either need to use a PHP function that allows to capture stderr (such as proc_open) or redirect stderr to somewhere else (such as stdout or a file).

Apart from that, PHP program execution functions have many optional parameters and return values and you are ignoring them all. For instance, system() returns the last line of the command output on success (and FALSE on failure) and allows to capture the return value (which will be non-zero in case of error and is the standard mechanism to detect errors).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top