note 87342 added to function.curl-setopt
| From: | fnjordy at gmail dot com | Date: | Mon, 01 Dec 2008 07:48:25 +0000 |
| Subject: | note 87342 added to function.curl-setopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-147475@lists.php.net to get a copy of this message | ||
There is no CURLOPT_MAXFILESIZE in the PHP module but it's function only works with
Content-Length headers anyway. There are two ways of checking download sizes, one is after the
download is complete using filesize(), the other is as the download is running allowing you to
terminate before wasting time and disk space.
<?
$GLOBALS['file_size'] = 0;
$GLOBALS['max_file_size'] = 1024 * 1024;
function on_curl_header($ch, $header)
{
$trimmed = rtrim($header);
if (preg_match('/^Content-Length: (\d+)$/', $trimmed, $matches))
{
$file_size = $matches[1];
if ($file_size > $GLOBALS['max_file_size']) {
// handle error here.
}
}
return strlen($header);
}
function on_curl_write($ch, $data)
{
$bytes = strlen($data);
$GLOBALS['file_size'] += $bytes;
if ($GLOBALS['file_size'] > $GLOBALS['max_file_size']) {
// handle error here.
}
return $bytes;
}
$ch = curl_init();
$options = array(CURLOPT_URL => 'http://www.php.net/',
CURLOPT_HEADER => false,
CURLOPT_HEADERFUNCTION => 'on_curl_header',
CURLOPT_WRITEFUNCTION => 'on_curl_write');
curl_setopt_array($ch, $options);
curl_exec($ch);
// ...
?>
----
Server IP: 203.169.164.81
Probable Submitter: 220.232.210.142
----
Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php
Edit -- https://master.php.net/note/edit/87342
Del: integrated -- https://master.php.net/note/delete/87342/integrated
Del: useless -- https://master.php.net/note/delete/87342/useless
Del: bad code -- https://master.php.net/note/delete/87342/bad+code
Del: spam -- https://master.php.net/note/delete/87342/spam
Del: non-english -- https://master.php.net/note/delete/87342/non-english
Del: in docs -- https://master.php.net/note/delete/87342/in+docs
Del: other reasons-- https://master.php.net/note/delete/87342
Reject -- https://master.php.net/note/reject/87342
Search -- https://master.php.net/manage/user-notes.php