eagle00789 Posted January 27, 2008 Share Posted January 27, 2008 i'm trying to send the following headers to get a file download active. Content-Type: application/octet-stream Content-Disposition: attachment; filename=<filename> GET <location of file on server including file name> HTTP/1.1 Host: <server> User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; MEGAUPLOAD 2.0; Alexa Toolbar) Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-en,en;q=0.5 Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7 Pragma: no-cache Cache-Control: no-chache Authorization: Basic <password> i'm trying to do that with the following code: $handle=fopen("files/".$_POST['FileName'], 'r'); while (!feof($handle)) { $data .= fgets($handle); } $data = str_replace("\r\n", "", $data); header($data); this works, except the download doesn't start. where have i gone wrong? Quote Link to comment https://forums.phpfreaks.com/topic/88052-send-headers/ Share on other sites More sharing options...
Bauer418 Posted January 27, 2008 Share Posted January 27, 2008 You have request and response headers there. Your server shouldn't be sending a GET header, or a User-Agent header, or any of the Accept headers. Nor should it be sending an Authorization: line with a password. With that said, your client shouldn't be sending, Content-type, Content-disposition, Pragma or Cache-Control to the server. What exactly are these headers for? Quote Link to comment https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-450472 Share on other sites More sharing options...
eagle00789 Posted January 28, 2008 Author Share Posted January 28, 2008 i'm trying to start a download from a remote server. that server requires authentication. i found the method i currently use on another forum (archive of that forum to be exactly, as the forum itself doesn't exists anymore) Quote Link to comment https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-451321 Share on other sites More sharing options...
Bauer418 Posted January 28, 2008 Share Posted January 28, 2008 If you are trying to just get a file from a remote server, try something such as this: $socket = fsockopen('remoteserver.com', 80, $errno, $errstr, 15); if (!$socket) { print "Socket Error [#" . $errno . "]: " . $errstr; } else { $out = "GET /filename.zip HTTP/1.1\r\n"; $out = "Host: remoteserver.com\r\n"; $out = "Connection: close\r\n\r\n"; fwrite($socket, $out); $file = ''; while (!feof($socket)) { $file .= fgets($socket, 128); } fclose($socket); // At this point, $file now contains the file contents } Quote Link to comment https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-451728 Share on other sites More sharing options...
eagle00789 Posted January 28, 2008 Author Share Posted January 28, 2008 and how do i send the $file to the client without saving the file first to the server?? Quote Link to comment https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-451749 Share on other sites More sharing options...
eagle00789 Posted January 28, 2008 Author Share Posted January 28, 2008 and what about my authorization on that server?? Quote Link to comment https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-451754 Share on other sites More sharing options...
Bauer418 Posted January 29, 2008 Share Posted January 29, 2008 Sorry, forgot about those things, here you go: $socket = fsockopen('remoteserver.com', 80, $errno, $errstr, 15); if (!$socket) { print "Socket Error [#" . $errno . "]: " . $errstr; } else { $out = "GET /filename.zip HTTP/1.1\r\n"; $out .= "Host: remoteserver.com\r\n"; $out .= "Authorization: BASIC " . base64_encode('username:password') . "\r\n"; $out .= "Connection: close\r\n\r\n"; fwrite($socket, $out); $file = ''; while (!feof($socket)) { $file .= fgets($socket, 128); } fclose($socket); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="filename.zip"'); print $file; } Quote Link to comment https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-452909 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.