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: Jo Anonymous <jojoanon_at_gmail.com>
Date: Thu, 6 Nov 2008 08:34:42 +1300

Cheers for your replies..

Have tried both methods with no luck.

Thorben, tried your method but it also didn't work for me, the results said:

there was no no filename in the headers... Failed writing body

The problem I think is that the actual file itself does not exist on the
server. It is just generated, so bascially all I need is a way to bypass the
download prompt and have it somehow let me download without any user
interaction.

Will give it another go, any other suggestions are greatly appreciated.

Cheers

On Wed, Nov 5, 2008 at 12:01 AM, Thorben Thuermer <
t.thuermer_at_mediaclipping.de> wrote:

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

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2008-11-05