Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/88052-send-headers/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-450472
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-451728
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/88052-send-headers/#findComment-452909
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.