From 73ccfcb964771f4bd540838c32791a1d49e29d5f Mon Sep 17 00:00:00 2001 From: chris Date: Sun, 11 Sep 2005 16:21:26 +0200 Subject: io_download mod to allow use of attachment names, plugin manager fix to use new io_download darcs-hash:20050911142126-50fdc-dd1b45c40b33153c3ea993ec53c11d063c34154c.gz --- inc/io.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'inc/io.php') diff --git a/inc/io.php b/inc/io.php index c25bd8975..1939dc6a0 100644 --- a/inc/io.php +++ b/inc/io.php @@ -259,22 +259,52 @@ function io_mkdir_ftp($dir){ } /** - * downloads a file from the net and saves it to the given location + * downloads a file from the net and saves it + * + * if $useAttachment is false, + * - $file is the full filename to save the file, incl. path + * - if successful will return true, false otherwise + + * if $useAttachment is true, + * - $file is the directory where the file should be saved + * - if successful will return the name used for the saved file, false otherwise * * @author Andreas Gohr + * @author Chris Smith */ -function io_download($url,$file){ +function io_download($url,$file,$useAttachment=false,$defaultName=''){ $http = new DokuHTTPClient(); $http->max_bodysize = 2*1024*1024; //max. 2MB $http->timeout = 25; //max. 25 sec $data = $http->get($url); if(!$data) return false; + + if ($useAttachment) { + $name = ''; + if (isset($http->resp_headers['content-disposition'])) { + $content_disposition = $http->resp_headers['content-disposition']; + if (is_string($content_disposition) && + preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match=array())) { + + $name = basename($match[1]); + } + + } + + if (!$name) { + if (!$defaultName) return false; + $name = $defaultName; + } + + $file = $file.$name; + } $fp = @fopen($file,"w"); if(!$fp) return false; fwrite($fp,$data); fclose($fp); + if ($useAttachment) return $name; return true; } -- cgit v1.2.3