UploadedFileInterface.php 4.6 KB

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