FileFormField.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 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)
  22. *
  23. * @throws \InvalidArgumentException When error code doesn't exist
  24. */
  25. public function setErrorCode($error)
  26. {
  27. $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);
  28. if (!in_array($error, $codes)) {
  29. throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
  30. }
  31. $this->value = array('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. $name = basename($value);
  53. // copy to a tmp location
  54. $tmp = tempnam(sys_get_temp_dir(), 'upload');
  55. unlink($tmp);
  56. copy($value, $tmp);
  57. $value = $tmp;
  58. } else {
  59. $error = UPLOAD_ERR_NO_FILE;
  60. $size = 0;
  61. $name = '';
  62. $value = '';
  63. }
  64. $this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size);
  65. }
  66. /**
  67. * Initializes the form field.
  68. *
  69. * @throws \LogicException When node type is incorrect
  70. */
  71. protected function initialize()
  72. {
  73. if ('input' != $this->node->nodeName) {
  74. throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
  75. }
  76. if ('file' != $this->node->getAttribute('type')) {
  77. 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')));
  78. }
  79. $this->setValue(null);
  80. }
  81. }