$file
$file : string
Path to file for download
Send HTTP Downloads/Responses.
With this package you can handle (hidden) downloads. It supports partial downloads, resuming and sending raw data ie. from database BLOBs.
ATTENTION: You shouldn't use this package together with ob_gzhandler or zlib.output_compression enabled in your php.ini, especially if you want to send already gzipped data!
__construct(array $params = array())
Constructor
Set supplied parameters.
array | $params | associative array of parameters one of:
and any of:
'Content-Disposition' is not HTTP compliant, but most browsers follow this header, so it was borrowed from MIME standard. It looks like this: "Content-Disposition: attachment; filename=example.tgz". |
setFile(string $file, boolean $send_error = true) : mixed
Set path to file for download
The Last-Modified header will be set to files filemtime(), actually. Returns PEAR_Error (HTTP_DOWNLOAD2_E_INVALID_FILE) if file doesn't exist. Sends HTTP 404 or 403 status if $send_error is set to true.
string | $file | path to file for download |
boolean | $send_error | whether to send HTTP/404 or 403 if the file wasn't found or is not readable |
Returns true on success or PEAR_Error on failure.
setResource(integer $handle = null) : mixed
Set resource for download
The resource handle supplied will be closed after sending the download. Returns a PEAR_Error (HTTP_DOWNLOAD2_E_INVALID_RESOURCE) if $handle is no valid resource. Set $handle to null if you want to unset this.
integer | $handle | resource handle |
Returns true on success or PEAR_Error on failure.
setCache(boolean $cache = true) : void
Whether to allow caching
If set to true (default) we'll send some headers that are commonly used for caching purposes like ETag, Cache-Control and Last-Modified.
If caching is disabled, we'll send the download no matter if it would actually be cached at the client side.
boolean | $cache | whether to allow caching |
setCacheControl(string $cache = 'public', integer $maxage) : boolean
Whether to allow proxies to cache
If set to 'private' proxies shouldn't cache the response. This setting defaults to 'public' and affects only cached responses.
string | $cache | private or public |
integer | $maxage | maximum age of the client cache entry |
setBufferSize(integer $bytes = 2097152) : mixed
Set Size of Buffer
The amount of bytes specified as buffer size is the maximum amount of data read at once from resources or files. The default size is 2M (2097152 bytes). Be aware that if you enable gzip compression and you set a very low buffer size that the actual file size may grow due to added gzip headers for each sent chunk of the specified size.
Returns PEAR_Error (HTTP_DOWNLOAD2_E_INVALID_PARAM) if $size is not greater than 0 bytes.
integer | $bytes | Amount of bytes to use as buffer. |
Returns true on success or PEAR_Error on failure.
setThrottleDelay(float $seconds) : void
Set Throttle Delay
Set the amount of seconds to sleep after each chunck that has been sent. One can implement some sort of throttle through adjusting the buffer size and the throttle delay. With the following settings Download2 will sleep a second after each 25 K of data sent.
Array(
'throttledelay' => 1,
'buffersize' => 1024 * 25,
)
Just be aware that if gzipp'ing is enabled, decreasing the chunk size too much leads to proportionally increased network traffic due to added gzip header and bottom bytes around each chunk.
float | $seconds | Amount of seconds to sleep after each chunk that has been sent. |
setLastModified(integer $last_modified) : void
Set "Last-Modified"
This is usually determined by filemtime() in Download2::setFile() If you set raw data for download with Download2::setData() and you want do send an appropiate "Last-Modified" header, you should call this method.
integer | $last_modified | unix timestamp |
setContentDisposition(string $disposition = HTTP_DOWNLOAD2_ATTACHMENT, string $file_name = null) : void
Set Content-Disposition header
Example:
$Download2->setContentDisposition(
HTTP_DOWNLOAD2_ATTACHMENT,
'download.tgz'
);
string | $disposition | whether to send the download inline or as attachment |
string | $file_name | the filename to display in the browser's download window |
setContentType(string $content_type = 'application/x-octetstream') : mixed
Set content type of the download
Default content type of the download will be 'application/x-octetstream'. Returns PEAR_Error (HTTP_DOWNLOAD2_E_INVALID_CONTENT_TYPE) if $content_type doesn't seem to be valid.
string | $content_type | content type of file for download |
Returns true on success or PEAR_Error on failure.
guessContentType() : mixed
Guess content type of file
First we try to use PEAR::MIME_Type, if installed, to detect the content type, else we check if ext/mime_magic is loaded and properly configured.
Returns PEAR_Error if: o if PEAR::MIME_Type failed to detect a proper content type (HTTP_DOWNLOAD2_E_INVALID_CONTENT_TYPE) o ext/magic.mime is not installed, or not properly configured (HTTP_DOWNLOAD2_E_NO_EXT_MMAGIC) o mime_content_type() couldn't guess content type or returned a content type considered to be bogus by setContentType() (HTTP_DOWNLOAD2_E_INVALID_CONTENT_TYPE)
Returns true on success or PEAR_Error on failure.
send(boolean $autoSetContentDisposition = true) : mixed
Send
Returns PEAR_Error if: o HTTP headers were already sent (HTTP_DOWNLOAD2_E_HEADERS_SENT) o HTTP Range was invalid (HTTP_DOWNLOAD2_E_INVALID_REQUEST)
boolean | $autoSetContentDisposition | Whether to set the Content-Disposition header if it isn't already. |
Returns true on success or PEAR_Error on failure.
sendArchive(string $name, mixed $files, string $type = HTTP_DOWNLOAD2_TGZ, string $add_path = '', string $strip_path = '') : mixed
Send a bunch of files or directories as an archive
Example:
require_once 'HTTP/Download2.php';
Download2::sendArchive(
'myArchive.tgz',
'/var/ftp/pub/mike',
HTTP_DOWNLOAD2_TGZ,
'',
'/var/ftp/pub'
);
string | $name | name the sent archive should have |
mixed | $files | files/directories |
string | $type | archive type |
string | $add_path | path that should be prepended to the files |
string | $strip_path | path that should be stripped from the files |
Returns true on success or PEAR_Error on failure.
None found |
sendChunks(array $chunks) : mixed
Send multiple chunks
array | $chunks | Chunks to send |
Returns true on success or PEAR_Error on failure.
None found |
sendChunk(array $chunk, string $cType = null, string $bound = null) : mixed
Send chunk of data
array | $chunk | start and end offset of the chunk to send |
string | $cType | actual content type |
string | $bound | boundary for multipart/byteranges |
Returns true on success or PEAR_Error on failure.
None found |
getChunks() : array
Get chunks to send
Chunk list or PEAR_Error on invalid range request
None found |
sortChunks( $chunks) : void
Sorts the ranges to be in ascending order
$chunks |
static | |
---|---|
author |
Philippe Jausions jausions@php.net |
mergeChunks(array $chunks) : array
Merges consecutive chunks to avoid overlaps
array | $chunks | Ranges to merge |
merged ranges
static | |
---|---|
author |
Philippe Jausions jausions@php.net |
None found |
None found |
None found |
None found |
compareAsterisk(string $svar, string $compare) : boolean
Compare against an asterisk or check for equality
string | $svar | key for the $_SERVER array |
string | $compare | string to compare |
None found |
None found |
None found |
None found |