PHP使用<input />多个属性上传文件

时间:2015-06-02 21:18:20

标签: php

我目前有几个<input type="file"字段用于上传图片。我想使用html multiple属性将输入字段转换为单个字段。

我的工作代码包含多个输入字段:

<input type="file" name="popup_images_1">
<input type="file" name="popup_images_2">
<input type="file" name="popup_images_3">...


for ($i = 1; $i <= 3; $i++) {
  if (is_uploaded_file($_FILES['popup_images_' . $i]['tmp_name'])) {
    $products_image = new upload('popup_images_' . $i);
    $products_image->set_destination(DIR_FS_CATALOG_IMAGES);

    if ($products_image->parse() && $products_image->save()) {
      $products_image_name = $products_image->filename;
      // do database stuff
    }
  }
}

我的新代码无效,只有一个输入字段:

<input type="file" name="popup_images[]" multiple="multiple">


$num_files = count($HTTP_POST_FILES['popup_images']['tmp_name']);
for ($i = 1; $i <= $num_files; $i++) {
  if (is_uploaded_file($HTTP_POST_FILES['popup_images']['tmp_name'][$i])) {
    $products_image = new upload('popup_images' . $i);
    $products_image->set_destination(DIR_FS_CATALOG_IMAGES);

    if ($products_image->parse() && $products_image->save()) {
      $products_image_name = $products_image->filename;
      // do database stuff
    }
  }
}

设置new upload('popup_images' . $i)的行似乎出错了。有什么想法吗?

这是upload类:

  class upload {
var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location;

function upload($file = '', $destination = '', $permissions = '777', $extensions = '') {
  $this->set_file($file);
  $this->set_destination($destination);
  $this->set_permissions($permissions);
  $this->set_extensions($extensions);

  $this->set_output_messages('direct');

  if (tep_not_null($this->file) && tep_not_null($this->destination)) {
    $this->set_output_messages('session');

    if ( ($this->parse() == true) && ($this->save() == true) ) {
      return true;
    } else {
      return false;
    }
  }
}

function parse() {
  global $HTTP_POST_FILES, $messageStack;

  $file = array();

  if (isset($_FILES[$this->file])) {
    $file = array('name' => $_FILES[$this->file]['name'],
                  'type' => $_FILES[$this->file]['type'],
                  'size' => $_FILES[$this->file]['size'],
                  'tmp_name' => $_FILES[$this->file]['tmp_name']);
  } elseif (isset($HTTP_POST_FILES[$this->file])) {
    $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
                  'type' => $HTTP_POST_FILES[$this->file]['type'],
                  'size' => $HTTP_POST_FILES[$this->file]['size'],
                  'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
  }

  if ( tep_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
    if (sizeof($this->extensions) > 0) {
      if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
        if ($this->message_location == 'direct') {
          $messageStack->add(ERROR_FILETYPE_NOT_ALLOWED, 'error');
        } else {
          $messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, 'error');
        }

        return false;
      }
    }

    $this->set_file($file);
    $this->set_filename($file['name']);
    $this->set_tmp_filename($file['tmp_name']);

    return $this->check_destination();
  } else {
    if ($this->message_location == 'direct') {
      $messageStack->add(WARNING_NO_FILE_UPLOADED, 'warning');
    } else {
      $messageStack->add_session(WARNING_NO_FILE_UPLOADED, 'warning');
    }

    return false;
  }
}

function save() {
  global $messageStack;

  if (substr($this->destination, -1) != '/') $this->destination .= '/';

  if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) {
    chmod($this->destination . $this->filename, $this->permissions);

    if ($this->message_location == 'direct') {
      $messageStack->add(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
    } else {
      $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
    }

    return true;
  } else {
    if ($this->message_location == 'direct') {
      $messageStack->add(ERROR_FILE_NOT_SAVED, 'error');
    } else {
      $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error');
    }

    return false;
  }
}

0 个答案:

没有答案