views:

42

answers:

2

Hi,

Scenario : I have a c# .net web page. I want the user to be able to download a file placed on a remote server from a link on my page. However while downloading there should be minimum load on my server. Hence i tried creating a HttpWebRequest instance, passed the download.php path

e.g. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://servername/download.php");

myHttpWebRequest.Headers.Add ("Content-disposition", "attachment;filename=XXX.pdf"); myHttpWebRequest.ContentType = "application/pdf";

Passed the httprequest object in the session; however while reading the httpwebresponse on another page the contenttype is reset to "text/html".

Also the php file readers the headers and uses a readfile command to download the file. It gives the following error. Warning: readfile() [function.readfile]: URL file-access is disabled in the server configuration in

Is there a better way to do the same . Need help urgently .

Thanks in advance.

+2  A: 
Pekka
Note that you also won't be able to use `file_get_contents`
NullUserException
Thanks for that info. I have tried altering the scripts. I have added the following in my c# code,i read these headers in the php file and set the headers accordingly. IE responds and loads the file however Chrome and Firefox display binary data. Thanks for that info. I have tried altering the scripts. I have added the following in my c# code myHttpWebRequest.Headers.Add("DESC", "File Transfer"); myHttpWebRequest.Headers.Add("DISP", "attachment; filename=cards.jpg"); myHttpWebRequest.Headers.Add("CONT", "image/jpeg"); myHttpWebRequest.Headers.Add("ENCODE", "binary");
Kushal
A: 

You can get around allow_url_fopen restrictions by using fsockopen. Here's a (rudimentary) implementation:

function fsock_get_contents($url) {
    $fp = fsockopen($url, 80, $errno, $errstr, 20);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
        return false;
    } else {
        $out = "GET / HTTP/1.1\r\n";
        $out .= "Host: " . parse_url($url, PHP_URL_HOST) . "\r\n";
        $out .= "Connection: Close\r\n\r\n";

        $contents = '';
        fwrite($fp, $out);
        while (!feof($fp)) {
            $contents .= fgets($fp, 128);
        } fclose($fp);
        return $contents;
    }
}

echo fsock_get_contents('www.google.com');
NullUserException