stupid php question (stream_get_contents)

Discussion in 'PHP' started by shadowpwner, Jun 26, 2007.

  1. #1
    I can't seem to get this php code to work:

    
    <?php
    
    $file = '/home/gamesjet/public_html/test.txt';
    
    if (is_writable($file) == false) {
            die('Unable to write to file');
    }
    
    // The data
    $data = fopen("http://www.example.com/", "a+");
    
    // Write to file
    file_put_contents ($file, $data);
    
    ?>
    
    PHP:
    i get this error:

    Unable to write to file


    test.txt is chmodded to 777.
     
    shadowpwner, Jun 26, 2007 IP
  2. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you have read/write access to the file? Perhaps you don't have permission to write. . . .
     
    dankenpo, Jun 26, 2007 IP
  3. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #3
    Just a quick guess, I don't have much knowledge of this function, but shouldn't the fopen lead to /home/gamesjet/public_html/test.txt , instead of that web url?
     
    Crayz, Jun 26, 2007 IP
  4. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Crayz, I believe the fopen is opening the data he wants to write into the text file. I believe. . . .

    who knows. . .

    Maybe he can be more specific. . .
     
    dankenpo, Jun 26, 2007 IP
  5. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #5
    all the fopen is doing is opening the connection to the website. he needs a file_get_contents in there to actually set $data before writing. as of right now all it is doing is setting $data as the file handler.
    http://us2.php.net/file-put-contents
    http://us2.php.net/file-get-contents
    http://us2.php.net/fopen

    
    $a = file_get_contents("http://www.foo.com");
    file_put_contents("my_file.txt",$a);
    
    PHP:
    also file_put_contents is limited to php5 only so if using prior versions, use fopen and fwrite
     
    ansi, Jun 26, 2007 IP
  6. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ahh, now I see.
     
    dankenpo, Jun 29, 2007 IP