【发布时间】:2011-09-09 11:46:29
【问题描述】:
当在这里提到这篇文章时:
http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/
它告诉你使用 XAMPP 从 shell 运行 php -q path\to\server.php,WAMP 有替代方案吗?
【问题讨论】:
标签: shell command-line wamp
当在这里提到这篇文章时:
http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/
它告诉你使用 XAMPP 从 shell 运行 php -q path\to\server.php,WAMP 有替代方案吗?
【问题讨论】:
标签: shell command-line wamp
将 php.exe 注册为环境变量可能很有用,这样命令行就可以识别“php”命令,而无需输入完整路径“\wamp\bin\php\php5.3.8\php”为命令。
为此,您可以按照此页面上列出的步骤操作:http://windows.fyicenter.com/view.php?ID=60。除第 5 步外,改为输入 WAMP 的 php.exe 的路径。例如,只需在变量值字段中添加$;C:\wamp\bin\php\php5.3.8。每次打开命令行时,只需使用
php pageYouAreRunning.php
..仍然记住 pageYouAreRunning.php 是相对于命令行中的当前路径的。
【讨论】:
本质上和描述的一样。您将需要打开 windows cmd shell,然后您只需要在您的特定安装中找到 php 文件的位置。
在 wamp 安装中,它位于 \wamp\bin\php\php5.3.8\php。所以要运行我想要的 php 脚本,我会调用...
\wamp\bin\php\php5.3.8\php pageYouAreRunning.php
请记住,pageYouAreRunning.php 是相对于您当前在 shell 控制台中的路径。
【讨论】:
不再建议将环境路径编辑为直接指向 PHP exe 的路径。这可能会让您更加头疼。
WampServer forum 上一位名叫 RiggsFoley 的版主分享了以下文件
叫phppath.cmd:
@echo off
REM **********************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Serch path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM * for example I installed WAMPServer on the D: drive you may
REM * have used C:
REM * - baseWamp : is the drive and folder where you installed WAMPServer
REM * - defaultPHPver : is the version of PHP that will be pathed
REM * if no Parameter is put on the bat file
REM * - composerInstalled : Where I insatlled composerInstalled
REM * - phpFolder : The folder structure that contains the Multiple
REM * possible version of PHP I have installed
REM **********************************************************************
set baseWamp=D:\wamp
set defaultPHPver=7.0.23
set composerInstalled=%baseWamp%\composer
set phpFolder=\bin\php\php
if %1.==. (
set phpver=%baseWamp%%phpFolder%%defaultPHPver%
) else (
set phpver=%baseWamp%%phpFolder%%1
)
PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------
REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP
IF exist %phpver%\pear (
set PHP_PEAR_SYSCONF_DIR=D:\wamp\bin\php\php%phpver%
set PHP_PEAR_INSTALL_DIR=D:\wamp\bin\php\php%phpver%\pear
set PHP_PEAR_DOC_DIR=D:\wamp\bin\php\php%phpver%\docs
set PHP_PEAR_BIN_DIR=D:\wamp\bin\php\php%phpver%
set PHP_PEAR_DATA_DIR=D:\wamp\bin\php\php%phpver%\data
set PHP_PEAR_PHP_BIN=D:\wamp\bin\php\php%phpver%\php.exe
set PHP_PEAR_TEST_DIR=D:\wamp\bin\php\php%phpver%\tests
echo PEAR INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
) else (
echo PEAR DOES NOT EXIST IN THIS VERSION OF php
echo ---------------------------------------------------------------
)
REM IF COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
ECHO COMPOSER INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
set COMPOSER_HOME=%baseWamp%\composer
set COMPOSER_CACHE_DIR=%baseWamp%\composer
PATH=%PATH%;%baseWamp%\composer
rem echo TO UPDATE COMPOSER do > composer self-update
echo ---------------------------------------------------------------
) else (
echo ---------------------------------------------------------------
echo COMPOSER IS NOT INSTALLED
echo ---------------------------------------------------------------
)
set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=
按照说明,您需要编辑baseWamp 和defaultPHPver 变量。检查 Wamp 的 bin\php 目录以获取可用的 PHP 版本号。
将文件放在您的C:\Windows 目录中,然后打开一个新的命令窗口,然后输入phppath。您现在可以在该窗口的会话期间使用php。
【讨论】:
未测试,但我会说它是一样的......
你的 wamp/php 文件夹中有一个“php.exe”...
通过命令行运行:
cd mywamp/php
php -q file.php
【讨论】:
如果 php 根本没有运行,请使用以下代码创建一个批处理文件,并将其作为“php”保存在您的全局文件夹中。
"c:\wamp64\bin\php\php7.0.29\php.exe" $1 ${2} ${3} ${4}
当然,用你机器上的 php 版本替换 php7.0.29
然后像往常一样运行任何 php 命令,例如
php -v
如果您需要更多参数,请继续将它们添加为可选参数,例如${5} ${6}
【讨论】:
无需修改 Win 中的任何内容。 首先检查您在 Wamp 中使用的是哪个版本,然后从 PHP 的 Wamp 文件夹中运行命令,例如:
C:\wamp64\bin\php\php7.2.33\php -v
例如,如果您应该运行 Magento 命令(但对于任何其他 CMS 或 PHP 软件都是如此),您将自己放置在安装 CMS 的文件夹中并运行该命令
C:\wamp64\bin\php\php7.2.33\php bin/magento setup:upgrade
【讨论】:
我在 Windows 上使用 Git Bash 并面临同样的问题,环顾四周后,我设计了自己的解决方案。
我创建了一个 .bashrc,将其保存在我的主文件夹中,但仍然没有什么问题,但这是我第一次使用 bash 脚本,但它仍然可以帮助其他人。
#!/bin/bash
default_php=php8.1.0;
default_mysql=mysql5.7.36;
php_folder=/c/wamp64/bin/php/;
mysql_folder=/c/wamp64/bin/mysql/;
current_folder=$PWD;
main_link_folder=/c/Scripts;
link_php=www;
link_mysql=db;
# In this case I would add C:\Scripts\www to my PATH variable
echo "PHP CLI Version: ${default_php//[!.0-9]/}, to change press P";
echo "MySQL CLI Version: ${default_mysql//[!.0-9]/}, to change press M";
echo '____________________________________';
while : ; do
read -s -n 1 k <&1
if [[ $k = p ]] ; then
for entry in "$php_folder"/*
do
if [[ ! $entry == *.txt ]]; then
arrVar[${#arrVar[@]}]=$(basename $entry)
fi
done
for i in "${!arrVar[@]}"
do
echo "$i : PHP ${arrVar[$i]//[!.0-9]/}"
done
read -s -n 1 opt <&1
if [ "${arrVar[opt]+abc}" ]; then
cd $main_link_folder
rm -rf $link_php
ln -rsfT $php_folder${arrVar[$opt]} $link_php
cd $current_folder
alias mysql="winpty $mysql_folder$default_mysql/bin/mysql.exe"
return
else
cd $main_link_folder
rm -rf $link_php
ln -rsfT $php_folder$default_php $link_php
cd $current_folder
alias mysql="winpty $mysql_folder$default_mysql/bin/mysql.exe"
return
fi
else
cd $main_link_folder
rm -rf $link_php
ln -rsfT $php_folder$default_php $link_php
cd $current_folder
alias mysql="winpty $mysql_folder$default_mysql/bin/mysql.exe"
return
fi
done
break
我刚刚将此脚本保存在我的用户文件夹中,每次打开 git bash 时它都会加载,太棒了!
我的问题解决了。
欢迎提出任何建议或进一步改进。
谢谢大家
【讨论】: