cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Re: How to download a file that has no extension in url? or bypass download prompt?

From: Thorben Thuermer <t.thuermer_at_mediaclipping.de>
Date: Tue, 4 Nov 2008 12:01:36 +0100

this is actually scarily related to the unanswered question i signed up to this
list for...

proper solution without downloading the file twice:
(but also using global variables)

<?
// these should really be stored inside the curl context
global $filename;
global $outfile;

function header_cb($handle,$h){
    global $filename;
    if(preg_match("/^Content-Disposition: .*filename\=([^\\n|\\r]+)[\\n\\r]/", $h, $m)){
        $filename=$m[1];
        echo "got filename: ".$filename."\n";
    }
    return strlen($h);
}

function data_cb($handle,$d){
    global $filename;
    global $outfile;
    if (!$outfile){
        $status=curl_getinfo($handle,CURLINFO_HTTP_CODE);
        if ($status != 200){
            echo "bad http status\n";
            return -1;
        }
        if ($filename == null){
            echo "there was no no filename in the headers...\n";
            return -1;
        }
        $outfile=fopen($filename,"w");
    }
    return fwrite($outfile,$d);
};

// testcase urls found via google:
$handle=curl_init("http://blog.ninedays.org/demo/direct-to-download-images-and-pdfs/download.image.php");
//$handle=curl_init("http://blog.ninedays.org/demo/direct-to-download-images-and-pdfs/original.jpg");

curl_setopt($handle,CURLOPT_WRITEFUNCTION,"data_cb");
curl_setopt($handle,CURLOPT_HEADERFUNCTION,"header_cb");
$filename=null;
if (!curl_exec($handle)){
    echo curl_error($handle)."\n";
}
if ($outfile) fclose($outfile);
curl_close($handle);

?>

- Thorben

On Mon, 3 Nov 2008 17:44:01 -0500
"Stephen Pynenburg" <spynenburg_at_gmail.com> wrote:
> There's probably a way to do it in one request, but I think you could just
> as easily do this:
> <?PHP
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_URL, "site/page.php");
> curl_setopt($ch, CURLOPT_HEADER, 1);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
> $res = curl_exec($ch);
> preg_match("/filename\=(.*)\\n/", $res, $matches);
> echo $matches[1]; //the filename
> ?>
>
> Then do another request with the filename gleaned from that request.
>
> -Stephen
>
> 2008/11/3 Jo Anonymous <jojoanon_at_gmail.com>
>
> > Hello,
> >
> > I am trying to download a csv file which is generated from the url.
> >
> >
> >
> > I can login to the website using curl succesfully but I can't download the
> > file I want.
> >
> > The download url looks something like this:
> >
> > http://www.websitename.com/file.aspx?type=all&download=1
> >
> >
> >
> >
> > The website has this line in the header:
> >
> > header("Content-Disposition: attachment; filename=dada.csv");
> >
> > which brings up a download prompt.
> >
> >
> >
> >
> > I just want to download the csv file to my server without any prompts.
> >
> >
> >
> >
> > I've been trying for ages to figure this out, there must be a way.
> >
> > Many thanks in advance.
> >
> > _______________________________________________
> > http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
> >
> >
>
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2008-11-04