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::fetchAll();

// Fetch files with pagination support
$paginatedResponse = File::fetchAllPaginated(['per_page' => 10]);
$files = $paginatedResponse->getJsonData();
$pagination = $paginatedResponse->toPaginationResult($files);

// Fetch a specific page of files
$paginationResult = File::fetchPage(['page' => 2, 'per_page' => 10]);
$files = $paginationResult->getData();
$hasNext = $paginationResult->hasNext();

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

Table of Contents

Properties

$contentType  : string
The content-type of the file
$createdAt  : string
The datetime the file was created
$displayName  : string
The display name of the file
$filename  : string
The filename of the file
$folderId  : int
The folder_id of the folder containing the file
$hidden  : bool
Whether the file is hidden
$id  : int
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
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
The datetime the file was last updated
$url  : string|null
The URL to download the file (not present for file upload requests)
$uuid  : string
The UUID of the file
$apiClient  : HttpClientInterface
$methodAliases  : array<string|int, mixed>
Define method aliases

Methods

__callStatic()  : mixed
Magic method to handle function aliases
__construct()  : mixed
BaseApi constructor.
delete()  : bool
Delete the file
fetchAll()  : array<string|int, File>
Fetch all files from current user's personal files
fetchAllPages()  : array<string|int, File>
Fetch all files from all pages from current user's personal files
fetchAllPaginated()  : PaginatedResponse
Fetch files with pagination support from current user's personal files
fetchCourseFiles()  : array<string|int, File>
Fetch files for a course
fetchGroupFiles()  : array<string|int, File>
Fetch files for a group
fetchPage()  : PaginationResult
Fetch files from a specific page from current user's personal files
fetchUserFiles()  : array<string|int, File>
Fetch files for a user
find()  : self
Find a file by ID
getContentType()  : string
Get the content 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
setApiClient()  : void
Set the API client
setContentType()  : void
Set the content 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
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
createPaginationResult()  : PaginationResult
Helper method to create PaginationResult from paginated response
fetchAllPagesAsModels()  : array<string|int, static>
Helper method to fetch all pages and convert to model instances
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 $contentType

$createdAt

The datetime the file was created

public string $createdAt

$displayName

The display name of the file

public string $displayName

$filename

The filename of the file

public string $filename

$folderId

The folder_id of the folder containing the file

public int $folderId

$hidden

Whether the file is hidden

public bool $hidden = false

$id

The unique identifier for the file

public int $id

$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 $size

$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 $updatedAt

$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 $uuid

$methodAliases

Define method aliases

protected static array<string|int, mixed> $methodAliases = ['fetchAll' => ['all', 'get', 'getAll'], 'find' => ['one', 'getOne'], 'fetchAllPaginated' => ['allPaginated', 'getPaginated'], 'fetchAllPages' => ['allPages', 'getPages'], 'fetchPage' => ['page', 'getPage']]

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>

__construct()

BaseApi constructor.

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

delete()

Delete the file

public delete() : bool
Return values
bool

fetchAll()

Fetch all files from current user's personal files

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

NOTE: Unlike other API classes (Course, User) that fetch from the account level, File::fetchAll() returns the current user's personal files. This is because files in Canvas are inherently context-specific and there is no global "all files" endpoint.

For specific contexts, use:

  • fetchCourseFiles() for course files
  • fetchUserFiles() for a specific user's files
  • fetchGroupFiles() for group files
Parameters
$params : array<string|int, mixed> = []

Query parameters for filtering/pagination

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

fetchAllPages()

Fetch all files from all pages from current user's personal files

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

Query parameters for the request

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

fetchAllPaginated()

Fetch files with pagination support from current user's personal files

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

Query parameters for the request

Tags
throws
CanvasApiException
Return values
PaginatedResponse

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>

fetchPage()

Fetch files from a specific page from current user's personal files

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

Query parameters for the request

Tags
throws
CanvasApiException
Return values
PaginationResult

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) : self
Parameters
$id : int
Tags
throws
CanvasApiException
Return values
self

getContentType()

Get the content type

public getContentType() : string
Return values
string

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

setContentType()

Set the content type

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

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

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>

fetchAllPagesAsModels()

Helper method to fetch all pages and convert to model instances

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

The API endpoint path

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

Query parameters for the request

Return values
array<string|int, static>

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