FileFormField.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * @api
  17. */
  18. class FileFormField extends FormField
  19. {
  20. /**
  21. * Sets the PHP error code associated with the field.
  22. *
  23. * @param integer $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)
  24. *
  25. * @throws \InvalidArgumentException When error code doesn't exist
  26. */
  27. public function setErrorCode($error)
  28. {
  29. $codes = array(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);
  30. if (!in_array($error, $codes)) {
  31. throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
  32. }
  33. $this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0);
  34. }
  35. /**
  36. * Sets the value of the field.
  37. *
  38. * @param string $value The value of the field
  39. *
  40. * @api
  41. */
  42. public function upload($value)
  43. {
  44. $this->setValue($value);
  45. }
  46. /**
  47. * Sets the value of the field.
  48. *
  49. * @param string $value The value of the field
  50. */
  51. public function setValue($value)
  52. {
  53. if (null !== $value && is_readable($value)) {
  54. $error = UPLOAD_ERR_OK;
  55. $size = filesize($value);
  56. $info = pathinfo($value);
  57. $name = $info['basename'];
  58. // copy to a tmp location
  59. $tmp = sys_get_temp_dir().'/'.sha1(uniqid(mt_rand(), true));
  60. if (array_key_exists('extension', $info)) {
  61. $tmp .= '.'.$info['extension'];
  62. }
  63. if (is_file($tmp)) {
  64. unlink($tmp);
  65. }
  66. copy($value, $tmp);
  67. $value = $tmp;
  68. } else {
  69. $error = UPLOAD_ERR_NO_FILE;
  70. $size = 0;
  71. $name = '';
  72. $value = '';
  73. }
  74. $this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size);
  75. }
  76. /**
  77. * Sets path to the file as string for simulating HTTP request
  78. *
  79. * @param string $path The path to the file
  80. */
  81. public function setFilePath($path)
  82. {
  83. parent::setValue($path);
  84. }
  85. /**
  86. * Initializes the form field.
  87. *
  88. * @throws \LogicException When node type is incorrect
  89. */
  90. protected function initialize()
  91. {
  92. if ('input' != $this->node->nodeName) {
  93. throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
  94. }
  95. if ('file' != $this->node->getAttribute('type')) {
  96. 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')));
  97. }
  98. $this->setValue(null);
  99. }
  100. }