PHP多上传图片问题

时间:2012-07-19 15:51:45

标签: php file-upload

我正在尝试编写一个可以上传多个图像的脚本。这是我正在使用的代码。

if(isset($_POST['submit_images'])) {
    $i = 0;
    foreach($_FILES['file'] as $file) {
        $image = new Image();
        $image->member_id = $_POST['id'][$i];
        $image->image_type = "MedlemsBillede";
        $image->attach_file($_FILES['file']);
        if($image->save()) {
            $message = 'Billedet blev uploadet med succes.';
        } else {
            $message  = join("<br />", $image->errors);
        }
        $i++;
    }
}

我的问题在于对函数的调用:

$image->attach_file($_FILES['file']);

这个功能看起来像这样:

public function attach_file($file) {
    if(!$file || empty($file) || !is_array($file)) {
        $this->errors[] = "Der blev ikke uploadet nogen fil.";
        return false;
    } elseif($file['error'] != 0) {
        $this->errors[] = $this->upload_errors[$file['error']];
        return false;
    } else {
        $this->temp_path    = $file['tmp_name'];
        $this->file_name    = str_replace(' ', '_', basename($file['name']));
        $this->file_type    = $file['type'];
        $this->file_size    = $file['size'];
        $this->ts   = date('Y-m-d H:i:s');
        return true;
    }
}

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我认为问题出在这一行:

$image->attach_file($_FILES['file']);

你正在迭代$ _FILES ['file']而不仅仅是$ _FILES

你也是调用你在循环中迭代的数组

电话应该是这样的

$image->attach_file($file);

这可以帮助你