Page 1 of 1

Path issue

Posted: Sun Jul 03, 2016 4:27 am
by quantumnube
Hello everyone,
I ran into an issue and dont have an explanation for it.
Trying to get global command prompt access for composer using the terminal.
First I set the path in the environment and rebooted, (windows) and checked it ok.
Next I made a little hiworld.php file and for the test put it file in the same dir as php.exe.

Thinking I had global capabilities I entered php hiworld.php from C:\ and get Could not open input file.
changed into the same dir as the hiworld.php and php.exe and it worked.
went back to the C:\ typed 'd' ^z and it echoed back: 'd'
Then typed php C:\dir0\dir1\dir2\hiworld.php it worked
Then typed php C:\dir0\dir1\dir2 hiworld.php it did not work Could not open input file

Whats perplexing is that a dos batch file and php 'd' both echo back from C:\
but it wont execute the .php file from C:\ unless I include the whole path? I assume that is the problem I am running into with composer as well?

I thought all I'd have to do is type php hiworld.php from anywhere and it would have worked like a dos batch file? An explanation escapes me.

Re: Path issue

Posted: Sun Jul 03, 2016 7:48 am
by requinix
The "global command prompt access" thing only applies to finding php.exe. When you tell PHP to run "hiworld.php" it's going to look in the current directory. Not PHP's directory, not in the global path, just where you are at that moment. If you give a full path to the file it will work, obviously. If you say

Code: Select all

php C:\dir0\dir1\dir2 hiworld.php
then you're telling PHP to run the "C:\dir0\dir1\dir2" script and pass it an argument of "hiworld.php". Which is not at all what you want to do.

The exact same thing happens with composer: if you run "php composer.phar" then composer.phar must be in the current directory.

I don't know what you think batch files do differently. They work the same way.

Re: Path issue

Posted: Sun Jul 03, 2016 12:16 pm
by quantumnube

Code: Select all

php C:\dir0\dir1\dir2 hiworld.php
Interestingly in the expression above php does not assume the missing '\' and it has to be the full path to work as below. That seems odd to me but....

Code: Select all

php C:\dir0\dir1\dir2\hiworld.php


In doing more tests found it differs from a dos batch file in that a batch file, as long as its in the windows environment path will execute from any directory just by entering the filename in the command prompt where php, as you said, only executes if you are in its current directory or use the full path statement with the command. Thanks!

Re: Path issue

Posted: Sun Jul 03, 2016 11:05 pm
by requinix
quantumnube wrote:

Code: Select all

php C:\dir0\dir1\dir2 hiworld.php
Interestingly in the expression above php does not assume the missing '\' and it has to be the full path to work as below. That seems odd to me but....

Code: Select all

php C:\dir0\dir1\dir2\hiworld.php
Because they're two completely different things. Did you not read what I said earlier?
quantumnube wrote:In doing more tests found it differs from a dos batch file in that a batch file, as long as its in the windows environment path will execute from any directory just by entering the filename in the command prompt where php, as you said, only executes if you are in its current directory or use the full path statement with the command. Thanks!
Again, as I said, the path thing only affects the program you are invoking. php.exe. This magic batch file you keep talking about is either changing the working directory or calling a program which looks for files in a different way than PHP (and, in fact, nearly all programs).