Contao\FileUpload::uploadTo PHP 메소드

uploadTo() 공개 메소드

Check the uploaded files and move them to the target directory
public uploadTo ( string $strTarget ) : array
$strTarget string
리턴 array
    public function uploadTo($strTarget)
    {
        if ($strTarget == '' || \Validator::isInsecurePath($strTarget)) {
            throw new \InvalidArgumentException('Invalid target path ' . $strTarget);
        }
        $maxlength_kb = $this->getMaximumUploadSize();
        $maxlength_kb_readable = $this->getReadableSize($maxlength_kb);
        $arrUploaded = array();
        $arrFiles = $this->getFilesFromGlobal();
        foreach ($arrFiles as $file) {
            // Sanitize the filename
            try {
                $file['name'] = \StringUtil::sanitizeFileName($file['name']);
            } catch (\InvalidArgumentException $e) {
                \Message::addError($GLOBALS['TL_LANG']['ERR']['filename']);
                $this->blnHasError = true;
                continue;
            }
            // Invalid file name
            if (!\Validator::isValidFileName($file['name'])) {
                \Message::addError($GLOBALS['TL_LANG']['ERR']['filename']);
                $this->blnHasError = true;
            } elseif (!is_uploaded_file($file['tmp_name'])) {
                if ($file['error'] == 1 || $file['error'] == 2) {
                    \Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['filesize'], $maxlength_kb_readable));
                    $this->blnHasError = true;
                } elseif ($file['error'] == 3) {
                    \Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['filepartial'], $file['name']));
                    $this->blnHasError = true;
                } elseif ($file['error'] > 0) {
                    \Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['fileerror'], $file['error'], $file['name']));
                    $this->blnHasError = true;
                }
            } elseif ($file['size'] > $maxlength_kb) {
                \Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['filesize'], $maxlength_kb_readable));
                $this->blnHasError = true;
            } else {
                $strExtension = strtolower(substr($file['name'], strrpos($file['name'], '.') + 1));
                // File type not allowed
                if (!in_array($strExtension, \StringUtil::trimsplit(',', strtolower(\Config::get('uploadTypes'))))) {
                    \Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['filetype'], $strExtension));
                    $this->blnHasError = true;
                } else {
                    $this->import('Files');
                    $strNewFile = $strTarget . '/' . $file['name'];
                    // Set CHMOD and resize if neccessary
                    if ($this->Files->move_uploaded_file($file['tmp_name'], $strNewFile)) {
                        $this->Files->chmod($strNewFile, \Config::get('defaultFileChmod'));
                        // Notify the user
                        \Message::addConfirmation(sprintf($GLOBALS['TL_LANG']['MSC']['fileUploaded'], $file['name']));
                        $this->log('File "' . $strNewFile . '" has been uploaded', __METHOD__, TL_FILES);
                        // Resize the uploaded image if necessary
                        $this->resizeUploadedImage($strNewFile);
                        $arrUploaded[] = $strNewFile;
                    }
                }
            }
        }
        return $arrUploaded;
    }

Usage Example

예제 #1
0
    /**
     * Return a form to choose a CSV file and import it
     *
     * @return string
     */
    public function importRecipients()
    {
        if (\Input::get('key') != 'import') {
            return '';
        }
        /** @var FileUpload $objUploader */
        $objUploader = new \FileUpload();
        // Import recipients
        if (\Input::post('FORM_SUBMIT') == 'tl_recipients_import') {
            $arrUploaded = $objUploader->uploadTo('system/tmp');
            if (empty($arrUploaded)) {
                \Message::addError($GLOBALS['TL_LANG']['ERR']['all_fields']);
                $this->reload();
            }
            $time = time();
            $intTotal = 0;
            $intInvalid = 0;
            foreach ($arrUploaded as $strCsvFile) {
                $objFile = new \File($strCsvFile);
                if ($objFile->extension != 'csv') {
                    \Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['filetype'], $objFile->extension));
                    continue;
                }
                // Get separator
                switch (\Input::post('separator')) {
                    case 'semicolon':
                        $strSeparator = ';';
                        break;
                    case 'tabulator':
                        $strSeparator = "\t";
                        break;
                    case 'linebreak':
                        $strSeparator = "\n";
                        break;
                    default:
                        $strSeparator = ',';
                        break;
                }
                $arrRecipients = array();
                $resFile = $objFile->handle;
                while (($arrRow = @fgetcsv($resFile, null, $strSeparator)) !== false) {
                    $arrRecipients = array_merge($arrRecipients, $arrRow);
                }
                $arrRecipients = array_filter(array_unique($arrRecipients));
                foreach ($arrRecipients as $strRecipient) {
                    // Skip invalid entries
                    if (!\Validator::isEmail($strRecipient)) {
                        $this->log('Recipient address "' . $strRecipient . '" seems to be invalid and was not imported', __METHOD__, TL_ERROR);
                        ++$intInvalid;
                        continue;
                    }
                    // Check whether the e-mail address exists
                    $objRecipient = $this->Database->prepare("SELECT COUNT(*) AS count FROM tl_newsletter_recipients WHERE pid=? AND email=?")->execute(\Input::get('id'), $strRecipient);
                    if ($objRecipient->count > 0) {
                        continue;
                    }
                    // Check whether the e-mail address has been blacklisted
                    $objBlacklist = $this->Database->prepare("SELECT COUNT(*) AS count FROM tl_newsletter_blacklist WHERE pid=? AND hash=?")->execute(\Input::get('id'), md5($strRecipient));
                    if ($objBlacklist->count > 0) {
                        $this->log('Recipient address "' . $strRecipient . '" has been unsubscribed and was not imported', __METHOD__, TL_ERROR);
                        continue;
                    }
                    $this->Database->prepare("INSERT INTO tl_newsletter_recipients SET pid=?, tstamp={$time}, email=?, active=1")->execute(\Input::get('id'), $strRecipient);
                    ++$intTotal;
                }
            }
            \Message::addConfirmation(sprintf($GLOBALS['TL_LANG']['tl_newsletter_recipients']['confirm'], $intTotal));
            if ($intInvalid > 0) {
                \Message::addInfo(sprintf($GLOBALS['TL_LANG']['tl_newsletter_recipients']['invalid'], $intInvalid));
            }
            \System::setCookie('BE_PAGE_OFFSET', 0, 0);
            $this->reload();
        }
        // Return form
        return '
<div id="tl_buttons">
<a href="' . ampersand(str_replace('&key=import', '', \Environment::get('request'))) . '" class="header_back" title="' . \StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['backBTTitle']) . '" accesskey="b">' . $GLOBALS['TL_LANG']['MSC']['backBT'] . '</a>
</div>
' . \Message::generate() . '
<form action="' . ampersand(\Environment::get('request'), true) . '" id="tl_recipients_import" class="tl_form" method="post" enctype="multipart/form-data">
<div class="tl_formbody_edit">
<input type="hidden" name="FORM_SUBMIT" value="tl_recipients_import">
<input type="hidden" name="REQUEST_TOKEN" value="' . REQUEST_TOKEN . '">
<input type="hidden" name="MAX_FILE_SIZE" value="' . \Config::get('maxFileSize') . '">

<fieldset class="tl_tbox nolegend">
<div>
  <h3><label for="separator">' . $GLOBALS['TL_LANG']['MSC']['separator'][0] . '</label></h3>
  <select name="separator" id="separator" class="tl_select" onfocus="Backend.getScrollOffset()">
    <option value="comma">' . $GLOBALS['TL_LANG']['MSC']['comma'] . '</option>
    <option value="semicolon">' . $GLOBALS['TL_LANG']['MSC']['semicolon'] . '</option>
    <option value="tabulator">' . $GLOBALS['TL_LANG']['MSC']['tabulator'] . '</option>
    <option value="linebreak">' . $GLOBALS['TL_LANG']['MSC']['linebreak'] . '</option>
  </select>' . ($GLOBALS['TL_LANG']['MSC']['separator'][1] != '' ? '
  <p class="tl_help tl_tip">' . $GLOBALS['TL_LANG']['MSC']['separator'][1] . '</p>' : '') . '
  <h3>' . $GLOBALS['TL_LANG']['MSC']['source'][0] . '</h3>' . $objUploader->generateMarkup() . (isset($GLOBALS['TL_LANG']['MSC']['source'][1]) ? '
  <p class="tl_help tl_tip">' . $GLOBALS['TL_LANG']['MSC']['source'][1] . '</p>' : '') . '
</div>
</fieldset>

</div>

<div class="tl_formbody_submit">

<div class="tl_submit_container">
  <button type="submit" name="save" id="save" class="tl_submit" accesskey="s">' . $GLOBALS['TL_LANG']['tl_newsletter_recipients']['import'][0] . '</button>
</div>

</div>
</form>';
    }
All Usage Examples Of Contao\FileUpload::uploadTo