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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
szalinski
Forum Newbie
Posts: 14
Joined: Sat Oct 20, 2007 12:58 pm

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

Post 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. :)
Last edited by szalinski on Mon Feb 04, 2008 8:16 pm, edited 1 time in total.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

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

Post 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".
szalinski
Forum Newbie
Posts: 14
Joined: Sat Oct 20, 2007 12:58 pm

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

Post 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. :(
Post Reply