• UploadFile对象
    • 实现代码:

    UploadFile对象

    该对象在用户上传文件时自动生成,可通过以下方法获取

    1. <?php
    2. $request= $this->request();
    3. $img_file = $request->getUploadedFile('img');//获取一个上传文件,返回的是一个\EasySwoole\Http\Message\UploadFile的对象
    4. $data = $request->getUploadedFiles();

    实现代码:

    1. <?php
    2. /**
    3. * Created by PhpStorm.
    4. * User: yf
    5. * Date: 2018/5/24
    6. * Time: 下午3:20
    7. */
    8. namespace EasySwoole\Http\Message;
    9. class UploadFile
    10. {
    11. private $tempName;
    12. private $stream;
    13. private $size;
    14. private $error;
    15. private $clientFileName;
    16. private $clientMediaType;
    17. function __construct( $tempName,$size, $errorStatus, $clientFilename = null, $clientMediaType = null)
    18. {
    19. $this->tempName = $tempName;
    20. $this->stream = new Stream(fopen($tempName,"r+"));
    21. $this->error = $errorStatus;
    22. $this->size = $size;
    23. $this->clientFileName = $clientFilename;
    24. $this->clientMediaType = $clientMediaType;
    25. }
    26. public function getTempName() {
    27. // TODO: Implement getTempName() method.
    28. return $this->tempName;
    29. }
    30. public function getStream()
    31. {
    32. // TODO: Implement getStream() method.
    33. return $this->stream;
    34. }
    35. public function moveTo($targetPath)
    36. {
    37. // TODO: Implement moveTo() method.
    38. return file_put_contents($targetPath,$this->stream) ? true :false;
    39. }
    40. public function getSize()
    41. {
    42. // TODO: Implement getSize() method.
    43. return $this->size;
    44. }
    45. public function getError()
    46. {
    47. // TODO: Implement getError() method.
    48. return $this->error;
    49. }
    50. public function getClientFilename()
    51. {
    52. // TODO: Implement getClientFilename() method.
    53. return $this->clientFileName;
    54. }
    55. public function getClientMediaType()
    56. {
    57. // TODO: Implement getClientMediaType() method.
    58. return $this->clientMediaType;
    59. }
    60. }