UploadedFileInterface.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare(strict_types=1);
  3. namespace Psr\Http\Message;
  4. /**
  5. * Value object representing a file uploaded through an HTTP request.
  6. *
  7. * Instances of this interface are considered immutable; all methods that
  8. * might change state MUST be implemented such that they retain the internal
  9. * state of the current instance and return an instance that contains the
  10. * changed state.
  11. */
  12. interface UploadedFileInterface
  13. {
  14. /**
  15. * Retrieve a stream representing the uploaded file.
  16. *
  17. * This method MUST return a StreamInterface instance, representing the
  18. * uploaded file. The purpose of this method is to allow utilizing native PHP
  19. * stream functionality to manipulate the file upload, such as
  20. * stream_copy_to_stream() (though the result will need to be decorated in a
  21. * native PHP stream wrapper to work with such functions).
  22. *
  23. * If the moveTo() method has been called previously, this method MUST raise
  24. * an exception.
  25. *
  26. * @return StreamInterface Stream representation of the uploaded file.
  27. * @throws \RuntimeException in cases when no stream is available or can be
  28. * created.
  29. */
  30. public function getStream();
  31. /**
  32. * Move the uploaded file to a new location.
  33. *
  34. * Use this method as an alternative to move_uploaded_file(). This method is
  35. * guaranteed to work in both SAPI and non-SAPI environments.
  36. * Implementations must determine which environment they are in, and use the
  37. * appropriate method (move_uploaded_file(), rename(), or a stream
  38. * operation) to perform the operation.
  39. *
  40. * $targetPath may be an absolute path, or a relative path. If it is a
  41. * relative path, resolution should be the same as used by PHP's rename()
  42. * function.
  43. *
  44. * The original file or stream MUST be removed on completion.
  45. *
  46. * If this method is called more than once, any subsequent calls MUST raise
  47. * an exception.
  48. *
  49. * When used in an SAPI environment where $_FILES is populated, when writing
  50. * files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be
  51. * used to ensure permissions and upload status are verified correctly.
  52. *
  53. * If you wish to move to a stream, use getStream(), as SAPI operations
  54. * cannot guarantee writing to stream destinations.
  55. *
  56. * @see http://php.net/is_uploaded_file
  57. * @see http://php.net/move_uploaded_file
  58. * @param string $targetPath Path to which to move the uploaded file.
  59. * @throws \InvalidArgumentException if the $targetPath specified is invalid.
  60. * @throws \RuntimeException on any error during the move operation, or on
  61. * the second or subsequent call to the method.
  62. */
  63. public function moveTo(string $targetPath);
  64. /**
  65. * Retrieve the file size.
  66. *
  67. * Implementations SHOULD return the value stored in the "size" key of
  68. * the file in the $_FILES array if available, as PHP calculates this based
  69. * on the actual size transmitted.
  70. *
  71. * @return int|null The file size in bytes or null if unknown.
  72. */
  73. public function getSize();
  74. /**
  75. * Retrieve the error associated with the uploaded file.
  76. *
  77. * The return value MUST be one of PHP's UPLOAD_ERR_XXX constants.
  78. *
  79. * If the file was uploaded successfully, this method MUST return
  80. * UPLOAD_ERR_OK.
  81. *
  82. * Implementations SHOULD return the value stored in the "error" key of
  83. * the file in the $_FILES array.
  84. *
  85. * @see http://php.net/manual/en/features.file-upload.errors.php
  86. * @return int One of PHP's UPLOAD_ERR_XXX constants.
  87. */
  88. public function getError();
  89. /**
  90. * Retrieve the filename sent by the client.
  91. *
  92. * Do not trust the value returned by this method. A client could send
  93. * a malicious filename with the intention to corrupt or hack your
  94. * application.
  95. *
  96. * Implementations SHOULD return the value stored in the "name" key of
  97. * the file in the $_FILES array.
  98. *
  99. * @return string|null The filename sent by the client or null if none
  100. * was provided.
  101. */
  102. public function getClientFilename();
  103. /**
  104. * Retrieve the media type sent by the client.
  105. *
  106. * Do not trust the value returned by this method. A client could send
  107. * a malicious media type with the intention to corrupt or hack your
  108. * application.
  109. *
  110. * Implementations SHOULD return the value stored in the "type" key of
  111. * the file in the $_FILES array.
  112. *
  113. * @return string|null The media type sent by the client or null if none
  114. * was provided.
  115. */
  116. public function getClientMediaType();
  117. }