PHP calls Linux command details

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags apache array calls function group linux linux command linux commands

PHP calls Linux command details
/*
The function to invoke Linux commands in the PHP tutorial is
String exec (String command, string [array], int [return_var]);
As

EXEC ("Vpopmail");

echo exec (' whoami ');

Look at one more example

function exec_enabled () {
$disabled = Explode (', ', Ini_get (' disable_functions '));
return!in_array (' exec ', $disabled);
}

<?php
$tmp = EXEC ("C:imagegm.exe convert C:imagefile1.tiff c:imagefile1.jpg", $results);
?>

Another order is

PHP invokes system commands through function system ().

String System (String $command [, int & $return _var])

Instance

System (' ASTERISK-VVVVVVVVVVVC ');

System () is the same function C, which executes the given command and the version of the output result. The call to this system () also attempts to automatically refresh the Web server's output buffers on each output line if PHP runs as a server module. If you need to execute a command, and have all passed the command data directly from the back without any interference, use the PassThru () function.


$last _line = System (' ls ', $retval);


Function My_exec ($cmd, $input = ')
         {$proc =proc_open ($ CMD, Array (0=>array (' Pipe ', ' r '), 1=>array (' Pipe ', ' w '), 2=>array (' Pipe ', ' w ')), $pipes);
          fwrite ($pipes [0], $input); fclose ($pipes [0]);
          $stdout =stream_get_contents ($pipes [1]); Fclose ($pipes [1]);
          $stderr =stream_get_contents ($pipes [2]); Fclose ($pipes [2]);
          $rtn =proc_close ($PROC);
          return Array (' stdout ' => $stdout,
                         ' stderr ' => $stderr,
                        ' return ' => $rtn
                      );
        }
Var_export (my_exec (' Echo-e $ (</dev/stdin) | wc-l ', ' Hnelnlo ');

Example Three

$cmd = "date";

$output = System ($CMD);
printf ("System Output: $outputn");

EXEC ($cmd, $results);
printf ("Exec Output: {$results [0]}n");


Permissions issues for PHP calling Linux commands


You can use the timed task to execute the PHP you want to invoke, at which point the permission is root,
PHP invokes system commands via function system ()


PHP is typically executed as an Apache user, add Apache to the parent folder that stores your files, and then change the parent folder permission to 775, so that the group members have write permission, and Apache belongs to this group to overwrite all files under the directory, of course, The group is best not to be root, you can change the folder to another normal user group.

Changing the apache/php run user method is unsafe
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.