Download the PHP package robtimus/multipart without Composer
On this page you can find all versions of the php package robtimus/multipart. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download robtimus/multipart
More information about robtimus/multipart
Files in robtimus/multipart
Package multipart
Short Description A library to support creating (streaming) multiparts
License Apache-2.0
Homepage https://github.com/robtimus/php-multipart
Informations about the package multipart
Multipart
A library to support creating (streaming) multiparts.
Supported multipart types
multipart/form-data
To create a multipart/form-data object, create a MultipartFormData instance, add the form fields, and call finish(). There are two methods for adding form fields:
addValue($name, $value, $contentType = '', $contentTransferEncoding = '')adds a string value with the given name.$nameand$valueare required.$contentTypeis optional, and will be used for theContent-Typeheader if it is set. It should be set if the implicit content type should not betext/plain.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
addFile($name, $filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a file with the given name.$name,$filename,$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
Multiple values or files with the same parameter name
MultipartFormData follows RFC 7578, and not RFC 2388. This means that multiple values or files with the same parameter name are not sent with a multipart/mixed field but instead as separate parts.
PHP servers require multiple values or files to be sent with a name that ends with []. Because MultipartFormData is written to support also other server types that do not have this requirement, it is up to the caller to add these. For instance:
multipart/related
To create a multipart/related object, create a MultipartRelated instance, add the root part and any inline files, and call finish(). There are two methods for adding parts:
addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part without a content disposition. This should be used for the root part.$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
addInlineFile($contentID, $filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds an inline file.$contentID,$filename,$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
To use this inline file in the HTML body, use cid:logo as the source of an image.
multipart/alternative
To create a multipart/alternative object, create a MultipartAlternative instance, add the alternatives, and call finish(). There are two methods for adding alternatives:
addMultipart(Multipart $multipart)adds another multipart as alternative. This is most often used with a multipart/related object.addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part with the given content.$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
multipart/mixed
To create a multipart/mixed object, create a MultipartMixed instance, add the parts, and call finish(). There are three methods for adding parts:
addMultipart(Multipart $multipart)adds another multipart. This is most often used with a multipart/alternative or multipart/related object.addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part with the given content. This can be used for the bodies of plain text emails.$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
addAttachment($filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')adds a part with content dispositionattachment.$filename,$contentand$contentTypeare required.$contentLengthis optional, and will be ignored if the content is a string.contentTransferEncodingis optional, and will be used for theContent-Transfer-Encodingheader if it is set.
An example:
Multipart content
The content of a part or file can be given in one of three ways:
- As a string. The content length will be ignored.
- As a resource that can be read using
fread. It is up to the caller to close this resource. - As a callable that takes a length, and returns a string that is not larger than the given length. If there is nothing more to read it should return the empty string.
Examples, using a MultipartFormData object:
cURL support
To send a multipart object with a cURL request, you need to follow some steps:
- Set the request type using
CURLOPT_CUSTOMREQUEST. - Set the
CURLOPT_UPLOADoption totrue. - Set the object's
curlReadmethod as theCURLOPT_READFUNCTION. - Make sure the
Content-TypeandContent-Lengthheaders are set. Note that theContent-Lengthheader is optional.
For instance:
Non-streaming support
If streaming is not possible (e.g. because a string is required, like in the mail function), you can buffer a multipart object in-memory by calling the buffer method. This method takes an optional buffer size, and returns the buffered contents. The content length will be set accordingly. Note that you should do this before calling read (or curlRead), otherwise the buffered contents may not contain all desired contents (especially if you're using resources or callables).
Multipart.__toString() has been overridden to buffer the multipart object as well, so you can achieve the same by casting a multipart object to string. The difference is that buffer requires the multipart object to be finished.