How to unset PATH? - php

I know it is possible to set PATH programmatically with using for example
SET PATH=%PATH%;C:\wamp\bin\php\php5.4.16\
But how can I unset PATH programmatically?
The reason why I want to unset PATH is that I could not rename my text file because it will show this error.
The action cannot be completed because the file is open in php.exe

It is possible to unset PATH in a command prompt window or in a batch file for everything started from within this command interpreter process after doing that.
The possible commands are set PATH= or path ; as explained in help of command path output in a command prompt window after entering path /? or help path.
But PATH is not unset for any process already running and for new process started not from within this command interpreter process.
To remove PATH is a good method for testing a batch file which should be executed by Windows task scheduler. If nothing in the batch file depends on PATH, the probability is high that it works also with Windows task scheduler running with a different account/credentials.
But in your case of being not able to rename a text file as being currently opened by php.exe, it is of no use to unset PATH. Terminate PHP process and rename the file, or restart Windows which results also in terminating PHP process and rename the file after Windows started again.

You just set a new path
path c:\windows\system32;c:\windows;c:\windows\system32\wbem
which is system standard path.
You can use set to remove C:\Program Files\Debugging Tools for Windows from path.
`set path=%PATH:C:\Program Files\Debugging Tools for Windows=%`
watch out for trailing semi colons.
Though it's unlikely to help your specific problem.
The formatting has fucked up, make sure you put =% on the end of the line.

Related

Running php in cmd gives access is denied and "This app can't run on your PC"

I have installed xampp on my windows 8.1 and wanted to add PHP to environmental variables. Therefore, under path i added ;C:\xampp\php; in the end, like i have done some long time ago.
Now, when i try to run any php function in cmd, i get the windows message
"This app can't run on your PC.."
and a "Access is denied" in cmd.
If i remove php from the path variable, i still get the same error.
What i suspect could be a problem is that when trying to add the variable by typing SET PATH=%PATH% I accidentally clicked enter and therefore might have added an some weird value, although i have double checked and the whole line looks fine (no weird leftovers or something).
I had the same problem, I have tried a lot of things, but without success ... At the end of my efforts, I noticed that the size php.exe is 0KB , so I copy php.exe from my backup folder, where size is php.exe 75kB and php via cmd works
I also faced the same problem, when I'm using composer(Dependency Manager for PHP). Then I realised the problem was coming from php.exe.
The causes of the error message "Access denied" in php.exe and possible solutions:
Path variable may be not added, so make sure that your path variable is added properly.
The antivirus software may have blocked access to the php.exe file. Check your antivirus software's log and quarantine.
You don't have the permissions to run the php.exe. Change the permissions of the file.
The php.exe file may have been damaged. Reinstall the application.
Some malware can be added a php.exe file into windows or system32 directory. Since default path variable includes those directories, operating system will load that malware rather than original php.exe . So try by adding your PHP directory path to the top of the path variable.(this was the problem in my case)
Refer this link for more details on common php.exe errors.
This worked for me:
(My PHP installation lives in C:\PHP. Administrator has sufficient rights.)
Added C:\PHP to PATH variable
Removed the 0Kb php.exe file that lived in C:\Windows\System32
(I guess this file caused the windows refusal to execute)

Can't access PHP command in Command Line

I have a problem in accessing the php command using command line.
I also created a PATH in my environment variables.
The PATH is like this
C:\wamp\bin\php\php5.5.12
Now the console output is this
C:\Users\Web4>php
'php' is not recognized as an internal or external command,
operable program or batch file.
I also restarted the PC and the command console still same effect. I also created a batch for this still same effect too.
SET PATH=%PATH%;C:\wamp\bin\php\php5.5.12
Is there some configuration other than this? I am using wampserver.
If you run echo %PATH% and it does not include the path to WAMP, then the problem is simply that you haven't set it properly in your environmental variables.
The setting is located here:
http://i.imgur.com/fy5LvRD.png
Edit the System Variables "Path" field and prepend your path to WAMP (obviously make sure you have the exact path to the folder where php.exe is located). Then click OK until all the settings dialogs are closed.
Once you change the path, you will need to close your CMD window and open a new one. The path changes don't take effect on any open CMD windows.

How to run .phar from anywhere on Windows?

I have .phar file and i want to run it from anywhere with this command.
php file.phar --options
where file.phar is in C:\my-php-cli\file
That is not possible. For example, putting file.phar to C:\Windows\System32 and calling php file.phar --options from another directory like C:\Temp will produce
Could not open input file: file.phar
You have to create a small batch file next to your *.phar, e.g., run-file.bat with this contents:
#ECHO OFF
php %~dp0file.phar %*
Then, calling run-file will execute your file.phar.
For Windows 10 I found that you need a your-file.cmd to run the .phar file.
I was trying to get php-cs-fixer running so in my PHP directory i had the following:
PHP\
phpFixer\
php-cs-fixer.phar
php-cs-fixer.cmd
Contents of php-cs-fixer.cmd:
#php "%~dp0php-cs-fixer.phar" %*
verified it works by entering this in command line:
php-cs-fixer --version
PHP CS Fixer 2.14.0 Sunrise by Fabien Potencier and Dariusz Ruminski (b788ea0)
Hope that helps
You would need to add the folder containing file.phar to your path variable.
See this link for more information.
To make wp-cli.phar work,
Create wp.bat
Put the following in it:
#ECHO OFF
php %~dpn0.phar %*
%~dpn0: Is a placeholder for $0 (Linux users will understand).
The directory where you want to place wp.bat, make sure to add that path to the environtment variables.
Rename wp-cli.phar to wp.phar (should be the same as the name of the .bat file, i.e. wp.bat and wp.phar. It is better to have both in the same directory.
Open a new PowerShell window, and execute
wp --help
The above command will only work if you have PHP installed.
This is how you make .phar files work the way .bat or .vbs files work where you can run them directly or add them to your path and it will automatically run them through the appropriate interpreter (in this case PHP)
Add ;.PHAR to the end of your PATHEXT environment variable (searching "Edit the system environment variables" in the start menu should get you to the right place). This is a semicolon separated list of file extensions that will be run in the same command window rather than launching their own window.
Set the default program for .phar files to be php.exe. You can get to the settings for this by searching "Choose a default app for each type of file" in the start menu or by selecting "Try an app on this PC" > "Look for another app on this PC" in the dialogue that opens the first time you try to open a .phar file.
At this point you can run .phar files just like you would run a .bat file. You don't have to worry about what directory php.exe is in, but you do have to worry about what directory the .phar is in. If you want to make a particular .phar file available from any folder it needs to be inside a folder listed in your PATH. Either move the .phar to an existing folder in your PATH (like C:\Windows\system32) or add the folder containing the .phar to your PATH the same way you modified PATHEXT in step 1.
You have to close and re-open any cmd/powershell windows before environment variable changes go into effect.
Run your .phar file without putting php in front. If it is in your PATH you can optionally omit the .phar at the end as well. This is nice if you are using, for example, composer.phar. Once you set everything up and add it to your path, you can do things like composer --version from any directory.

How to update PATH Variable permanently Using PHP

Hi there I want to update Path environmental variable permanently through PHP script. I know about putenv() but it is not permanent. I have read some where of generating a .reg file with following content.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
'PATH'='c:/abc/test/ImageMagick-6.7.8-Q8/convert.exe'
I have generated the file with content.
When I run this file, it shows me that file has been run successfully and key values containing in file is added in registry successfully. But When I go to path variable to check, my values are not there :-(
Can any body tell me why is this happening. I am using window 7. But my work has also to be compatible with window xp. Or is there any other way to accomplish this task?
Need suggestions....
Best Regards
The PATH environment variable in the registry is copied to a process' environment when the process is created.
cmd.exe (the command window or shell) is a process in it's own right. You have to SET PATH= within the shell to change it.
If you open a new command window, you should have the new PATH set. See also Programmatically adding a directory to Windows PATH environment variable
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 :-)

Does it matter if I use relative or absolute path's when running a php script via cron?

I am currently working on a script that I am attempting to automate via cron. Running the script via terminal is just fine, but when I attempt to run the script with crontab, I am getting some issues.
Part of my script loads and validates and xml file via DOMDocument::loadXML() and DOMDocument::validate() and php throws an error when attempting to validate stating:
Failed to load external entity: /linuxuser/homefolder/my_dtd.dtd
Within the xml file, the dtd is set to:
../../../../../../../my_dtd.dtd
Is there some misconfiguration of the server or is it more likely something wrong with my php code at this point? It seems to grab my linux home directory rather than the path relative to the xml file. Just wondering if anyone else has seen an issue like this or could point me in the right direction. Thanks.
Quoting the PHP documentation for differences in CLI usage (command-line interface):
The CLI SAPI does not change the
current directory to the directory of
the executed script!
When PHP scripts are run via CRON, it is executed on the user's home directory. You can either replace all relative path references used by the script to absolute, or place this on the start of the script:
chdir(dirname(__FILE__)); # for PHP 5.2.x and below
# or
chdir(__DIR__); # for PHP 5.3+
Most likely, the problem is in working dir and resolving relative path.
Try absolute path in your xml file.

Categories