14

Possible Duplicate:
php shell_exec() vs exec()

How do I run a linux command from a PHP script? I'm running Linux Debian and PHP5. I want to be able to issue a wget command to the console.

An example of what I'm looking for is something like this:

phpFunction ("wget http://www.example.com/image.jpg /folder");
echo "done";

Also would I be able to echo the output of that function?

Community
  • 1
  • 1
DonJuma
  • 2,028
  • 13
  • 42
  • 70
  • 1
    use [exec] (http://php.net/manual/en/function.exec.php) its very direct – Baba Oct 11 '12 at 00:38
  • 3
    you might want to look at the [PHP cURL](http://php.net/manual/en/book.curl.php) library to handle web requests from your script rather than using a system call to wget – HorusKol Oct 11 '12 at 00:46
  • [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – hakre Oct 11 '12 at 01:09

4 Answers4

21

Use exec to run any command. Be careful not to exec any user input though, as it can severely compromise your server.

Also, note that most shared servers block off the exec function so you won't be able to use it.

Finally, as a shorthand, you can wrap the command you want to exec in backticks.

Ayush
  • 41,754
  • 51
  • 164
  • 239
5

You can do what you want with the following code :

system(command);

See http://php.net/system

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
3

You can execute linux commands within a php script - all you have to do is put the command line in brackits (`).

And also concentrate on exec() , this and shell_exec() ..

John Conde
  • 217,595
  • 99
  • 455
  • 496
Tajamul
  • 31
  • 2
2

you can use

exec
shell_exec

http://php.net/manual/en/function.shell-exec.php

mahen3d
  • 7,047
  • 13
  • 51
  • 103