1

How to run a PHP script with command line globally, like:

update var1=abc var2 var3=def

"Update" located in fact: "C:\Localhost\Scripts\Update.php"

Many sources suggest this as:

php update.php var1=abc var2 var3=def

Shortly: How to run a PHP script as if it was a registered executable?

Phil
  • 157,677
  • 23
  • 242
  • 245
Digerkam
  • 1,826
  • 4
  • 24
  • 39
  • 2
    Possible duplicate of [Windows equivalent to #!/usr/bin/php](https://stackoverflow.com/questions/16646003/windows-equivalent-to-usr-bin-php). In particular, see [this answer](https://stackoverflow.com/a/16651493/283366) – Phil Nov 12 '18 at 23:37
  • Also see http://php.net/manual/install.windows.legacy.index.php#install.windows.legacy.commandline – Phil Nov 12 '18 at 23:40

2 Answers2

1

Disclaimer: I've written this answer for Linux/Ubuntu, I'm not sure if it'll work for Windows (the OP updated the question's requirements).


So, this is possible, the way I've described here basically just has a global bash script call a PHP file. Here's the steps:

  1. Make a new bash script:

    #!/usr/bin/env bash
    php /path/to/yourphpfile.php
    
  2. Change the file executable: chmod +x /path/to/yourbashscript

  3. Copy the bash script into /usr/local/bin/: sudo cp /path/to/yourbashscript /usr/local/bin/
  4. Open a new terminal, then run yourbashscript (or whatever you named it).

And voila! A globally available PHP script!

Ethan
  • 4,295
  • 4
  • 25
  • 44
  • 1
    I forgot to mention but my OS is win10, does it matter? – Digerkam Nov 12 '18 at 23:47
  • 1
    @Digerkam Hmm, I'm not sure. I'd written this answer for Linux/Ubuntu, but if it works for Windows 10 then great :) I'll update my answer if I find a way specifically for Windows. – Ethan Nov 12 '18 at 23:49
  • 1
    In Windows you can add an environment variable to point to a path or file. Google environment variables windows to find out more. Then use: php %yourNewPath% – siggi_pop Nov 12 '18 at 23:55
  • @siggi_pop I understand, but it looks very dirty: php %update%... Is there any way to remove percentage or "php" prefix? – Digerkam Nov 13 '18 at 08:42
  • 1
    It should be possible without the percentage symbol. The PHP prefix is necessary, it's the global alias for php.exe. what you could do is type PHP + enter once to open the PHP cli and from there you could use PHP command without the prefix. – siggi_pop Nov 13 '18 at 09:12
1

I finally solved it:

-Create a .bat file where your php file in, it contains:

@ECHO OFF
php %~dp0/update.php %*

-And save it as "update.bat"
-Add the directory name into "Path" in "Environment Variables"

Now you can write "update var1=abc var2 var3=def" freely in command prompt!

Digerkam
  • 1,826
  • 4
  • 24
  • 39