0

Hi there I want to update Path environmental variable through PHP script. I have generated a .reg file using PHP. The content of reg file looks like

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

When I run this file, at last step I face a error pop displaying

 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

I have tried to do this task using setx and my .bat file looks like

@echo off

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

This files runs and create a path variable in user variables not in system variable.

Can any body guide me about this error and how to handle this situation?

Best Regards

Awais Qarni
  • 121
  • 4

3 Answers3

1

For the record, the following PHP code will work if php_com_dotnet.dll is enabled in php.ini (and the script is running with sufficient privileges):

<?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";
0

If you run REGEDIT, navigate to the path you are trying to update, right click, choose Export, you will be able to save an example .REG to see what you need to re-create in PHP.

You need a header:

Windows Registry Editor Version 5.00

You need to use double quotes:

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

Windows paths use back slashes, not forward slashes, and they need 'escaping' with another slash.

"C:\\Folder\\File.exe" not "C:/Folder/File.exe"
WhoIsRich
  • 421
  • 4
  • 7
  • @WholsRic. I have added Windows Registry Editor Version 5.00 and changed slashes too. Now file succfully run but my values are not getting appended in path variable ? – Awais Qarni Mar 18 '13 at 10:22
  • .REG files overwrite, not append, so you would need to lookup existing entries using PHP. Alternatively you could use 'SETX' instead of a .REG file, but still need to check your entry is not already set. – WhoIsRich Mar 18 '13 at 11:43
  • @WholsRic Please see I have updated my question – Awais Qarni Mar 18 '13 at 12:29
0

Hi I have solved this issue. I have solved it by creating .bat file. The content of .bat file looks like

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

-m is used if you want to set it at system level for all users. If you want it to set only for current user, then remove -m. The above command will set D:\songs in environement Path variable. To run this you need to be the administrator of the system.

Cheers if this is useful for any one :-)

Awais Qarni
  • 121
  • 4