Writing to files in the PHP CLI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pcfreak30
    New Member
    • May 2008
    • 5

    Writing to files in the PHP CLI

    ok, i probably have not been registered here long, but i am no noob. I am very experienced in php, but the php cli is a bit of new territory for me.

    I am trying to create a simple little script that will fetch a webpage and save it to a file. This is just for practice for the records.

    here is my code:

    [PHP]

    <?php
    fwrite(STDOUT," Created By Derrick Hammer\n");
    fwrite(STDOUT," Copyright 2008 Derrick Hammer\n");
    fwrite(STDOUT," Derrick Hammer.com\n");


    fwrite(STDOUT," \n\nPlease enter the URL you wish to fetch: ");

    $url=fgets(STDI N);
    fwrite(STDOUT," Fetching ...\n\n\n");

    $data= file_get_conten ts($url);

    fwrite(STDOUT," What file do you want the data to be saved to? ");
    $file= fgets(STDIN);

    $f= fopen($file,"w+ ");
    if($f){
    fwrite($f,$data );
    fclose($f);

    fwrite(STDOUT, "Data successfully saved! Press Enter to exit ...");
    fgets(STDIN);

    }
    else{

    fwrite(STDOUT, "ERROR! Cound not open file $file . Press Enter to exit ...");
    fgets(STDIN);
    }
    exit(0);
    ?>

    [/PHP]

    it always gives me an error. here is the command line output:

    Code:
     
    
    
    C:\php5>php "C:\Documents and Settings\Administrator\Desktop\fetcher.php"
    Created By Derrick Hammer
    Copyright 2008 Derrick Hammer
    Derrick Hammer.com
    
    
    Please enter the URL you wish to fetch: http://www.google.com
    Fetching ...
    
    
    What file do you want the data to be saved to? google.txt
    PHP Warning:  fopen(google.txt
    ): failed to open stream: Invalid argument in C:\Documents and Settings\Administ
    rator\Desktop\fetcher.php on line 17
    ERROR! Cound not open file google.txt
     . Press Enter to exit ...
    
    C:\php5>
    what the heck is going on???

    (note this is on windows) i do have cygwin, but cygwin and php dont mix well with me.

    also, i have tried different file modes and also have use php5's file_put_conten ts()
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    You could maybe have two reasons for the error message you are getting.

    Firstly, it seems as if you are working on a Windows machine. Try to specify the full path of the file you want to write to. In other words, instead of just "google.txt " try to qualify it with full path including drive letter, e.g. "C:\google.txt" . You could do this by prepending a standard directory path stub to whatever file name you type in at the keyboard.

    Secondly, you must make certain that the directory that you are trying to write the output file to is "writeable" by the PHP CLI program. That is, let us say you set up a directory that you want to write these files to, like C:\myoutput\, then make sure this directory does not have any security properties that forbids the PHP CLI program from creating files and writing to it.

    Comment

    • pcfreak30
      New Member
      • May 2008
      • 5

      #3
      1. tried that via getcwd() function, same result

      2. it wouldnt because i created the directory myself and extracted the php to there. No installer. Oh and it does not matter weither the file exists or not, it fails every time. If i cant not do that i might as well give up on cli.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        The problem is that all the user input is being cought, including the return character (the Enter key), which makes the path to the file illegal. (You can see this on the error message... note how everything after the file name is shown in a new line)

        Try running your file name through trim() before using it.

        Comment

        • pcfreak30
          New Member
          • May 2008
          • 5

          #5
          thankyou, it worked.

          no all i need to do is decode the urls so i dont have things like %20 which wont be a problem.

          Thank you!!!

          Comment

          Working...