123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace Symfony\Component\DomCrawler\Field;
- class FileFormField extends FormField
- {
-
- public function setErrorCode($error)
- {
- $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];
- if (!\in_array($error, $codes)) {
- throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
- }
- $this->value = ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0];
- }
-
- public function upload($value)
- {
- $this->setValue($value);
- }
-
- public function setValue($value)
- {
- if (null !== $value && is_readable($value)) {
- $error = UPLOAD_ERR_OK;
- $size = filesize($value);
- $info = pathinfo($value);
- $name = $info['basename'];
-
- $tmp = sys_get_temp_dir().'/'.strtr(substr(base64_encode(hash('sha256', uniqid(mt_rand(), true), true)), 0, 7), '/', '_');
- if (\array_key_exists('extension', $info)) {
- $tmp .= '.'.$info['extension'];
- }
- if (is_file($tmp)) {
- unlink($tmp);
- }
- copy($value, $tmp);
- $value = $tmp;
- } else {
- $error = UPLOAD_ERR_NO_FILE;
- $size = 0;
- $name = '';
- $value = '';
- }
- $this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size];
- }
-
- public function setFilePath($path)
- {
- parent::setValue($path);
- }
-
- protected function initialize()
- {
- if ('input' !== $this->node->nodeName) {
- throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
- }
- if ('file' !== strtolower($this->node->getAttribute('type'))) {
- 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')));
- }
- $this->setValue(null);
- }
- }
|