Problem with fopen command

Discussion in 'PHP' started by aries34, Jul 31, 2006.

  1. #1
    Could someone help me with this problem: have a bot on shell on one server, which make log file (log.txt) and I want to open that file on a specific page on my site which is on other server. I've tried to use following command (fopen) in .php file:

    <?php
    $file = fopen('http://shell-sample.com/user/archiva/log.txt', 'r') or exit("Unable to open file!");

    while(!feof($file))
    {
    echo fgets($file). "<br />";
    }
    fclose($file);
    ?>

    but I always get message:

    Warning: fopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/user/public_html/index.php on line 2

    Warning: fopen(http://shell-sample.com/user/archiva/url.txt): failed to open stream: Success in /home/user/public_html/index.php on line 2
    Unable to open file!

    Don't know what is the problem, is that right command and is it written right? or is there any other way (command) I could do same - open that file.
     
    aries34, Jul 31, 2006 IP
  2. Jdog

    Jdog Peon

    Messages:
    267
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Who is your host? I tried to use fopen on dreamhost and they do not allow it, so I had to switch it to curl and it worked perfect.
     
    Jdog, Jul 31, 2006 IP
  3. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your host may have disabled 'allow_url_fopen' . Try using cURL instead to access remote files.

    http://www.php.net/curl

    Thomas
     
    coderlinks, Aug 1, 2006 IP
  4. aries34

    aries34 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've tried curl and got this message:

    Warning: fopen(url.txt): failed to open stream: Permission denied in /home/user/public_html/qqq/q.php on line 4
    Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/user/public_html/qqq/q.php on line 6
    Warning: fclose(): supplied argument is not a valid stream resource in /home/user/public_html/qqq/q.php on line 11

    what's now wrong?
     
    aries34, Aug 1, 2006 IP
  5. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you sure you are using curl in the correct way? I will give you an example code:
    
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://shell-sample.com/user/archiva/log.txt");
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $output = curl_exec($ch);
    curl_close($ch);
    
    PHP:
    After that code, $output should contain the contents of the file if not error occured.

    Thomas
     
    coderlinks, Aug 1, 2006 IP
    Colleen likes this.
  6. daniah

    daniah Well-Known Member

    Messages:
    1,092
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    138
    #6
    the file you are trying to open via fopen does not exist. fopen cannot open the url http://shell-sample.com/user/archiva/log.txt, hence the error:
    php_network_getaddresses: getaddrinfo failed:
    check if the file exists by trying to open it in the browser
     
    daniah, Aug 1, 2006 IP
    Colleen likes this.
  7. aries34

    aries34 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    daniah, url "http://shell-sample.com/user/archiva/log.txt" you tried to follow is just a example, real url is http://shells.hostingmania.co.yu/~skanner/arhiva/log.txt :)
    coderlinks, when I use code you suggested no errors occured but also doesn't show me contents of file.... could u please use http://shells.hostingmania.co.yu/~skanner/arhiva/log.txt url and figure out what's wrong. Thanx in advance. Sorry on all trouble, I'm just a confused begginer. :)
     
    aries34, Aug 2, 2006 IP
  8. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #8
    My code only fetches the contents of the file into the variable $output. Then you have to 'echo' it.
    
    echo $output;
    
    PHP:
    Or you can directly put the contents from cURL to the browser, just use this modified code
    
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://shell-sample.com/user/archiva/log.txt");
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_exec($ch);
    if(curl_errno($ch))
    {
       echo curl_error($ch);
    }
    curl_close($ch); 
    
    PHP:
    This one also checks for errors and displays them.

    Thomas
     
    coderlinks, Aug 2, 2006 IP
  9. aries34

    aries34 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I've tried it, but doesn't work... did u tried it?
     
    aries34, Aug 2, 2006 IP
  10. daniah

    daniah Well-Known Member

    Messages:
    1,092
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    138
    #10
    try this code:

    $URL="http://shell-sample.com/user/archiva/log.txt";

    $file = fopen("$URL", "r");
    $r = "";
    do {
    $data = fread($file, 8192);
    if (strlen($data) == 0) {
    break;
    }
    $r .= $data;
    } while (true);
    fclose ($file);
    echo $r;
     
    daniah, Aug 2, 2006 IP
  11. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #11
    It would be better if you could put this code in a PHP file
    <?php phpinfo(); ?>
    PHP:
    And give me the URL to that file. That would give some info as to whats wrong.

    Thomas
     
    coderlinks, Aug 2, 2006 IP
  12. Jdog

    Jdog Peon

    Messages:
    267
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #12
    With my host I had to curl any files/urls that are on a different server and write them to a text file on my server and fopen that. They won't let you do a whole lot with files that aren't yours.
     
    Jdog, Aug 2, 2006 IP
  13. daniah

    daniah Well-Known Member

    Messages:
    1,092
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    138
    #13
    set $URL to where the log file is located, the code will work

     
    daniah, Aug 2, 2006 IP
  14. aries34

    aries34 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    DANIAH, KISS! FINALLY! thank you so much, that code finally works! sorry again for all trouble... :) btw, when I tried it first time, I typed one letter wrong :( my mistake
     
    aries34, Aug 2, 2006 IP
  15. daniah

    daniah Well-Known Member

    Messages:
    1,092
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    138
    #15
    gald it finally worked...next time you get an
    Warning: fopen(): php_network_getaddresses: getaddrinfo failed: error, you know what to do ;)
     
    daniah, Aug 2, 2006 IP