Documentation

File extends AbstractBaseApi
in package

File Class

Represents a file in the Canvas LMS. This class provides methods to upload, find, and fetch files from the Canvas LMS system. It supports Canvas's 3-step file upload process and multiple upload contexts.

Usage:

// Upload a file to a course
$fileData = [
    'name' => 'document.pdf',
    'size' => 1024000,
    'content_type' => 'application/pdf',
    'parent_folder_id' => 123,
    'file' => '/path/to/document.pdf'
];
$file = File::uploadToCourse(456, $fileData);

// Upload via URL
$urlData = [
    'name' => 'image.jpg',
    'url' => 'https://example.com/image.jpg'
];
$file = File::uploadUrlToCourse(456, $urlData);

// Find a file by ID
$file = File::find(789);

// Get file download URL
$downloadUrl = $file->getDownloadUrl();

// Fetch all files from current user (first page only)
$files = File::get();

// Get paginated files
$paginationResult = File::paginate(['per_page' => 10]);
$files = $paginationResult->getData();
$hasNext = $paginationResult->hasNext();

// Fetch all files from all pages
$allFiles = File::all(['per_page' => 50]);

Table of Contents

Properties

$contentType  : string|null
The content-type of the file
$createdAt  : string|null
The datetime the file was created
$displayName  : string|null
The display name of the file
$filename  : string|null
The filename of the file
$folderId  : int|null
The folder_id of the folder containing the file
$hidden  : bool
Whether the file is hidden
$id  : int|null
The unique identifier for the file
$lockAt  : string|null
The datetime the file was locked at
$locked  : bool
Whether the file is locked
$lockedForUser  : bool
Whether the file is locked for the user
$lockExplanation  : string|null
Explanation of why the file is locked
$previewUrl  : string|null
A URL to the file preview
$size  : int|null
The file size in bytes
$thumbnailUrl  : string|null
An abbreviated URL to the file that can be inserted into the rich content editor
$unlockAt  : string|null
The datetime the file will be deleted (not present for file upload requests)
$updatedAt  : string|null
The datetime the file was last updated
$url  : string|null
The URL to download the file (not present for file upload requests)
$uuid  : string|null
The UUID of the file
$apiClient  : HttpClientInterface
$contextId  : int|null
Context ID where the file belongs
$contextType  : string|null
Context type where the file belongs
$methodAliases  : array<string|int, mixed>
Define method aliases

Methods

__callStatic()  : mixed
Magic method to handle function aliases
__construct()  : mixed
BaseApi constructor.
all()  : array<string|int, static>
Get all files from current user's personal files.
delete()  : self
Delete the file
fetchByContext()  : array<string|int, self>
Fetch files from a specific context
fetchCourseFiles()  : array<string|int, File>
Fetch files for a course
fetchGroupFiles()  : array<string|int, File>
Fetch files for a group
fetchUserFiles()  : array<string|int, File>
Fetch files for a user
find()  : self
Find a file by ID
folder()  : null
Get the folder containing this file
get()  : array<string|int, static>
Get first page of files from current user's personal files.
getContentType()  : string
Get the content type
getContextId()  : int|null
Get the context ID
getContextType()  : string|null
Get the context type
getCreatedAt()  : string
Get the created at timestamp
getDisplayName()  : string
Get the display name
getDownloadUrl()  : string
Get file download URL
getFilename()  : string
Get the filename
getFolderId()  : int
Get the folder ID
getId()  : int
Get the file ID
getSize()  : int
Get the file size
getUpdatedAt()  : string
Get the updated at timestamp
getUrl()  : string|null
Get the file URL
getUuid()  : string
Get the file UUID
isHidden()  : bool
Check if the file is hidden
isLocked()  : bool
Check if the file is locked
paginate()  : PaginationResult
Get paginated files from current user's personal files.
setApiClient()  : void
Set the API client
setContentType()  : void
Set the content type
setContextId()  : void
Set the context ID
setContextType()  : void
Set the context type
setCreatedAt()  : void
Set the created at timestamp
setDisplayName()  : void
Set the display name
setFilename()  : void
Set the filename
setFolderId()  : void
Set the folder ID
setHidden()  : void
Set the hidden status
setId()  : void
Set the file ID
setLocked()  : void
Set the locked status
setSize()  : void
Set the file size
setUpdatedAt()  : void
Set the updated at timestamp
setUrl()  : void
Set the file URL
setUuid()  : void
Set the file UUID
uploadToAssignmentSubmission()  : self
Upload a file for an assignment submission
uploadToContext()  : self
Upload a file to a specific context
uploadToCourse()  : self
Upload a file to a course
uploadToGroup()  : self
Upload a file to a group
uploadToUser()  : self
Upload a file to a user
castValue()  : DateTime|mixed
Cast a value to the correct type
checkApiClient()  : void
Check if the API client is set, if not, instantiate a new one
convertPaginatedResponseToModels()  : array<string|int, static>
Helper method to convert paginated response data to model instances
createConfiguredHttpClient()  : HttpClient
Create an HttpClient with configured middleware
createPaginationResult()  : PaginationResult
Helper method to create PaginationResult from paginated response
getEndpoint()  : string
Get the endpoint for this resource.
getPaginatedResponse()  : PaginatedResponse
Helper method to get paginated response from API endpoint
populate()  : void
Populate the object with new data
toDtoArray()  : array<string|int, mixed>
Convert the object to an array
performUpload()  : self
Perform the 3-step Canvas file upload process

Properties

$contentType

The content-type of the file

public string|null $contentType = null

$createdAt

The datetime the file was created

public string|null $createdAt = null

$displayName

The display name of the file

public string|null $displayName = null

$filename

The filename of the file

public string|null $filename = null

$folderId

The folder_id of the folder containing the file

public int|null $folderId = null

$hidden

Whether the file is hidden

public bool $hidden = false

$id

The unique identifier for the file

public int|null $id = null

$lockAt

The datetime the file was locked at

public string|null $lockAt = null

$locked

Whether the file is locked

public bool $locked = false

$lockedForUser

Whether the file is locked for the user

public bool $lockedForUser = false

$lockExplanation

Explanation of why the file is locked

public string|null $lockExplanation = null

$previewUrl

A URL to the file preview

public string|null $previewUrl = null

$size

The file size in bytes

public int|null $size = null

$thumbnailUrl

An abbreviated URL to the file that can be inserted into the rich content editor

public string|null $thumbnailUrl = null

$unlockAt

The datetime the file will be deleted (not present for file upload requests)

public string|null $unlockAt = null

$updatedAt

The datetime the file was last updated

public string|null $updatedAt = null

$url

The URL to download the file (not present for file upload requests)

public string|null $url = null

$uuid

The UUID of the file

public string|null $uuid = null

$contextId

Context ID where the file belongs

protected int|null $contextId = null

$contextType

Context type where the file belongs

protected string|null $contextType = null

$methodAliases

Define method aliases

protected static array<string|int, mixed> $methodAliases = ['get' => ['fetch', 'list', 'fetchAll'], 'all' => ['fetchAllPages', 'getAll'], 'paginate' => ['getPaginated', 'withPagination', 'fetchPage'], 'find' => ['one', 'getOne']]

Methods

__callStatic()

Magic method to handle function aliases

public static __callStatic(string $name, array<string|int, mixed> $arguments) : mixed
Parameters
$name : string
$arguments : array<string|int, mixed>
Tags
throws
InvalidArgumentException

__construct()

BaseApi constructor.

public __construct(array<string|int, mixed> $data) : mixed
Parameters
$data : array<string|int, mixed>

all()

Get all files from current user's personal files.

public static all([array<string, mixed> $params = [] ]) : array<string|int, static>

Overrides base to set context information.

Parameters
$params : array<string, mixed> = []

Query parameters

Return values
array<string|int, static>

delete()

Delete the file

public delete() : self
Return values
self

fetchByContext()

Fetch files from a specific context

public static fetchByContext(string $contextType, int $contextId[, array<string, mixed> $params = [] ]) : array<string|int, self>
Parameters
$contextType : string

Context type ('courses', 'groups', 'users', 'folders')

$contextId : int

Context ID (course_id, group_id, user_id, or folder_id)

$params : array<string, mixed> = []

Query parameters

Tags
throws
CanvasApiException
Return values
array<string|int, self>

fetchCourseFiles()

Fetch files for a course

public static fetchCourseFiles(int $courseId[, array<string|int, mixed> $params = [] ]) : array<string|int, File>
Parameters
$courseId : int
$params : array<string|int, mixed> = []
Tags
throws
CanvasApiException
Return values
array<string|int, File>

fetchGroupFiles()

Fetch files for a group

public static fetchGroupFiles(int $groupId[, array<string|int, mixed> $params = [] ]) : array<string|int, File>
Parameters
$groupId : int
$params : array<string|int, mixed> = []
Tags
throws
CanvasApiException
Return values
array<string|int, File>

fetchUserFiles()

Fetch files for a user

public static fetchUserFiles(int $userId[, array<string|int, mixed> $params = [] ]) : array<string|int, File>
Parameters
$userId : int
$params : array<string|int, mixed> = []
Tags
throws
CanvasApiException
Return values
array<string|int, File>

find()

Find a file by ID

public static find(int $id[, array<string|int, mixed> $params = [] ]) : self
Parameters
$id : int
$params : array<string|int, mixed> = []

Optional query parameters

Tags
throws
CanvasApiException
Return values
self

folder()

Get the folder containing this file

public folder() : null

NOTE: The Folder API class is not yet implemented in this SDK. This method serves as a placeholder for future implementation.

Return values
null

get()

Get first page of files from current user's personal files.

public static get([array<string, mixed> $params = [] ]) : array<string|int, static>

Overrides base to set context information.

Parameters
$params : array<string, mixed> = []

Query parameters

Return values
array<string|int, static>

getContentType()

Get the content type

public getContentType() : string
Return values
string

getContextId()

Get the context ID

public getContextId() : int|null
Return values
int|null

getContextType()

Get the context type

public getContextType() : string|null
Return values
string|null

getCreatedAt()

Get the created at timestamp

public getCreatedAt() : string
Return values
string

getDisplayName()

Get the display name

public getDisplayName() : string
Return values
string

getDownloadUrl()

Get file download URL

public getDownloadUrl() : string
Tags
throws
CanvasApiException
Return values
string

getFilename()

Get the filename

public getFilename() : string
Return values
string

getFolderId()

Get the folder ID

public getFolderId() : int
Return values
int

getId()

Get the file ID

public getId() : int
Return values
int

getSize()

Get the file size

public getSize() : int
Return values
int

getUpdatedAt()

Get the updated at timestamp

public getUpdatedAt() : string
Return values
string

getUrl()

Get the file URL

public getUrl() : string|null
Return values
string|null

getUuid()

Get the file UUID

public getUuid() : string
Return values
string

isHidden()

Check if the file is hidden

public isHidden() : bool
Return values
bool

isLocked()

Check if the file is locked

public isLocked() : bool
Return values
bool

paginate()

Get paginated files from current user's personal files.

public static paginate([array<string, mixed> $params = [] ]) : PaginationResult

Overrides base to set context information.

Parameters
$params : array<string, mixed> = []

Query parameters

Return values
PaginationResult

setContentType()

Set the content type

public setContentType(string $contentType) : void
Parameters
$contentType : string

setContextId()

Set the context ID

public setContextId(int|null $contextId) : void
Parameters
$contextId : int|null

setContextType()

Set the context type

public setContextType(string|null $contextType) : void
Parameters
$contextType : string|null

setCreatedAt()

Set the created at timestamp

public setCreatedAt(string $createdAt) : void
Parameters
$createdAt : string

setDisplayName()

Set the display name

public setDisplayName(string $displayName) : void
Parameters
$displayName : string

setFilename()

Set the filename

public setFilename(string $filename) : void
Parameters
$filename : string

setFolderId()

Set the folder ID

public setFolderId(int $folderId) : void
Parameters
$folderId : int

setHidden()

Set the hidden status

public setHidden(bool $hidden) : void
Parameters
$hidden : bool

setId()

Set the file ID

public setId(int $id) : void
Parameters
$id : int

setLocked()

Set the locked status

public setLocked(bool $locked) : void
Parameters
$locked : bool

setSize()

Set the file size

public setSize(int $size) : void
Parameters
$size : int

setUpdatedAt()

Set the updated at timestamp

public setUpdatedAt(string $updatedAt) : void
Parameters
$updatedAt : string

setUrl()

Set the file URL

public setUrl(string|null $url) : void
Parameters
$url : string|null

setUuid()

Set the file UUID

public setUuid(string $uuid) : void
Parameters
$uuid : string

uploadToAssignmentSubmission()

Upload a file for an assignment submission

public static uploadToAssignmentSubmission(int $courseId, int $assignmentId, UploadFileDTO|array<string|int, mixed> $fileData) : self
Parameters
$courseId : int
$assignmentId : int
$fileData : UploadFileDTO|array<string|int, mixed>
Tags
throws
Exception
Return values
self

uploadToContext()

Upload a file to a specific context

public static uploadToContext(string $contextType, int $contextId, array<string, mixed>|UploadFileDTO $fileData) : self
Parameters
$contextType : string

Context type ('courses', 'groups', 'users')

$contextId : int

Context ID

$fileData : array<string, mixed>|UploadFileDTO

File data to upload

Tags
throws
CanvasApiException
Return values
self

uploadToCourse()

Upload a file to a course

public static uploadToCourse(int $courseId, UploadFileDTO|array<string|int, mixed> $fileData) : self
Parameters
$courseId : int
$fileData : UploadFileDTO|array<string|int, mixed>
Tags
throws
Exception
Return values
self

uploadToGroup()

Upload a file to a group

public static uploadToGroup(int $groupId, UploadFileDTO|array<string|int, mixed> $fileData) : self
Parameters
$groupId : int
$fileData : UploadFileDTO|array<string|int, mixed>
Tags
throws
Exception
Return values
self

uploadToUser()

Upload a file to a user

public static uploadToUser(int $userId, UploadFileDTO|array<string|int, mixed> $fileData) : self
Parameters
$userId : int
$fileData : UploadFileDTO|array<string|int, mixed>
Tags
throws
Exception
Return values
self

castValue()

Cast a value to the correct type

protected castValue(string $key, mixed $value) : DateTime|mixed
Parameters
$key : string
$value : mixed
Tags
throws
Exception
Return values
DateTime|mixed

checkApiClient()

Check if the API client is set, if not, instantiate a new one

protected static checkApiClient() : void

convertPaginatedResponseToModels()

Helper method to convert paginated response data to model instances

protected static convertPaginatedResponseToModels(PaginatedResponse $paginatedResponse) : array<string|int, static>
Parameters
$paginatedResponse : PaginatedResponse
Return values
array<string|int, static>

getEndpoint()

Get the endpoint for this resource.

protected static getEndpoint() : string

Files default to current user context.

Return values
string

getPaginatedResponse()

Helper method to get paginated response from API endpoint

protected static getPaginatedResponse(string $endpoint[, array<string|int, mixed> $params = [] ]) : PaginatedResponse
Parameters
$endpoint : string

The API endpoint path

$params : array<string|int, mixed> = []

Query parameters for the request

Return values
PaginatedResponse

populate()

Populate the object with new data

protected populate(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>
Tags
throws
Exception

toDtoArray()

Convert the object to an array

protected toDtoArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

        
On this page

Search results