Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
Don't do this. You will wipe out the existing PATH and break other applications on the user's computer. –  Michael Hampton Mar 15 '13 at 15:59
    
@MichaelHampton Then how can I add some path to my PATH variable through my script? –  Awais Qarni Mar 18 '13 at 7:42
    
Make a proper Windows installer for your application? –  Michael Hampton Mar 18 '13 at 11:21

3 Answers 3

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"
share|improve this answer
    
@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

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 :-)

share|improve this answer

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";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.