...

View Full Version : passing arguments in cli to create new files



phpnoobtv
01-13-2012, 09:05 PM
Trying to create two different files from arguments passed in commend line, but it only creates one file in any arguments I passed.

script file: test.php


#!C:\wamp\bin\php\php5.3.8
<?php

define("SRV_RT",realpath(dirname(__FILE__).'/..'));

if ($argv =0 ){
$Data = SRV_RT."/file1.txt";
}
else{
$Data = SRV_RT."/file2.txt";
}

$Handle = fopen($Data, "w+") or die("can't open file");
fwrite($Handle, 'Work Dammit');
fclose($Handle);

?>
commend line

php test.php --> creates file2.txt * but I want it to create file1.txt*
php test.php arg1 --> creates file2.txt

What to do?

Spookster
01-13-2012, 09:22 PM
Trying to create two different files from arguments passed in commend line, but it only creates one file in any arguments I passed.

script file: test.php


#!C:\wamp\bin\php\php5.3.8
<?php

define("SRV_RT",realpath(dirname(__FILE__).'/..'));

if ($argv =0 ){
$Data = SRV_RT."/file1.txt";
}
else{
$Data = SRV_RT."/file2.txt";
}

$Handle = fopen($Data, "w+") or die("can't open file");
fwrite($Handle, 'Work Dammit');
fclose($Handle);

?>
commend line

php test.php --> creates file2.txt * but I want it to create file1.txt*
php test.php arg1 --> creates file2.txt

What to do?

What is it you are intending to check for here? if ($argv =0 ){

1. $argv is an array so you can't check it like that.
2. You are using an assignment operator rather than an equality operator
3. $argv will always have a value at $argv[0] which is the name of the script.

$argv[1] -$argv[x] will correspond to the arguments you passed to the script on the command line
php test.php arg1 arg2 arg3 argX

http://us3.php.net/manual/en/reserved.variables.argv.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum