FileFormField.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DomCrawler\Field;
  11. /**
  12. * FileFormField represents a file form field (an HTML file input tag).
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class FileFormField extends FormField
  17. {
  18. /**
  19. * Sets the PHP error code associated with the field.
  20. *
  21. * @param int $error The error code (one of UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, or UPLOAD_ERR_EXTENSION)
  22. *
  23. * @throws \InvalidArgumentException When error code doesn't exist
  24. */
  25. public function setErrorCode(int $error)
  26. {
  27. $codes = [\UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION];
  28. if (!\in_array($error, $codes)) {
  29. throw new \InvalidArgumentException(sprintf('The error code "%s" is not valid.', $error));
  30. }
  31. $this->value = ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0];
  32. }
  33. /**
  34. * Sets the value of the field.
  35. */
  36. public function upload(?string $value)
  37. {
  38. $this->setValue($value);
  39. }
  40. /**
  41. * Sets the value of the field.
  42. */
  43. public function setValue(?string $value)
  44. {
  45. if (null !== $value && is_readable($value)) {
  46. $error = \UPLOAD_ERR_OK;
  47. $size = filesize($value);
  48. $info = pathinfo($value);
  49. $name = $info['basename'];
  50. // copy to a tmp location
  51. $tmp = sys_get_temp_dir().'/'.strtr(substr(base64_encode(hash('sha256', uniqid(mt_rand(), true), true)), 0, 7), '/', '_');
  52. if (\array_key_exists('extension', $info)) {
  53. $tmp .= '.'.$info['extension'];
  54. }
  55. if (is_file($tmp)) {
  56. unlink($tmp);
  57. }
  58. copy($value, $tmp);
  59. $value = $tmp;
  60. } else {
  61. $error = \UPLOAD_ERR_NO_FILE;
  62. $size = 0;
  63. $name = '';
  64. $value = '';
  65. }
  66. $this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size];
  67. }
  68. /**
  69. * Sets path to the file as string for simulating HTTP request.
  70. */
  71. public function setFilePath(string $path)
  72. {
  73. parent::setValue($path);
  74. }
  75. /**
  76. * Initializes the form field.
  77. *
  78. * @throws \LogicException When node type is incorrect
  79. */
  80. protected function initialize()
  81. {
  82. if ('input' !== $this->node->nodeName) {
  83. throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
  84. }
  85. if ('file' !== strtolower($this->node->getAttribute('type'))) {
  86. throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is "%s").', $this->node->getAttribute('type')));
  87. }
  88. $this->setValue(null);
  89. }
  90. }