Horde_Browser::wasFileUploaded PHP Method

wasFileUploaded() public method

Determines if the file was uploaded or not. If not, will return the appropriate error message.
public wasFileUploaded ( string $field, string $name = null )
$field string The name of the field containing the uploaded file.
$name string The file description string to use in the error message. Default: 'file'.
    public function wasFileUploaded($field, $name = null)
    {
        if (is_null($name)) {
            $name = 'file';
        }
        if (!($uploadSize = self::allowFileUploads())) {
            throw new Horde_Browser_Exception(Horde_Browser_Translation::t("File uploads not supported."));
        }
        /* Get any index on the field name. */
        $index = Horde_Array::getArrayParts($field, $base, $keys);
        if ($index) {
            /* Index present, fetch the error var to check. */
            $keys_path = array_merge(array($base, 'error'), $keys);
            $error = Horde_Array::getElement($_FILES, $keys_path);
            /* Index present, fetch the tmp_name var to check. */
            $keys_path = array_merge(array($base, 'tmp_name'), $keys);
            $tmp_name = Horde_Array::getElement($_FILES, $keys_path);
        } else {
            /* No index, simple set up of vars to check. */
            if (!isset($_FILES[$field])) {
                throw new Horde_Browser_Exception(Horde_Browser_Translation::t("No file uploaded"), UPLOAD_ERR_NO_FILE);
            }
            $error = $_FILES[$field]['error'];
            if (is_array($error)) {
                $error = reset($error);
            }
            $tmp_name = $_FILES[$field]['tmp_name'];
            if (is_array($tmp_name)) {
                $tmp_name = reset($tmp_name);
            }
        }
        if (empty($_FILES)) {
            $error = UPLOAD_ERR_NO_FILE;
        }
        switch ($error) {
            case UPLOAD_ERR_NO_FILE:
                throw new Horde_Browser_Exception(sprintf(Horde_Browser_Translation::t("There was a problem with the file upload: No %s was uploaded."), $name), UPLOAD_ERR_NO_FILE);
            case UPLOAD_ERR_OK:
                if (is_uploaded_file($tmp_name) && !filesize($tmp_name)) {
                    throw new Horde_Browser_Exception(Horde_Browser_Translation::t("The uploaded file appears to be empty. It may not exist on your computer."), UPLOAD_ERR_NO_FILE);
                }
                // SUCCESS
                break;
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
                throw new Horde_Browser_Exception(sprintf(Horde_Browser_Translation::t("There was a problem with the file upload: The %s was larger than the maximum allowed size (%d bytes)."), $name, $uploadSize), $error);
            case UPLOAD_ERR_PARTIAL:
                throw new Horde_Browser_Exception(sprintf(Horde_Browser_Translation::t("There was a problem with the file upload: The %s was only partially uploaded."), $name), $error);
            case UPLOAD_ERR_NO_TMP_DIR:
                throw new Horde_Browser_Exception(Horde_Browser_Translation::t("There was a problem with the file upload: The temporary folder used to store the upload data is missing."), $error);
            case UPLOAD_ERR_CANT_WRITE:
                // No reason to try to explain to user what a "PHP extension" is.
            // No reason to try to explain to user what a "PHP extension" is.
            case UPLOAD_ERR_EXTENSION:
                throw new Horde_Browser_Exception(Horde_Browser_Translation::t("There was a problem with the file upload: Can't write the uploaded data to the server."), $error);
        }
    }

Usage Example

Exemplo n.º 1
0
Arquivo: Base.php Projeto: horde/horde
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step.
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  */
 public function nextStep($action, array $param = array())
 {
     /* First step. */
     if (is_null($action)) {
         return Horde_Data::IMPORT_FILE;
     }
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             if (!isset($this->_browser)) {
                 throw new LogicException('Missing browser parameter.');
             }
             /* Sanitize uploaded file. */
             try {
                 $this->_browser->wasFileUploaded('import_file', $param['file_types'][$this->_vars->import_format]);
             } catch (Horde_Exception $e) {
                 throw new Horde_Data_Exception($e);
             }
             if ($_FILES['import_file']['size'] <= 0) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("The file contained no data."));
             }
             $this->storage->set('format', $this->_vars->import_format);
             break;
         case Horde_Data::IMPORT_MAPPED:
             if (!$this->_vars->dataKeys || !$this->_vars->appKeys) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("You didn\\'t map any fields from the imported file to the corresponding fields."));
             }
             $dataKeys = explode("\t", $this->_vars->dataKeys);
             $appKeys = explode("\t", $this->_vars->appKeys);
             $dates = $map = array();
             if (!($import_data = $this->storage->get('data'))) {
                 $import_data = array();
             }
             foreach ($appKeys as $key => $app) {
                 $map[$dataKeys[$key]] = $app;
                 if (isset($param['time_fields']) && isset($param['time_fields'][$app])) {
                     $dates[$dataKeys[$key]]['type'] = $param['time_fields'][$app];
                     $dates[$dataKeys[$key]]['values'] = array();
                     $i = 0;
                     /* Build an example array of up to 10 date/time fields. */
                     while ($i < count($import_data) && count($dates[$dataKeys[$key]]['values']) < 10) {
                         if (!empty($import_data[$i][$dataKeys[$key]])) {
                             $dates[$dataKeys[$key]]['values'][] = $import_data[$i][$dataKeys[$key]];
                         }
                         ++$i;
                     }
                 }
             }
             $this->storage->set('map', $map);
             if (count($dates) > 0) {
                 foreach ($dates as $key => $data) {
                     if (count($data['values'])) {
                         $this->storage->set('dates', $dates);
                         return Horde_Data::IMPORT_DATETIME;
                     }
                 }
             }
             return $this->nextStep(Horde_Data::IMPORT_DATA, $param);
         case Horde_Data::IMPORT_DATETIME:
         case Horde_Data::IMPORT_DATA:
             if ($action == Horde_Data::IMPORT_DATETIME) {
                 $params = array('delimiter' => $this->_vars->delimiter, 'format' => $this->_vars->format, 'order' => $this->_vars->order, 'day_delimiter' => $this->_vars->day_delimiter, 'day_format' => $this->_vars->day_format, 'time_delimiter' => $this->_vars->time_delimiter, 'time_format' => $this->_vars->time_format);
             }
             if (!$this->storage->exists('data')) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("The uploaded data was lost since the previous step."));
             }
             /* Build the result data set as an associative array. */
             $data = array();
             $data_map = $this->storage->get('map');
             foreach ($this->storage->get('data') as $row) {
                 $data_row = array();
                 foreach ($row as $key => $val) {
                     if (isset($data_map[$key])) {
                         $mapped_key = $data_map[$key];
                         if ($action == Horde_Data::IMPORT_DATETIME && !empty($val) && isset($param['time_fields']) && isset($param['time_fields'][$mapped_key])) {
                             $val = $this->_mapDate($val, $param['time_fields'][$mapped_key], $params, $key);
                         }
                         $data_row[$mapped_key] = $val;
                     }
                 }
                 $data[] = $data_row;
             }
             return $data;
     }
 }