我正在尝试将文件大小列添加到我的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]')");
}
}
答案 0 :(得分: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