CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
      • Client
      • Cookie
      • Exception
      • Link
      • Middleware
      • RateLimit
      • Response
      • Session
      • TestSuite
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class UploadedFile

PSR-7 uploaded file implementation.

Namespace: Cake\Http

Constants

  • array<int, string>
    ERROR_MESSAGES ¶
    [UPLOAD_ERR_OK => 'There is no error, the file uploaded with success', UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was ' . 'specified in the HTML form', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded', UPLOAD_ERR_NO_FILE => 'No file was uploaded', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload']

    Human readable messages for the UPLOAD_ERR_* constants.

Property Summary

  • $file protected
    string|null

    Path to the uploaded file.

  • $moved protected
    bool

    Whether the file has been moved already.

  • $stream protected
    Psr\Http\Message\StreamInterface|null

    Stream of the uploaded file.

Method Summary

  • __construct() public

    Constructor

  • assertMovable() protected

    Ensure the upload succeeded and hasn't been moved yet.

  • copyStream() protected

    Copy a readable upload into an open destination, checking every write.

  • getClientFilename() public

    Retrieve the filename sent by the client.

    Do not trust the value returned by this method. A client could send a malicious filename with the intention to corrupt or hack your application.

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

  • getClientMediaType() public

    Retrieve the media type sent by the client.

    Do not trust the value returned by this method. A client could send a malicious media type with the intention to corrupt or hack your application.

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

  • getError() public

    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.

  • getSize() public

    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.

  • getStream() public

    Retrieve a stream representing the uploaded file.

    This method MUST return a StreamInterface instance, representing the uploaded file. The purpose of this method is to allow utilizing native PHP stream functionality to manipulate the file upload, such as stream_copy_to_stream() (though the result will need to be decorated in a native PHP stream wrapper to work with such functions).

    If the moveTo() method has been called previously, this method MUST raise an exception.

  • moveTo() public

    Move the uploaded file to a new location.

    Use this method as an alternative to move_uploaded_file(). This method is guaranteed to work in both SAPI and non-SAPI environments. Implementations must determine which environment they are in, and use the appropriate method (move_uploaded_file(), rename(), or a stream operation) to perform the operation.

    $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 or stream 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.

  • writeFile() protected

    Write the uploaded file stream to the given path.

Method Detail

__construct() ¶ public

__construct(Psr\Http\Message\StreamInterface|resource|string $streamOrFile, int|null $size, int $error, string|null $clientFilename = null, string|null $clientMediaType = null)

Constructor

Parameters
Psr\Http\Message\StreamInterface|resource|string $streamOrFile

The file path, resource or stream.

int|null $size

The file size in bytes.

int $error

One of the UPLOAD_ERR_* constants.

string|null $clientFilename optional

The filename as provided by the client.

string|null $clientMediaType optional

The media type as provided by the client.

Throws
InvalidArgumentException
When the stream/file or error status is invalid.

assertMovable() ¶ protected

assertMovable(): void

Ensure the upload succeeded and hasn't been moved yet.

Returns
void
Throws
RuntimeException

copyStream() ¶ protected

copyStream(Psr\Http\Message\StreamInterface $stream, resource $handle, string $path): void

Copy a readable upload into an open destination, checking every write.

Parameters
Psr\Http\Message\StreamInterface $stream

The upload source.

resource $handle

The destination handle.

string $path

The destination path for error messages.

Returns
void

getClientFilename() ¶ public

getClientFilename(): string|null

Retrieve the filename sent by the client.

Do not trust the value returned by this method. A client could send a malicious filename with the intention to corrupt or hack your application.

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

Returns
string|null

getClientMediaType() ¶ public

getClientMediaType(): string|null

Retrieve the media type sent by the client.

Do not trust the value returned by this method. A client could send a malicious media type with the intention to corrupt or hack your application.

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

Returns
string|null

getError() ¶ public

getError(): int

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

getSize() ¶ public

getSize(): int|null

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|null

getStream() ¶ public

getStream(): StreamInterface

Retrieve a stream representing the uploaded file.

This method MUST return a StreamInterface instance, representing the uploaded file. The purpose of this method is to allow utilizing native PHP stream functionality to manipulate the file upload, such as stream_copy_to_stream() (though the result will need to be decorated in a native PHP stream wrapper to work with such functions).

If the moveTo() method has been called previously, this method MUST raise an exception.

Returns
StreamInterface

moveTo() ¶ public

moveTo(string $targetPath): void

Move the uploaded file to a new location.

Use this method as an alternative to move_uploaded_file(). This method is guaranteed to work in both SAPI and non-SAPI environments. Implementations must determine which environment they are in, and use the appropriate method (move_uploaded_file(), rename(), or a stream operation) to perform the operation.

$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 or stream 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.

Parameters
string $targetPath
Returns
void

writeFile() ¶ protected

writeFile(string $path): void

Write the uploaded file stream to the given path.

Parameters
string $path

The target path.

Returns
void
Throws
RuntimeException
When the target path can't be opened.

Property Detail

$file ¶ protected

Path to the uploaded file.

Type
string|null

$moved ¶ protected

Whether the file has been moved already.

Type
bool

$stream ¶ protected

Stream of the uploaded file.

Type
Psr\Http\Message\StreamInterface|null
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs