How to Allow the Use of PHP System Functions
When you want your PHP script to run an external program in the background, you need to utilize PHP's built-in system execution functions. Some reasons to run an external program include wanting to display a list of files in a folder, running an image conversion program on an uploaded file or an admin launching a logging utility. Deciding which PHP function to use depends on how you want the program's output to be handled.
Instructions
-
-
1
Connect to the server hosting your Web page using an FTP client.
-
2
Locate and download the server's "php.ini" file. If you don't know the location of the file, you can find its path by running "<?php phpinfo(); ?>" on the server and looking for the line starting with "Configuration File (php.ini) Path."
-
-
3
Search the file for the line starting with "safe_mode =" and change the text from "safe_mode = On" to "safe_mode = Off" if necessary.
-
4
Save the "php.ini" file if you made any changes, and then upload it back to the server using the FTP client.
-
5
Restart your Web server if you changed the "php.ini" file.
-
6
Open your Web page's PHP source file in a text editor, such as Windows Notepad.
-
7
Call the "system(command)" function to execute a system program. Text from the program is sent to the output stream. If the program will continue running, make sure its output is sent to an output stream, or PHP will hang waiting for the program to end.
For example, "$my_output = system('ls', $ret_value);" will call the system command "ls" and return the last line of output to the "my_output" string.
-
8
Call the "exec(command, output)" function if you want all the program's text output to be directed to your "output" variable array instead of to the system's output stream. For example, "exec('ls', $output = array());".
-
9
Save the PHP file and load it on your server to make sure it works correctly. If the program doesn't run, make sure the code syntax is correct and that the program's file permissions are set to allow execution.
-
1
Tips & Warnings
Use "escapeshellarg()" or "escapeshellcmd()" for security when using visitor-supplied data in the command string.
PHP commands need to be contained within "<?php" and "?>" tags.