Domanda

How to run php file with parameter using notepad++

test.php

<?php 
    /* ------- 
        test.php
    ------- */
    if(!isset($_GET['file'])){
        exit;
    }
    $code=file_get_contents($_GET['file']);
    echo $code;

?>

demo_file.php -----$(FULL_CURRENT_PATH)

contents:

hello world


cd "D:\PHPnow-1.5.6\htdocs\zc_default\my_debug_fw"<br>
"d:\PHPnow-1.5.6\php-5.2.14-Win32\php.exe" "test.php" [what here?]

how to send "demo_file.php" as $_GET['file'] to test.php ?

The console finally should output:...... hello world

È stato utile?

Soluzione

When utilizing PHP from the command line the arguments aren't passed in as part of the $_GET superglobal. They're passed in as part of the $_SERVER - superglobal where $_SERVER['argc'] is the number of arguments and $_SERVER['argv'] is an array of the argument values. $_SERVER['argv'][0] is the name of the php script and $_SERVER['argv'][1] would be the first argument.

    if($_SERVER['argc'] < 2){
        exit("Usage: php test.php <file>");
    }

    $code = file_get_contents($_SERVER['argv'][1]);

Per your example above...

Altri suggerimenti

After closing Notepad++, go to %APPDATA%/Notepad++ and open shortcuts.xml file in Notepad. Within append the following line.

<Command name="Launch Server" Ctrl="yes" Alt="yes" Shift="no" Key="90">chrome &quot;http://localhost/index.php?file=$(FULL_CURRENT_PATH)&quot;</Command>

Now change the content of default index.php in htdocs to

<?php
    if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS']))
    {   $uri = 'https://'; }

    else
    {   $uri = 'http://'; }
    $uri .= $_SERVER['HTTP_HOST'];

    if(isset($_GET['file']))
    {
        $root = $_SERVER['DOCUMENT_ROOT'];
        $file = str_replace('\\', '/', $_GET['file']);
        $file = str_replace($root, '', $file);

        header("Location: http://localhost{$file}");
    }
    else
    {
        header('Location: '.$uri.'/xampp/');
        //header('Location: '.$uri.'/yourproject/');
    }
    exit;
?>
Something is wrong with the XAMPP installation :-(

Now run Xampp and click Notepad++>Run>Launch Server or use Shortcut Key CTRL+ALT+Z to directly run the code!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top