0

我正在尝试将文件大小列添加到我的 mysqli 查询中,但我不确定在此循环中执行此操作的正确方法。我试过了$_FILES[file][size]。但我不希望这会奏效。谁能启发我实现这一目标的正确方法。谢谢

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

  if(move_uploaded_file($_FILES['file']['tmp_name'][$key],"test/$name"))
 {
  mysqli_query($db,"INSERT INTO files VALUES('$_GET[draft]','$name','$_FILES[file][size]')");
 }
}
4

1 回答 1

1

假设您的 HTML 代码中有

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="myFile1" />
    <input type="file" name="myFile2" />
    ....
    ...// other input and your submit input
</form>

你的$_FILES遗嘱结构是

Array
(
    [myFile1] => Array
        (
            [name] => XXX
            [type] => XXX
            [tmp_name] => XXX
            [error] => UPLOAD_ERR_OK  (= 0)
            [size] => XXXX
        )

    [myFile2] => Array
        (
            [name] => XXXX
            [type] => XXXX
            [tmp_name] => XXXX
            [error] => UPLOAD_ERR_OK
            [size] => XXXX
        )
    etc..
)

然后你可以做

// Loop on each entry of $_FILES
foreach($_FILES as $nameOfInputFile => $fileStructure) {

    // Check the error status first, skip the file if an error occured
    if ($fileStructure['error'] != UPLOAD_ERR_OK)
        continue;

    // $fileStructure contain the current file data
    if(move_uploaded_file($fileStructure['tmp_name'],"test/" . $fileStructure['name']))
    {
        mysqli_query($db,"INSERT INTO files VALUES('" . $_POST['draft'] . "','$name','" . $fileStructure['size'] . "')");
    }

}

注意:当您发布文件时,您的草稿值应使用$_POST

于 2013-03-13T22:40:51.350 回答