Thanks for this, it seems to work generally ok, but with one problem.

If i get prompted to download the fie, there is no filesize (content-length is missing), so i wouldn't be able to resume the file if it was big!

I tried adding another function that gets the headers from the file first, (to find the content-length), but instead I get an error message stating 'Cannot modify header information - headers already sent...', so I'm thinking that i am now in a catch-22 situation. I need to get the headers to find the content-length, and then send it to the browser to prompt the download of the file with the correct filesize.

Also i noticed u used http 1.1, which i simply changed to 1.0 - hope this is enough to avoid chunked decoding. I also changed the method to POST as i need it.

so either is it a paradox, or am i just misunderstanding where the headers should be placed?

thanks :)

On Tue, 05 Feb 2008 07:16:31 -0000, <[EMAIL PROTECTED]> wrote:


 Try this, it should work.


//Get the file from the remote location
function getFile($host, $resource, $port)
{
??? $hdr = '';
??? $file_cont = '';
??? $fh = fsockopen($host, $port, $errno, $errstr, 300);
???
??? if(! $fh)
??? {
??? ??? return "error";
??? } else {

??? ??? $hdr .= "GET /$resource HTTP/1.1 \r\n";
??? ??? $hdr .= "Host: $host\r\n";
??? ??? $hdr .= "Connection: close\r\n\r\n";

??? ??? fwrite($fh, $hdr);

??? ??? while(!feof($fh))
??? ??? {
??? ??? ??? $file_cont .= fgets($fh, 128);
??? ??? }

??? ??? //Return the file as a string
??? ??? return $file_cont;
??? }
}

//Set up essential headers
header("Content-Type: Application/GIF");
header("Content-Disposition: application/gif; filename=one.gif");

//Strip the text headers in the file and print it out.
print preg_replace("/^.*\r\n/m", "", getFile("wisdomleaf.com", "images/logo.gif", 80));



Anyway, the core of the script is download the file as a string, print it out as a string with suitable headers.


Cheers,
V






-----Original Message-----
From: szalinski <[EMAIL PROTECTED]>
To: php-general@lists.php.net; php-general@lists.php.net <php-general@lists.php.net>
Sent: Tue, 5 Feb 2008 8:58 am
Subject: [PHP] How to download a file (with browser) using fsockopen() ?









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:?
?

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?
?

http://localhost/index.php?url=http://remotedomain.com/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, and i don't see the need for it, unless someone is able to provide a working chunked data decoder.?
?

Thanks to anyone who can help. :)?
?

--PHP General Mailing List (http://www.php.net/)?

To unsubscribe, visit: http://www.php.net/unsub.php?





________________________________________________________________________
You are invited to Get a Free AOL Email ID. - http://webmail.aol.in

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to