从PHP脚本更新PATHvariables

嗨,我想通过PHP脚本更新Path环境variables。 我使用PHP生成了一个.reg文件。 reg文件的内容看起来像

  [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment] 'PATH'='c:/abc/test/ImageMagick-6.7.8-Q8/convert.exe' 

当我运行这个文件,在最后一步我面临一个错误popup显示

  Cannot Import c:\User\qarni\downloads\13633555989_.reg: The specified file is not a registry script. You can only import binary registry files from within the registry script 

我试图用setx和我的.bat文件来完成这个任务

 @echo off set KeyName=Path set KeyValue="D:\songs;%PATH%" setx %KeyName% %KeyValue% 

这个文件运行并在不是系统variables的用户variables中创build一个pathvariables。

任何机构可以指导我这个错误,以及如何处理这种情况?

最好的祝福

为了logging,如果在php.ini启用了php_com_dotnet.dll (并且脚本以足够的权限运行),则以下PHP代码将起作用:

 <?php $path_to_add = "C:\\new\\path\\"; define("REG_VAL", "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\Path"); $WshShell = new COM("WScript.Shell"); $WshShell->RegWrite(REG_VAL, $WshShell->RegRead(REG_VAL) . ";" . $path_to_add); echo "The updated PATH in the registry is:\r\n\r\n"; echo $WshShell->RegRead(REG_VAL) . "\r\n"; 

如果运行REGEDIT,导航到你要更新的path,右键点击,select导出,你将能够保存一个例子.REG,看看你需要在PHP中重新创build什么。

你需要一个标题:

 Windows Registry Editor Version 5.00 

你需要使用双引号:

 "Path"="Something" not 'Path'='Something' 

Windowspath使用反斜杠,而不是正斜杠,他们需要用另一个斜杠“转义”。

 "C:\\Folder\\File.exe" not "C:/Folder/File.exe" 

嗨,我已经解决了这个问题。 我已经通过创build.bat文件解决了它。 .bat文件的内容看起来像

  @echo off set KeyName=Path set KeyValue="D:\songs;%PATH%" setx -m %KeyName% %KeyValue% 

如果您想要在system level for all users设置,则使用-m 。 如果您只想为当前用户设置,请删除-m 。 上面的命令将在环境Pathvariables中设置D:\songs 。 要运行这个,你需要成为系统的pipe理员。

欢呼如果这是有用的任何一个:-)