src/Actions/UploadAction.php
protected function handleFile($field, $options)
    {
        $file = $this->form->data($field);

        if (!is_array($file) || !isset($file['error']) || intval($file['error']) !== UPLOAD_ERR_OK) {
            // If this is an array, kirby-form already recognized and validated the
            // uploaded file. If the file is required, this should have been checked
            // during validation.
            return;
        }

        if (!array_key_exists('target', $options)) {
            // No translation because this is a developer error.
            $this->fail("The target directory is missing for field {$field}.");
        }

        $target = $options['target'];

        if (!is_dir($target)) {
            if (@mkdir($target, 0755)) {
                $this->createdDirectories[] = $target;
            } else {
                $this->fail(I18n::translate('uniform-upload-mkdir-fail'), $field);
            }
        }

        $name = $file['name'];
        $prefix = A::get($options, 'prefix');

        if (is_null($prefix)) {
            $name = $this->getRandomPrefix($name);
        } elseif ($prefix !== false) {
            $name = $prefix.$name;
        }

        $path = $target.DIRECTORY_SEPARATOR.$name;
        if (is_file($path)) {
            $this->fail(I18n::translate('uniform-upload-exists'), $field);
        }

        $success = $this->moveFile($file['tmp_name'], $path);

        if ($success) {
            $this->createdFiles[] = $path;
        } else {
            $this->fail(I18n::translate('uniform-upload-failed'), $field);
        }
    }
protected function handleFile($field, $options)
    {
        $file = $this->form->data($field);

        if (!is_array($file) || !isset($file['error']) || intval($file['error']) !== UPLOAD_ERR_OK) {
            // If this is an array, kirby-form already recognized and validated the
            // uploaded file. If the file is required, this should have been checked
            // during validation.
            return;
        }

        if (!array_key_exists('target', $options)) {
            // No translation because this is a developer error.
            $this->fail("The target directory is missing for field {$field}.");
        }

        $target = $options['target'];

        if (!is_dir($target)) {
            if (@mkdir($target, 0755)) {
                $this->createdDirectories[] = $target;
            } else {
                $this->fail(I18n::translate('uniform-upload-mkdir-fail'), $field);
            }
        }

        $name = $file['name'];
        $prefix = A::get($options, 'prefix');

        if (is_null($prefix)) {
            $name = $this->getRandomPrefix($name);
        } elseif ($prefix !== false) {
            $name = $prefix.$name;
        }

        $path = $target.DIRECTORY_SEPARATOR.$name;
        if (is_file($path)) {
            $this->fail(I18n::translate('uniform-upload-exists'), $field);
        }

        $success = $this->moveFile($file['tmp_name'], $path);

        if ($success) {
            $this->createdFiles[] = $path;
        } else {
            $this->fail(I18n::translate('uniform-upload-failed'), $field);
        }
    }
protected function handleFile ( $ field , $ options ) { $ file = $ this -> form -> data ( $ field ) ; if ( ! is_array ( $ file ) || ! isset ( $ file [ 'error' ] ) || intval ( $ file [ 'error' ] ) !== UPLOAD_ERR_OK ) { // If this is an array, kirby-form already recognized and validated the // uploaded file. If the file is required, this should have been checked // during validation. return ; } if ( ! array_key_exists ( 'target' , $ options ) ) { // No translation because this is a developer error. $ this -> fail ( "The target directory is missing for field {$field}." ) ; } $ target = $ options [ 'target' ] ; if ( ! is_dir ( $ target ) ) { if ( @ mkdir ( $ target , 0755 ) ) { $ this -> createdDirectories [ ] = $ target ; } else { $ this -> fail ( I18n :: translate ( 'uniform-upload-mkdir-fail' ) , $ field ) ; } } $ name = $ file [ 'name' ] ; $ prefix = A :: get ( $ options , 'prefix' ) ; if ( is_null ( $ prefix ) ) { $ name = $ this -> getRandomPrefix ( $ name ) ; } elseif ( $ prefix !== false ) { $ name = $ prefix . $ name ; } $ path = $ target . DIRECTORY_SEPARATOR . $ name ; if ( is_file ( $ path ) ) { $ this -> fail ( I18n :: translate ( 'uniform-upload-exists' ) , $ field ) ; } $ success = $ this -> moveFile ( $ file [ 'tmp_name' ] , $ path ) ; if ( $ success ) { $ this -> createdFiles [ ] = $ path ; } else { $ this -> fail ( I18n :: translate ( 'uniform-upload-failed' ) , $ field ) ; } }
Move a single uploaded file.

@param string $field Form field name.
@param array $options
Move a single uploaded file .