1

PHP: Output multiple line command-line outputs as different lines. Sorry if the title is difficult to understand. Basically I want my output like A, instead of B. It currently looks like B. I have tried nl2br. The script I am trying to run is:

Script:

echo "Virus Scan Results:";
$scanme = system('cd /var/www/upload/files; clamscan --remove=yes '.$furl);
printf(nl2br($scanme));

A:

802931t_e_s_t.txt: OK 
----------- SCAN SUMMARY ----------- 
Known viruses: 574585 
Engine version: 0.95.1 
Scanned directories: 0 
Scanned files: 1 
Infected files: 0 
Data scanned: 0.00 MB 
Data read: 0.00 MB (ratio 0.00:1) 
Time: 2.352 sec (0 m 2 s) 
Time: 2.352 sec (0 m 2 s)

B:

802931t_e_s_t.txt: OK ----------- SCAN SUMMARY ----------- Known viruses: 574585 Engine version: 0.95.1 Scanned directories: 0 Scanned files: 1 Infected files: 0 Data scanned: 0.00 MB Data read: 0.00 MB (ratio 0.00:1) Time: 2.352 sec (0 m 2 s) Time: 2.352 sec (0 m 2 s)

3 Answers 3

4

why are you using nl2br if this is on the command line?

nl2br outputs <br /> tags for new lines... which would have no meaning on command line

Edit

Two things:

1 try

system('cd /var/www/upload/files; clamscan --remove=yes '.$furl, $scanme);

2 You may want to use the exec function instead of system

e.g.

exec('cd /var/www/upload/files; clamscan --remove=yes '.$furl, $scanme);
$scanme = implode("\n",$scanme);

exec ( string $command [, array &$output [, int &$return_var ]] )

Sign up to request clarification or add additional context in comments.

4 Comments

The result is: <pre> 733141t_e_s_t.txt: OK ----------- SCAN SUMMARY ----------- Known viruses: 574585 Engine version: 0.95.1 Scanned directories: 0 Scanned files: 1 Infected files: 0 Data scanned: 0.00 MB Data read: 0.00 MB (ratio 0.00:1) Time: 2.305 sec (0 m 2 s) Time: 2.305 sec (0 m 2 s) </pre> It seems to only affect the last line. Got any other ideas?
733141t_e_s_t.txt: OK ----------- SCAN SUMMARY ----------- Known viruses: 574585 Engine version: 0.95.1 Scanned directories: 0 Scanned files: 1 Infected files: 0 Data scanned: 0.00 MB Data read: 0.00 MB (ratio 0.00:1) Time: 2.305 sec (0 m 2 s) Time: 2.305 sec (0 m 2 s)
Works now, it seems like the $scanme was actually outputting the data without an echo or printf. Thanks for the help.
no worries. the docs for those functions say system, exec, etc return a string. but it's the "last" line returned. The optional second/third parameter is for an output var.
1

Have you tried just printing the output of the command directly?

echo "Virus Scan Results:";
echo exec('cd /var/www/upload/files; clamscan --remove=yes '.$furl);

PS. You really should sanitise the input like so (if you are not doing so already):

$furl = escapeshellarg($furl)

escapeshellarg() - Escape a string to be used as a shell argument

Comments

0

If you are running on the commandline, a newline is represented as '\n', or '\r\n' in a Windows environment. So make sure there is a \n at the end of each line, and you should get the output you want. Edit:
Tom: Oops. Fixed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.