File Sharing API for PubNub PHP SDK

Allows users to upload and share files. You can upload any file of up to 5 MB in size. This feature is commonly used in social apps to share images, or in medical apps to share medical records for patients.

When a file is uploaded on a channel, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel receive a file event which contains a file ID, filename, and optional description.

Send file

Upload file / data to specified channel.

Method(s)

$pubnub->sendFile()
->channel(string)
->fileName(string)
->message(string|array)
->shouldStore(Boolean)
->shouldCompress(Boolean)
->ttl(Int)
->fileHandle(Resource)
->fileContent(bytes|File)
->meta(string|array)
->customMessageType(string)
->sync();
ParameterTypeRequiredDefaultDescription
channelstringYesChannel for the file.
fileNamestringYesName of the file to send.
messagestring or arrayOptionalMessage which should be sent along with file to specified channel.
shouldStoreBooleanOptionalTrueWhether PubNub published file message should be stored in channel history.
shouldCompressBooleanOptionalTrueWhether the request payload should be compressed.
ttlIntegerOptionalHow long message should be stored in channel's storage.
fileHandleResourceYesPointer to a resource to be read and placed in the buffer.
fileContentbytes or PHP file objectYesInput stream with file content.
metastring or arrayOptionalMeta data object which can be used with the filtering ability.
customMessageTypestringOptionalA case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn-.

Examples: text, action, poll.

Basic Usage


Returns

The sendFile() operation returns an PNSendFileResult which contains the following fields:

PNSendFileResult

Property NameTypeDescription
namestringName of the uploaded file.
fileIdstringID of the uploaded file.

List channel files

Retrieve list of files uploaded to Channel.

Method(s)

$pubnub->listFiles()
->channel(string)
->sync();
ParameterTypeRequiredDefaultDescription
channelstringYesn/aChannel to get the list of files.

Basic Usage


Returns

The listFiles() operation returns an PNGetFilesResult which contains the following fields:

PNGetFilesResult

Property NameTypeDescription
nextstringRandom string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
prevstringRandom string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data.
countIntNumber of files returned.
dataArrayArray of PNGetFilesItem.

PNGetFilesItem contains the following properties:

Property NameTypeDescription
idstringId of the uploaded file.
namestringName of the upload file.
sizestringSize of the uploaded file.
creationTimestringTime of creation.

Get File Url

Generate URL which can be used to download file from target Channel.

Method(s)

$pubnub.getFileDownloadUrl()
->channel(string)
->fileId(string)
->fileName(string)
->sync()
ParameterTypeRequiredDescription
channelstringYesName of channel to which the file has been uploaded.
fileNamestringYesName under which the uploaded file is stored.
fileIdstringYesUnique identifier for the file, assigned during upload.

Basic Usage


Returns

The getFileDownloadUrl() operation returns an PNGetFileDownloadURLResult which contains the following fields:

PNGetFileDownloadURLResult

Property NameTypeDescription
fileUrlstringURL to be used to download the requested file.

Download file

Download file from specified Channel.

Method(s)

$pubnub.downloadFile()
->channel(string)
->fileId(string)
->fileName(string)
->sync()
ParameterTypeRequiredDescription
channelstringYesName of channel to which the file has been uploaded.
fileNamestringYesName under which the uploaded file is stored.
fileIdstringYesUnique identifier for the file, assigned during upload.

Basic Usage


Returns

The downloadFile() operation returns an PNDownloadFileResult which contains the following fields:

PNDownloadFileResult

Property NameTypeDescription
fileContentbytesThe file that was uploaded.

Delete file

Delete file from specified Channel.

Method(s)

$pubnub.deleteFile()
->channel(string)
->fileId(string)
->fileName(string)
->sync()
ParameterTypeRequiredDescription
channelstringYesThe channel from which to delete the file.
fileIdstringYesUnique identifier of the file to be deleted.
fileNamestringYesName of the file to be deleted.

Basic Usage


Returns

The deleteFile() operation returns an PNDeleteFileResult which contains the following fields:

Property NameTypeDescription
statusIntReturns a status code.

Publish file message

Publish file message from specified Channel.

Method(s)

$pubnub.publishFileMessage()
->channel(string)
->fileId(string)
->fileName(string)
->message(string|array)
->meta(string|array)
->shouldStore(Boolean)
->ttl(Int)
->customMessageType(string)
->sync();
ParameterTypeRequiredDefaultDescription
channelStringYesn/aName of channel to publish file message.
file_idStringYesn/aUnique identifier of the file.
file_nameStringYesn/aName of the file.
messageDictionaryOptionaln/aThe payload.
metaDictionaryOptionaln/aMeta data object which can be used with the filtering ability.
should_storeBooleanOptionalTrueSet to False to not store this message in history. By default, messages are stored according to the retention policy you set on your key.
ttlIntOptional0How long the message should be stored in the channel's history. If not specified, defaults to the key set's retention value.
customMessageTypestringOptionalA case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn-.

Examples: text, action, poll.

Basic Usage

pubnub.publishFileMessage()
->message("Hey, this is the requested file.")
->channel("channel_1")
->fileId("p1n4ppl3p1zz4")
->fileName("pinapplePizza.jpg")
->customMessageType("file-message")
->sync();

Returns

The publish_file_message() operation returns an PNPublishFileMessageResult which contains the following fields:

PNPublishFileMessageResult

The publishFileMessage() operation returns a PNPublishFileMessageResult which contains the following property:

Property NameTypeDescription
timestampstringThe timetoken when the message was published.
Last updated on