将文件插入 mysql 和目录,但只记录最后插入的文件

insert files into mysql and directory but only last inserted file is recorded

我有在数据库和目录中存储多个文件的输入。

<input id="attachment_file" name="attachment_file" type="file" value="<?=$_POST["attachment_file"]?>" class="form-control hidden" multiple />

如果有 2 个文件上传,我只是想将它们记录在数据库中并将它们移动到其他目录,但奇怪的是记录只是最后插入的文件... 这是一些代码:

for ($i=0; $i < count($_FILES['attachment_file']); $i++) { 
        
        $file_name = $_FILES["attachment_file"]["name"][$i];
        $tmp_name = $_FILES['attachment_file']['tmp_name'][$i];
        $docs_dir = F_FS_PATH_USER_PHOTOS."files/docs/";

        if(!is_dir($docs_dir)) {
            mkdir($docs_dir, 0777, true);
            chmod($docs_dir, 0777);
        }

        $new_dir = move_uploaded_file($tmp_name, $docs_dir.$file_name);
        $docs_url = F_WS_PATH_USER_PHOTOS."files/docs/".$_FILES["attachment_file"]["name"][$i];

        
     
        
        query("INSERT INTO orders_documents SET
        id = NULL,
        date_added = now(),
        order_id =".forsql($order_id).",
        doc_type =".forsql($_FILES["attachment_file"]["type"][$i]).",
        doc_url =".forsql($docs_url).",
        doc_id =".forsql('1').",
        doc_name =".forsql($_FILES["attachment_file"]["name"][$i]));
    }

我还尝试插入 3 个文件“random.pdf”、“alter_tables.txt”、“testfile.txt”,并简单地检查是否所有文件都存储在 $_FILE 变量中,例如:

print_r($_FILES);
        exit();

但我得到的答案是:

    Array
(
    [attachment_file] => Array
        (
            [name] => testfile.txt
            [type] => text/plain
            [tmp_name] => C:\xampp\tmp\phpB3DC.tmp
            [error] => 0
            [size] => 150
        )
)

就像我说的,我在数据库和目录中只记录了这个文件,但没有记录其他 2 个文件?

我尝试将 [] 添加到输入名称但没有任何改变? 我不知道我还应该尝试检查为什么只在 $_FILES

中存储最后一个文件

表格也有enctype="multipart/form-data"

inputname 属性必须表明您将收到一个数组。将其更改为 attachment_file[] 以允许将数组传回您的服务器。

<input id="attachment_file" name="attachment_file[]" type="file" value="<?=$_POST["attachment_file"]?>" class="form-control hidden" multiple />