简体   繁体   English

Php 多图上传器,带 class

[英]Php multiple image uploader with class

What am i doing wrong?我究竟做错了什么? I cannot send multiple files, only one.我不能发送多个文件,只能发送一个。 I cannot pass an array multiple files into a function.我无法将数组多个文件传递到 function。

Uploading files to the server and saving into sql database - it works.将文件上传到服务器并保存到 sql 数据库 - 它可以工作。

form形式

<input type="file" name="product_images[]"

new.php new.php

$photo_multi = new Photographmultiple();

$id = $database->insert_id;

$files = 'product_images';

$photo_multi->upload_file($files, $id);

class Photographmultiple class 照片多张

public function upload_file($field_name, $id) {

    if(isset($_FILES[$field_name])) {

        foreach ($_FILES[$field_name]['name'] as $key => $val) {

            $image = ($_FILES[$field_name]['name'][$key]);
            $target_path = $this->upload_dir . '/' . $image;
            move_uploaded_file($_FILES[$field_name]['tmp_name'][$key], $target_path);

            $sql = "INSERT INTO " . self::$table_name . " (";
            $sql .= "product_id, product_images";
            $sql .= ") VALUES ('";
            $sql .= self::$database->escape_string($id) . "', '";
            $sql .= self::$database->escape_string($image) . "')";
            //echo $sql;
            $result = self::$database->query($sql);
            return $result;
        }
    }

}

I have removed from public function upload_file return $result;我已从public function upload_file return $result;中删除and everything works, I can send now multiple files.一切正常,我现在可以发送多个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM