Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: fopen() problem

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default fopen() problem

    Hi all, just wanted to mess around with this function a bit and was trying a few examples from w3 schools, but for some reason it doesn't seem to work for me.

    I have the following simple code:

    Code:
    <?php
    $file = fopen("http://www.w3schools.com/","r");
    ?>
    But although it doesn't give any errors when I open the page, nothing happens. That code is taken straight from the w3 website, is it something to do with my host not allowing this feature? I thought it would play quite a big part for some websites so it seems sort of illogical for it to be disabled, any ideas?

    Thanks,

    Jack

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    It works!

    You have to do something with that stream now. Might I suggest fread()?

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You cannot open w3schools - you must chose a local file.
    Jeremy | jfein.net

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

    Default

    You can open W3Schools if allow_url_fopen is on in your host's config. As techietim said, the main problem is that you've got the stream, but you're not doing anything with it.
    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!

  5. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I see, well that was pretty silly of me - however, I'm trying to open a file now on my C: drive that I know is there and has the path of - "C:\hello.txt".

    Here is my PHP code, and the errors I get:

    Code:
    <?php
    
    $filename = 'C:\\hello.txt';
    $file = fopen($filename,"r");
    
    $contents = fread($file, filesize($filename));
    fclose($file);
    
    echo $contents;
    
    
    ?>
    Code:
    Warning: fopen(C:\hello.txt) [function.fopen]: failed to open stream: No such file or directory in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 12
    
    Warning: filesize() [function.filesize]: stat failed for C:\hello.txt in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 14
    
    Warning: fread(): supplied argument is not a valid stream resource in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 14
    
    Warning: fclose(): supplied argument is not a valid stream resource in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 15
    I managed to get it to work for a file on the server, just not a local file - no errors but nothing showed up, any more suggestions?

    I've also tried C:\hello.txt, C:/hello.txt but both got me nowhere.

    Thanks for your help thus far,

    Jack

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You've gotta put the file or directory in:
    Code:
    /mnt/local/home/garlandsails/bristoldnb.co.uk/php/
    Jeremy | jfein.net

  7. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Yea I have succesfully done that before, but I thought you could open a file on the local hard disk with fopen? At least that's what it says on the W3 website.
    Last edited by Schmoopy; 01-09-2009 at 02:29 PM.

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Can you post a link to it on the w3 site? I can't find where it says that on w3schools..
    http://www.w3schools.com/php/func_filesystem_fopen.asp
    Post the link from w3 though.
    Jeremy | jfein.net

  9. #9
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I seemed to have read the page here wrongly : http://uk2.php.net/fopen

    I was getting confused with how it was saying "c:\\data\\info.txt", I interpreted as the C: drive.

    The other reason I was led to believe you could open a local file was because I was reading this tutorial: http://www.allsyntax.com/tutorials/P...Tutorial/6.php

    And on there it says "This can be a file on your system, or a file on a remote server."

    Is this just false, or could you do it on earlier versions of PHP?

    Thanks,

    Jack

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You can if your doing it local host such as:
    Code:
    C:/wamp/www/quick.php
    Jeremy | jfein.net

Tags for this Thread

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
  •