I am trying to read in the filename when running my php file (in CLI).
Runtime Command: php phpfile.php filename.csv
The line of code is as follows:
$argv = $_SERVER[argv];
I am getting the following error:
Use of undefined constant argv - assumed 'argv' in /home/holcim/csvimport.php on line 14
I have register_argv_argc
set to ON
.
$argv and $argc are set automatically when you run your script.
$argc gets you the number of arguments (including the script name), and $argv is an array containing the arguments.
That's because $_SERVER
doesn't exist for the CLI SAPI, because there is no server at the command line. You just use $argc
and $argv
because they're automatically populated.
See this doc page for some details about running scripts from CLI.
EDIT
Note that $argv = $_SERVER[argv];
would return the Use of undefined constant argv - assumed 'argv'
in any SAPI (even for a webserver). It should be $argv = $_SERVER['argv'];
unless you have argv
defined as a constant.
Make use of the argv array and argc variables. Here's code to start you off. It displays an error if you try to run it in a web browser. If the parameter count is less than 2, then it displays a mini help message and exits. Finally, when at least 1 argument is entered, argument1 will contain the first argument value.
<?php
if ($_SERVER['HTTP_USER_AGENT']){echo "Doesnt work in web mode";exit();}
if ($argc < 2){
echo "
";
echo $argv[0]." needs arguments as follows...
";
echo "argument1 -- does something
";
exit();
}
$argument1 = $argv[1];
?>