fopen, fget

Discussion in 'PHP' started by Silver89, Mar 15, 2008.

  1. #1
    I'm trying to download a list of files with php to a directory on my host, however i am recieved the following error ...

    "[function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections in"


    $filename = 'http://remotefilehere.com';
    
    $handle = fopen("$filename", "w");
    fwrite($handle, 'download/');
    fclose($handle);
    PHP:
    Is something wrong here?
     
    Silver89, Mar 15, 2008 IP
  2. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Dude,

    First of all, fopen needs a filename, you put there...

    $filename = "http://somesite.com" <-- this is not a file

    You could have at least put, $filename = "http://somesite.com/index.php", Im not sure but this could work if the site has read permission to all. Im not sure because I used fopen only on local files and not a file you get from another site. The error is saying something about HTTP, thats maybe because you are accessing a file from another machine.

    Lastly, just a minor thing, dont put quotes on the filename inside fopen...

    fopen("$filename", "r") this should be --> fopen($filename, "r");
     
    bokiatenxi, Mar 16, 2008 IP
  3. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I analyzed your code a bit more, I think what you want to do is add a directory on your site. If thats the case you should use the function mkdir()

    Hope my posts help..
     
    bokiatenxi, Mar 16, 2008 IP
  4. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #4
    Ok, but all i want to do is download an external .txt file to a directory (/downloads/) on my site.
     
    Silver89, Mar 16, 2008 IP
  5. mulari

    mulari Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Your all concept is wrong.

    Your php code is running on your site, so you don't need (and can't) access it through http.

    Here is an example on how to fetch a file and write it.

    $external = file_get_contents('http://www.somesite.com/file.txt');

    $save_filename = 'downloads/try.txt';
    $handle = fopen($save_filename, "w");
    fwrite($handle, $external);
    fclose($handle);

    Make sure "downloads" dir is created on your server with write permissions.

    (btw, if you use PHP5, you can simply use file_put_contents and skip the all fopen/fwrite/fclose thing)
     
    mulari, Mar 16, 2008 IP