📜 ⬆️ ⬇️

PHP5 as a scripting language for Windows

At this weekend I do the cleaning, incl. and on the computer. Cleaning is associated with a bunch of repetitive actions - I want to automate them and leave to drink beer. But…

The niksoids * have shell scripts and Perl. I envy.
Windows users have BAT scripts, js scripts and vbs scripts. The first lacks flexibility, and there is no sense in learning Javascript or VBScript.

But I have PHP with its great features. And PHP5 can be used through the Command Line Interface, in short, to call scripts without a server, from the command line. It looks like this somewhere:
 > php.exe script.php

Sloppy and non-native. I'll tell you how you can bring PHP-scripting under Windows to the same convenience as BAT. I lie - to the same as shell-scripts. The linuxsoids will be jealous!

Rule the registry


File associations in Windows are stored in the HKEY_CLASSES_ROOT branch in the registry. I will not go into details for it has prepared everything you need in the file php_reg.zip .
')
Download the file, unpack it, open it in the editor ( do not import it into the registry! It’s too early.)

Replace d:\\php with the path to the installed PHP5 (with double slashes, of course), and d:\\windows\\temp with the path to the folder with temporary files (yes, I have Windows with D:).

Now you can import.

Opportunities


Now PHP scripts are run almost in the same way as BAT scripts, namely, not only by double-clicking, but they also take command line arguments:
 > do_some_stuff.php with some args

Note that neither $_GET , nor $_POST , nor $_REQUEST available from console scripts. Instead, use $_SERVER['argv'] :
 _SERVER ["argv"] => Array
 (
     [0] => F: \ do_some_stuff.php,
     [1] => with,
     [2] => some,
     [3] => args
 )

 _SERVER ["argc"] => 4

Naturally, we have all the PHP toolkit in our hands - and this, in addition to the powerful built-in array and string processing functions, also the PDO , GD2 , SimpleXML + DOM libraries , curl , PEAR classes, and all other PHP features.

PHP and in console mode is capable of almost anything!

Input Output


For output echo and print are fine, but the result will be displayed after the script ends. If you need to withdraw something immediately - use
 fputs (STDOUT, "Hey! I'm deleting your documents here a little!");

By the way, it’s better not to speak Russian to the console - it uses CP866 encoding, not CP-1251, and certainly not UTF8 ...

If you need to ask something from the user:
 $ input = fgets (STDIN);

Return result


PHP scripts can return values ​​as BAT or as normal programs. The value is passed to the exit parameter:
 exit (1);  // exit with result 1

A little convenience - see the result in HTML


And if you want to display the results of the script in HTML? I provided it.

Right click on the script, “Open in the browser” - and after the execution, your default browser will open with the page that the script issued.

To use this default behavior, in the .reg file you need to correct the line:
 [HKEY_CLASSES_ROOT \ PHPFile \ Shell]
 @ = "OpenInBrowser"

A little more convenience - simplify the connection of files


Recall that if you specify in php.ini
 include_path = ".; d: \ php \ includes"

it will be possible to connect files from the d:\php\includes folder to any script.

Illustrative example


Well ... Let it be a folder tree output script - in HTML, of course!
 <? php
 function recurse_dir ($ dir)
 {
   $ olddir = getcwd ();
   chdir ($ dir);
   $ thisdir = getcwd ();
   echo "<ul>";
   if ($ handle = opendir ('.')) {
     while (false! == ($ file = readdir ($ handle))) {
       if ($ file! = "." && $ file! = "..") {
 ?> <li> <a href="file://<?=$thisdir?> \ <? = $ file?> "> <? = $ file?> <?
         if (is_dir ($ file))
           recurse_dir ($ file);
 ?> </ li> <?
       }
     }
   }
   closedir ($ handle);
   echo "</ ul>";
   chdir ($ olddir);
 }

 if ($ _SERVER ['argc']> = 2)
   $ dir = $ _SERVER ['argv'] [1];
 else
   $ dir = getcwd ();
 ?>
 <h1> <? = $ dir?> </ h1>
 <?
   recurse_dir ($ dir);
 ?>



Crosspost from my blog: PHP5 as a scripting language for Windows

Source: https://habr.com/ru/post/22158/


All Articles