热门装机教程
装机教程
您现在的位置: 首页 > 装机教程

Unix/Linux中直接执行PHP脚本文件的方法

来源:黑云一键重装系统    发布时间:2016-03-24 11:12:23

  Linux系统的使用中,服务器都有搭建完整的PHP环境,很多用户会用PHP去写一些执行自动化任务的脚本,不过用户反映,每次执行PHP脚本都需要使用php myscript.php的方式,这样操作感觉非常的麻烦。不过我们还是可以直接执行PHP脚本文件的,可是具体该怎么操作呢?这里就让小编来为大家介绍下,Unix/Linux中直接执行PHP脚本文件的操作方法

72-160323142S53c.jpg

  编写你的脚本文件

  这里我们编写一个名字为 test_run.php 的文件,文件的内容如下:

  Here is some plain text.

  Here is the file name:

  《?php

  echo $argv[0], PHP_EOL;

  ?》

  脚本内容很简单,就是把当前脚本文件的名称打印出来。

  然后,我们使用 PHP 命令执行一下这个脚本:

  yuanyu@ymac:phpworkspace $ php test_run.php hello

  Here is some plain text.

  Here is the file name:

  test_run.php

  yuanyu@ymac:phpworkspace $

  给脚本文件增加头信息,并且设置权限

  然后,在这个文件的第一行写上 php 命令的全路径,前面是一个 #!:

  #!/usr/bin/php

  Here is some plain text.

  Here is the file name:

  《?php

  echo $argv[0], PHP_EOL;

  ?》

  然后给这个文件赋予可执行的权限:

  yuanyu@ymac:phpworkspace $ chmod u+x 。/test_run.php

  接下来就可以直接执行这个脚本了:

  yuanyu@ymac:phpworkspace $ 。/test_run.php

  Here is some plain text.

  Here is the file name:

  。/test_run.php

  yuanyu@ymac:phpworkspace $

  这种方式在 PHP 官方文档中也是有说的,请参考:

  http://php.net/manual/en/features.commandline.usage.php

  文档中的

  “Example #2 Script intended to be run from command line (script.php)”

  关于Unix/Linux中直接执行PHP脚本文件的操作方法就介绍完了如果不熟悉的用户可以按照上面介绍的步骤来操作即可