Class UploadedFile

Summary

Fully Qualified Name: CodeIgniter\HTTP\Files\UploadedFile
Extends: File
Implements: UploadedFileInterface

Description

Value object representing a single file uploaded through an HTTP request. Used by the IncomingRequest class to provide files.

Typically, implementors will extend the SplFileInfo class.

Methods

Name Description Defined By
__construct() Accepts the file information as would be filled in from the $_FILES array. UploadedFile
getClientExtension() Returns the original file extension, based on the file name that was uploaded. This is NOT a trusted source. UploadedFile
getClientMimeType() Returns the mime type as provided by the client. UploadedFile
getClientName() Returns the name of the file as provided by the client during upload. UploadedFile
getDestination() Returns the destination path for the move operation where overwriting is not expected. File
getError() Retrieve the error associated with the uploaded file. UploadedFile
getErrorString() Get error string UploadedFile
getExtension() Overrides SPLFileInfo's to work with uploaded files, since the temp file that's been uploaded doesn't have an extension. UploadedFile
getMimeType() Retrieve the media type of the file. SHOULD not use information from the $_FILES array, but should use other methods to more accurately determine the type of file, like finfo, or mime_content_type(). File
getName() Retrieve the filename. This will typically be the filename sent by the client, and should not be trusted. If the file has been moved, this will return the final name of the moved file. UploadedFile
getRandomName() Generates a random names based on a simple hash and the time, with the correct file extension attached. File
getSize() Retrieve the file size. File
getSizeByUnit() Retrieve the file size by unit. File
getTempName() Gets the temporary filename where the file was uploaded to. UploadedFile
guessExtension() Attempts to determine the best file extension. UploadedFile
hasMoved() Returns whether the file has been moved or not. If it has, the move() method will not work and certain properties, like the tempName, will no longer be available. UploadedFile
isValid() Returns whether the file was uploaded successfully, based on whether it was uploaded via HTTP and has no errors. UploadedFile
move() Move the uploaded file to a new location. UploadedFile
store() Save the uploaded file to a new location. UploadedFile

Method Details

__construct()

Accepts the file information as would be filled in from the $_FILES array.

Parameter Name Type Description
$path string The
$originalName string The
$mimeType string The
$size int The
$error int The

Returns:

getClientExtension()

Returns the original file extension, based on the file name that was uploaded. This is NOT a trusted source.

For a trusted version, use guessExtension() instead.

Returns: string

getClientMimeType()

Returns the mime type as provided by the client.

This is NOT a trusted value. For a trusted version, use getMimeType() instead.

Returns: string The media type sent by the client or null if none was provided.

getClientName()

Returns the name of the file as provided by the client during upload.

Returns: string

getDestination()

Returns the destination path for the move operation where overwriting is not expected.

First, it checks whether the delimiter is present in the filename, if it is, then it checks whether the last element is an integer as there may be cases that the delimiter may be present in the filename. For the all other cases, it appends an integer starting from zero before the file's extension.

Parameter Name Type Description
$destination string
$delimiter string
$i int

Returns: string

getError()

Retrieve the error associated with the uploaded file.

The return value MUST be one of PHP's UPLOAD_ERR_XXX constants.

If the file was uploaded successfully, this method MUST return UPLOAD_ERR_OK.

Implementations SHOULD return the value stored in the "error" key of the file in the $_FILES array.

Returns: int One of PHP's UPLOAD_ERR_XXX constants.

getErrorString()

Get error string

Returns: string

getExtension()

Overrides SPLFileInfo's to work with uploaded files, since the temp file that's been uploaded doesn't have an extension.

Is simply an alias for guessExtension for a safer method than simply relying on the provided extension. Additionally it will return clientExtension in case if there are other extensions with the same mime type.

Returns:

getMimeType()

Retrieve the media type of the file. SHOULD not use information from the $_FILES array, but should use other methods to more accurately determine the type of file, like finfo, or mime_content_type().

Returns: string|null The media type we determined it to be.

getName()

Retrieve the filename. This will typically be the filename sent by the client, and should not be trusted. If the file has been moved, this will return the final name of the moved file.

Returns: string The filename sent by the client or null if none was provided.

getRandomName()

Generates a random names based on a simple hash and the time, with the correct file extension attached.

Returns: string

getSize()

Retrieve the file size.

Implementations SHOULD return the value stored in the "size" key of the file in the $_FILES array if available, as PHP calculates this based on the actual size transmitted.

Returns: int The file size in bytes

getSizeByUnit()

Retrieve the file size by unit.

Parameter Name Type Description
$unit string

Returns: int|string

getTempName()

Gets the temporary filename where the file was uploaded to.

Returns: string

guessExtension()

Attempts to determine the best file extension.

Returns: string|null

hasMoved()

Returns whether the file has been moved or not. If it has, the move() method will not work and certain properties, like the tempName, will no longer be available.

Returns: bool

isValid()

Returns whether the file was uploaded successfully, based on whether it was uploaded via HTTP and has no errors.

Returns: bool

move()

Move the uploaded file to a new location.

$targetPath may be an absolute path, or a relative path. If it is a relative path, resolution should be the same as used by PHP's rename() function.

The original file MUST be removed on completion.

If this method is called more than once, any subsequent calls MUST raise an exception.

When used in an SAPI environment where $_FILES is populated, when writing files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be used to ensure permissions and upload status are verified correctly.

If you wish to move to a stream, use getStream(), as SAPI operations cannot guarantee writing to stream destinations.

Parameter Name Type Description
$targetPath string Path
$name string the
$overwrite bool State

Returns: bool

store()

Save the uploaded file to a new location.

By default, upload files are saved in writable/uploads directory. The YYYYMMDD folder and random file name will be created.

Parameter Name Type Description
$folderName string the
$fileName string the

Returns: string file full path

Top