FileFormField.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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($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. * @param string $value The value of the field
  37. */
  38. public function upload($value)
  39. {
  40. $this->setValue($value);
  41. }
  42. /**
  43. * Sets the value of the field.
  44. *
  45. * @param string $value The value of the field
  46. */
  47. public function setValue($value)
  48. {
  49. if (null !== $value && is_readable($value)) {
  50. $error = UPLOAD_ERR_OK;
  51. $size = filesize($value);
  52. $info = pathinfo($value);
  53. $name = $info['basename'];
  54. // copy to a tmp location
  55. $tmp = sys_get_temp_dir().'/'.strtr(substr(base64_encode(hash('sha256', uniqid(mt_rand(), true), true)), 0, 7), '/', '_');
  56. if (\array_key_exists('extension', $info)) {
  57. $tmp .= '.'.$info['extension'];
  58. }
  59. if (is_file($tmp)) {
  60. unlink($tmp);
  61. }
  62. copy($value, $tmp);
  63. $value = $tmp;
  64. } else {
  65. $error = UPLOAD_ERR_NO_FILE;
  66. $size = 0;
  67. $name = '';
  68. $value = '';
  69. }
  70. $this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size];
  71. }
  72. /**
  73. * Sets path to the file as string for simulating HTTP request.
  74. *
  75. * @param string $path The path to the file
  76. */
  77. public function setFilePath($path)
  78. {
  79. parent::setValue($path);
  80. }
  81. /**
  82. * Initializes the form field.
  83. *
  84. * @throws \LogicException When node type is incorrect
  85. */
  86. protected function initialize()
  87. {
  88. if ('input' !== $this->node->nodeName) {
  89. throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
  90. }
  91. if ('file' !== strtolower($this->node->getAttribute('type'))) {
  92. 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')));
  93. }
  94. $this->setValue(null);
  95. }
  96. }