简体   繁体   English

从命令行运行PHP脚本

[英]Running PHP script from command line

So I'm working with a designer on a website in PHP/MySQL and there are a few scripts that he would like to have to make life easier for him. 因此,我正在与PHP / MySQL网站上的设计师合作,并且他希望有一些脚本可以让他的生活更轻松。 He is pretty comfortable using the command line for stuff like git, SASS, node, etc. I would like him to be able to run my scripts like he would run a program, instead of running it through PHP. 他非常适合使用git,SASS,node等东西的命令行。我希望他能像运行程序一样运行我的脚本,而不是通过PHP运行它。

So instead of this: 所以不是这样的:

php /path/to/file/create_module.php module_name

I would like to do this: 我想这样做:

myscript create_module module_name

Is it possible to do this with PHP on an Apache server? 是否可以在Apache服务器上使用PHP执行此操作? I know I will most likely have to modify the server to interpret it properly, which is fine. 我知道我很可能必须修改服务器才能正确解释它,这很好。 I just don't even know where to begin, and couldn't find what I needed on Google. 我甚至不知道从哪里开始,也无法在Google上找到我需要的东西。

Your best bet would to be to create an alias . 你最好的选择是创建一个alias

So an alias of myscript would actually point to the command: php /path/to/file/create_module.php and then any extra arguments will be passed as typed. 因此, myscript的别名实际上将指向命令: php /path/to/file/create_module.php然后任何额外的参数将作为类型传递。

In command line, do the following: 在命令行中,执行以下操作:

cd /etc/
nano bash.bashrc

At the very bottom of the file, add this line of text: 在文件的最底部,添加以下文本:

alias "myscript=php /path/to/file/create_module.php"

BASHRC is a script that is run on user login, so the alias will be recreated every time the user logs into the system. BASHRC是一个在用户登录时运行的脚本,因此每次用户登录系统时都会重新创建别名。

I am not sure what you are looking for myscript to do, but to run a php script via the command line without specifying the php binary, just add aa shebang , like 我不确定你要找myscript做什么,但是通过命令行运行php脚本而不指定php二进制文件,只需添加一个shebang ,就像

#!/bin/env php
<?php

// The above expects env is in /bin

$foo = "bar";

Or the full path if you like 或者如果你愿意,可以选择完整路径

#!/usr/bin/php

Another option: Shell script wrapper 另一种选择:Shell脚本包装器

create a new text file in /bin or another directory in your PATH , name it how you would like to invoke your script and give it this content /binPATH另一个目录中创建一个新的文本文件,将其命名为调用脚本的方式并为其提供此内容

#!/bin/bash
cd /path/to/your/php/scripts/folder
php script.php $*

don't forget to 别忘了

chmod a+x /path/to/bash/script

The advantage of this is that your PHP script is run in the right directory where it may expect other resources to be that it depends on. 这样做的好处是您的PHP脚本在正确的目录中运行,在该目录中可能会有其他资源依赖于它。

On A Linux Solution: 在Linux解决方案上:

/usr/bin/php /path/to/file.php

On a Windows Solution: 在Windows解决方案上:

C:\Path\To\PHPExe C:\Path\To\phpfile.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM