doufan9395 2016-01-24 05:14
浏览 113
已采纳

如何通过输入文件名从命令行执行PHP脚本?

I want to be able to type into the command line, and have the following PHP script execute like so:

$ hello.php

Question is, where do I save my hello.php file to run it like this. I see a lot of tutorials showing this way, but they do not mention where to save the actual file. If I save it on my desktop then it will run when I type in:

~/Desktop/hello.php

I want to be able to just type in the file name regardless of what directory I'm in and have it execute.

Do I have to setup an alias? Or can I just put it in a specific folder.

hello.php

<?php

echo 'Hello! This is a test';
  • 写回答

2条回答 默认 最新

  • duanmajing9332 2016-01-24 05:22
    关注

    All you have to do is add a shebang line:

    #!/usr/bin/php
    <?php
    
    echo 'Hello! This is a test';
    

    And then:

    chmod +x hello.php
    

    Then you can run it from the same directory by just typing:

    ./hello.php
    

    If you want to be able to run it from anywhere, put it somewhere in $PATH, such as /usr/local/bin. You can either actually put the file there, or change your PATH, or create a symbolic link. Then, you can run it as:

    hello.php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?