Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    passing arguments in cli to create new files

    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

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

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

            if (
    $argv =){
            
    $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?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by phpnoobtv View Post
    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

    PHP Code:
    #!C:\wamp\bin\php\php5.3.8
    <?php
     
        define
    ("SRV_RT",realpath(dirname(__FILE__).'/..'));    
     
            if (
    $argv =){
            
    $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/reserve...ables.argv.php
    Last edited by Spookster; 01-13-2012 at 10:25 PM.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    phpnoobtv (01-17-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •