Results 1 to 7 of 7

Thread: stupid php question (stream_get_contents)

  1. #1
    Join Date
    Aug 2006
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default stupid php question (stream_get_contents)

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

    PHP Code:
    <?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);

    ?>
    i get this error:

    Unable to write to file


    test.txt is chmodded to 777.
    Last edited by cursed; 06-26-2007 at 09:15 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's probably because you're trying to write the file handler to the file. You need to read the data from it and write that instead.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2006
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks for the reply. glad you got promoted to Global Moderator .

    i changed my code a bit, and i can echo it, but i cant record it into a file. i think im missing something very obvious.

    PHP Code:


    <?php

    $file 
    fopen("/home/gamesjet/public_html/test.txt""a+");
    if (
    $stream fopen('http://www.example.net''r')) {
        
    // print the first 5 bytes

    $contents stream_get_contents($stream100000);
        echo 
    $contents;

    echo 
    file_put_contents($file$contents);


        
    fclose($stream);
    }

    ?>

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try taking out the echo before file_put_contents().

    Code:
    <?php
    
    $file = fopen("/home/gamesjet/public_html/test.txt", "a+");
    if ($stream = fopen('http://www.example.net', 'r')) {
        // print the first 5 bytes
    
    $contents = stream_get_contents($stream, 100000);
        echo $contents;
    
    echo file_put_contents($file, $contents);
    
    
        fclose($stream);
    }
    
    ?>
    That should get it to work (or at least produce an error if none is present now).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Aug 2006
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    error:


    Warning: fopen(/home/gamesjet/public_html/test.txt) [function.fopen]: failed to open stream: Permission denied in /home/gamesjet/public_html/proxy/index.php on line 3

    You have reached this web page by typing "example.com", "example.net", or "example.org" into your web browser.

    These domain names are reserved for use in documentation and are not available for registration. See RFC 2606, Section 3.

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Try chmod the directory.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You don't want to do that in this case, because the file is located in the OP's root directory. You may want to make sure that the file exists and is writeable, other than that I have no clue.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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