Running php file through command line? - php

Everytime I try to load a file through command line I get this message:
Could not open input file: dummy.php
I have tried everything,
php C:\Program Files (x86)\Zend\Apache2\htdocs\dummpy.php
php dummy.php
php htdocs\dummy.php
I can't run a file! I can get php information and all other stuff, so I belive my CLI is ok.
I actually need this to run phpwebsocket, but I can't run anything through CLI.

You either need to enter the path without spaces, like this:
php c:\progra~2\Zend\Apache2\htdocs\dummy.php
Or, you can put quotes around the whole thing since your path does have spaces, like this:
php "C:\Program Files (x86)\Zend\Apache2\htdocs\dummy.php"
Or, when you open your command prompt, make sure to navigate to the folder that your PHP file exists in. For example, a simple navigation might look this this (type enter after each line):
cd \
cd Program Files (x86)
cd zend
cd apache2
cd htdocs
php dummy.php

If you write
php dummy.php
you must be sure that your current directory is
C:\Program Files (x86)\Zend\Apache2\htdocs\

Related

What does the command php bin/composer.phar do?

To install Composer in my shared host, I ran from my home directory:
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
Then I was told to run: $ php bin/composer.phar
And following that I was able to use Composer, confirmed by running:
$ composer.phar -V
Composer version 1.0-dev
Can someone please explain the command php bin/composer.phar?
Is it simply telling php to unpack the .phar file? If so, where does it unpack it to? I see bin/bin/composer.phar* after having run it. Then I have to run composer using the entire path to the phar file, unless I'm in the home directory.
A .phar file is a ZIP-like file that contains all the files that make up a PHP application, and some initial code in the file header that sets up some stuff and then transfers execution to the first zipped file.
The command php bin/composer.phar is calling PHP and gives the phar file as a parameter to make PHP execute that file. This will make PHP read that file, find the header with the bootstrapping code and execute that. PHP will initialize PHAR execution and go further into that compressed file.
The command composer.phar will only work if that file has been marked with the x file flag to make it executable, and it is placed in a directory that is mentioned in the $PATH environment variable. Part of the PHAR file is the first line of the file header that is called "shebang". This line is used in all script files for the command line to specify the interpreter that should be used to execute the file. In case of composer.phar, this interpreter is PHP. So in essence, the same thing happens as with the first call: PHP is "running" the PHAR file, but with less typing on the command line.

Creating batch file launching .phar with alias

I'm currently experimenting with WP-CLI (http://wp-cli.org) - I have finally managed to get it to run on my basic command line by downloading/moving it to a PATH variable directory (i.e C:\xampp\php) and then updating my PATHSPEC to include .PHAR.
I have renamed the original file from wp-cli.phar to wp.phar in order to be able to reference it as wp in my cmd.exe.
Prior to this method I installed wp-cli using CURL and chmod in my Git Bash installation then renaming the file to wp (without an extension) and adding the path containing file to the PATH variable. This caused the .PHAR file to work in Git Bash but not to work on the command line.
MY ISSUE:
Whenever I try to use my wp.phar in the native CLI I get a php error report - it does recognise the command and does show a list of suggestions (which is usually given when syntax is incomplete or incorrect).
How do I even start to figure this out?
1 - I've attempted to look for a git batch file in my Git Bash directory to see if I can find a dependency I'm missing but no dice.
2 - My Git Bash is now not recognizing the wp command and I now need to refer to wp.pharand then add any sub-commands/arguments after. However using the Git Bash CLI wp.phar doesn't cause errors
After a few months I found a solution.
Make sure that your PHP root folder path and the folder path containing your phar files are added to the Windows PATH Environment Variable (to allow us to just use the filename to refer to them).
CMD Line Options (Run as Administrator):
assoc .php=phpfile and assoc .phar=pharfile
Next is adding the program that opens this file. Adding an ftype command allows us to open .php/.phar files without using the php.
ftype pharfile=php.exe %1 %* (The %1 is a placeholder for the file called and %* refers to any other arguments that many be input).
FINALLY
The key issue was getting the .phar scripts to execute in the command line as windows ends up trying to open these files in it's Open With Dialog
So what I did was wrap my .phar in a batch file with this command (in the same folder as the phar
echo php "%~dp0wp-cli.phar" %* > [name of file].cmd
I then run this file as [name of file] with any arguments and it works as usual.

Setting PHP variable path for Cygwin

I am having a lot of difficulty routing PHP to cygwin.
My goal is simply to run the following PHP test command successfully:
php -v
I'm currently getting the following error: -bash: php: command not found
This is what I tried in the .bash_profile document:
PATH = $PATH:/xampp/php
export PATH
I am using xampp and my php.exe is in the following path C:/xampp/php/php.exe, also I noticed there isn't a bin directory in my php directory. There is a bin directory in C:/xampp/apache perhaps this is the one I should link to? I tried this as well with no success.
In Cygwin you windows C drive is mounted to /cygdrive/c/ so if you wanted to add C:/xampp/php/ to your path, then you should add the following to your .bashrc:
export PATH=$PATH:/cygdrive/c/xampp/php
What #flungo wrote was absolutely correct:
I have to add this line to C:\cygwin64\etc\bash.bashrc in order for each new cygwin terminal to have the correct $PATH
export PATH=$PATH:/cygdrive/c/xampp/php

How do I run "php phpunit.phar" CLI from anywhere?

I just installed phpunit.phar and am trying to set it up. What I am trying to do is to put phpunit.phar in a path so that I can run it from anywhere directly from the php cli. At this point, it is in my include_path and I can include it within a script from anywhere. But, what I think I need to do is run it from the cli "php phpunit.phar".
Is this doable or is it even appropriate?
I think that the phpunit.phar just need to be in your path variable. Or write a small script which is located in /usr/bin with this content:
#!/bin/sh
php /path/to/phpunit.phar
if you are using windows
create bat file named phpunit.bat
past the below line
keep the file any where you want
set the path variable on windows
%~dp0php.exe %~dp0pharcommand.phar %*
Now you can run on cmd prompt
phpunit --v
This is no longer necesssary, according to http://phpunit.de/getting-started.html. You can simply rename phpunit.phar to phpunit, chmod it executable, and put it in your path (usually /usr/local/bin) and it should automatically detect the interpreter directive.
You can run it from everywhere. Just give the correct path:
/usr/share/pear/exec/phpunit.phar -c app

Executing php program in eclipse

I installed php eclipse in my system i wrote sample php program and try to run it i'm getting error like this
Exception occurred executing command line.
Cannot run program "C:\Program Files\xampp\php" (in directory "C:\Program Files\xampp\php\www\Hello world"): CreateProcess error=5, Access is denied
Try naming your projects without spaces & installing XAMPP somewhere other then in Program Files like on the root C:
C:\xampp\php\www\HelloWorld
Most command line programs (EG:The launcher in this case) have problems with spaces in files & folder names, including the /Program Files/ directory.
Or in eclipse change the path to php.exe to C:\PROGRA~1\xampp\php

Categories