Page 1 of 1

How to download a file (with browser) using fsockopen() ?

Posted: Sun Feb 03, 2008 10:30 am
by szalinski
Hi

I have been working on this download script for quite a while, and I just can't find how to download a remote file via a user's browser using fsockopen.

Basically I am wondering if anyone can just give me a simple working example on how to use fsockopen() to fetch a file on a remote server, and then pop up a save dialog box in my browser. For example, let's say I want to download this file from here:

Code: Select all

http://remotedomain.com/file.zip
Instead of putting this directly into my browser and then being prompted to save it to my pc, how can i use fsockopen() to fetch the file, and get the same prompt on my browser? E.g. I want to be able to do

Code: Select all

http://localhost/index.php?url=http://r ... m/file.zip
I know that this does not seem the most obvious and easy way to do it, but i simply cannot get a file to download myself using fsockopen. I specifically want this function, as I need to POST headers to the server and I haven't as yet been able to download a file using it, without it being corrupt, or the connection hanging. I just can't figure it out, and I'm getting a bit tired with it!
I don't need a whole hand-made script, I just need the part where fsockopen will download this file. Perhaps a working function that would do it. Please try not to use classes or objects because I haven't quite figured out object-oriented programming yet!!

Also, I would like if you can do it via HTTP 1.0 because I know HTTP 1.1 is tricky, and might require a chunk decoder.

Thanks to anyone who can help. :)

Re: Reponse headers not trimming right - or other problem? :(

Posted: Sun Feb 03, 2008 11:44 am
by Mordred
When you get the HTTP response from the remote server, you need to trim the HTTP headers before sending them to the client. Now you're sending your headers PLUS whatever the remote server gave you, that's why you see the remote headers in the "content".

Re: How to download a file (with browser) using fsockopen() ?

Posted: Mon Feb 04, 2008 8:19 pm
by szalinski
I just need help doing the fsockopen bit, i just can't figure out how to get a remote file to download in my browser using this function and HTTP 1.0. :(