www.webdeveloper.com
Results 1 to 11 of 11

Thread: [RESOLVED] fopen() always fails

  1. #1
    Join Date
    Mar 2008
    Posts
    70

    resolved [RESOLVED] fopen() always fails

    Hi how do I get my site to create files? blah.html does not exist and I'm trying to write it. the folder "cache" has permissions set to 777. I am running the script on the same domain as I am trying to create the file on, but it gives the same error as if I tried to write to a different domain. I can only use fopen to read.

    It used to work until I got a new server.

    PHP Code:
    //Try to make a local file.
    $file fopen('http://mysite.com/blah.html''w');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";
    }

    $file fopen('blah.html''w');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";
    }

    //Try to write a file with directory permission of 777
    $file fopen('http://mysite.com/cache/blah.html''w');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";
    }

    $file fopen('/cache/blah.html''w');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";
    }

    //Try a file that exists already.
    $file fopen('http://mysite.com/reconstruct.html''w');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";
    }


    //Try reading a file that exists.
    $file fopen('http://mysite.com/reconstruct.html''r');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";
    }


    //Try that thing.
    $file fopen('/chroot/home/mysite/mysite.com/blah.html''w');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";
    }


    //Try mysite IP.
    $file fopen('http://211.59.25.55/blah.html''w');
    if(
    $file)
    {
        echo 
    "file.";
        
    fclose($file);
    }
    else
    {
        echo 
    "nope.";


    Warning: fopen(http://mysite.com/blah.html) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in /chroot/home/mysite/mysite.com/html/fopen.php on line 17
    nope.
    Warning: fopen(blah.html) [function.fopen]: failed to open stream: Permission denied in /chroot/home/mysite/mysite.com/html/fopen.php on line 6
    nope.
    Warning: fopen(http://mysite.com/cache/blah.html) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in /chroot/home/mysite/mysite.com/html/fopen.php on line 29
    nope.
    Warning: fopen(/cache/blah.html) [function.fopen]: failed to open stream: No such file or directory in /chroot/home/mysite/mysite.com/html/fopen.php on line 40
    nope.
    Warning: fopen(http://mysite.com/reconstruct.html) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in /chroot/home/mysite/mysite.com/html/fopen.php on line 52
    nope.file.
    Warning: fopen(/chroot/home/mysite/mysite.com/blah.html) [function.fopen]: failed to open stream: Permission denied in /chroot/home/mysite/mysite.com/html/fopen.php on line 78
    nope.
    Warning: fopen(http://211.59.25.55/blah.html) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in /chroot/home/mysite/mysite.com/html/fopen.php on line 91
    nope.

  2. #2
    Join Date
    Jul 2009
    Posts
    91
    The second one is the way I do it myself and when I pasted your code without change it is the only one which worked (I didn't bother creating the cached directory). That is without 777 permissions set. So if it doesn't work for you it must be something about the server.

  3. #3
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    It looks like your host does not allow fopen. Try using file_put_contents and file_get_contents instead.

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    Is the location you are trying to write to on the same server as your script? If so you could use fopen without the HTTP wrapper (it is likely it is just URL file handling which is disabled).

    For example:

    PHP Code:
    $file fopen('/home/you/public_html/cache/somefile.txt''w'); 

  5. #5
    you may have to manually set the php.ini on your sever to allow for fopen() to work with HTTP://

    check with your hosting company or set allow_url_fopen=On in your PHP.ini file.

  6. #6
    Join Date
    Mar 2008
    Posts
    70
    Thanks for all the replies. Ok I can get it to work only if the path is "/chroot/home/mysite/mysite.com/html/myfolder" and myfolder permissions is 777. phpinfo() says allow_url_fopen is On.

    Any ideas on how to make fopen work with my http:// url?

  7. #7
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Just because the setting allows URL types does not mean that the host allows http as part of their firewall policy.

    My host has that kind of policy on using certain functions, while they may be enabled in php, their firewall locks it down.

    So this will be a question you need to ask your hosting company.

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Surely you would need something sitting on the target machine to receive the data and write the to the filesystem? I could be wrong as I've never tried it, but it doesn't make sense to me. Can you not use FTP instead of HTTP?
    Last edited by Mindzai; 07-31-2009 at 04:57 AM.

  9. #9
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Well to me it sounds like this is the beginnings of a site crawler or slurp engine which is possibly why the need for http.

    Theirs one / possibly Two method(s) that has not been discussed yet and until we know what the purpose of the function is and its intended purpose, IM not saying owt.

  10. #10
    Join Date
    Nov 2008
    Posts
    2,477
    Yes I could understand that if the OP wanted to read via HTTP, but it's the writing via HTTP which is throwing me off. I'm not sure how that would even work without something on the target machine to handle the actual writing to the filesystem.

  11. #11
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Good point, according to the manual,
    Example #1 fopen() examples
    PHP Code:
    <?php
    $handle 
    fopen("/home/rasmus/file.txt""r");
    $handle fopen("/home/rasmus/file.gif""wb");
    $handle fopen("http://www.example.com/""r");
    $handle fopen("ftp://user:password@example.com/somefile.txt""w");
    ?>
    So according to the last two examples, it would appear so.

    I suppose it is down to the local server policy and if it allows these actions or not.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles