note 87342 modified in function.curl-setopt by thiago

From: Date: Mon, 01 Dec 2008 11:59:01 +0000
Subject: note 87342 modified in function.curl-setopt by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147480@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. <?php $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); // ... ?> --was-- 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); // ... ?> http://php.net/manual/en/function.curl-setopt.php

« previous php.notes (#147480) next »