php $_files 循环,php – 你如何循环$_FILES数组?

您的示例表单应该正常工作.只是你期望$_FILES超级全局的结构与实际使用的字段名称使用数组结构不同.

此多维数组的结构如下:

$_FILES[fieldname] => array(

[name] => array( /* these arrays are the size you expect */ )

[type] => array( /* these arrays are the size you expect */ )

[tmp_name] => array( /* these arrays are the size you expect */ )

[error] => array( /* these arrays are the size you expect */ )

[size] => array( /* these arrays are the size you expect */ )

);

Forfor count($_FILES [“fieldname”])将产生5.

但是,计数更深的维度也不会产生您可能期望的结果.例如计数count($_FILES [“fieldname”] [“tmp_name”])的字段将始终导致文件字段的数量,而不是实际上传的文件数.您仍然必须循环遍历元素,以确定是否已为特定文件字段上传任何内容.

编辑

因此,要循环遍历字段,您将执行以下操作:

// !empty( $_FILES ) is an extra safety precaution

// in case the form's enctype="multipart/form-data" attribute is missing

// or in case your form doesn't have any file field elements

if( strtolower( $_SERVER[ 'REQUEST_METHOD' ] ) == 'post' && !empty( $_FILES ) )

{

foreach( $_FILES[ 'image' ][ 'tmp_name' ] as $index => $tmpName )

{

if( !empty( $_FILES[ 'image' ][ 'error' ][ $index ] ) )

{

// some error occured with the file in index $index

// yield an error here

return false; // return false also immediately perhaps??

}

/*

edit: the following is not necessary actually as it is now

defined in the foreach statement ($index => $tmpName)

// extract the temporary location

$tmpName = $_FILES[ 'image' ][ 'tmp_name' ][ $index ];

*/

// check whether it's not empty, and whether it indeed is an uploaded file

if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )

{

// the path to the actual uploaded file is in $_FILES[ 'image' ][ 'tmp_name' ][ $index ]

// do something with it:

move_uploaded_file( $tmpName, $someDestinationPath ); // move to new location perhaps?

}

}

}

有关更多信息,请参阅the docs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php // 连接数据库 $conn = mysqli_connect("localhost", "tms", "123456", "nut"); // 检查连接是否成功 if (!$conn) { die("数据库连接失败: " . mysqli_connect_error()); } // 处理表单提交 if ($_SERVER["REQUEST_METHOD"] == "POST") { // 获取表单数据 $aoiSteps = $_POST['aoi_step']; $defectTypes = $_POST['defect_type']; $layerCodes = $_POST['layer_code']; $types = $_POST['type']; $dpets = $_POST['dpet']; $subcodes = $_POST['subcode']; $codeDescriptions = $_POST['code_description']; $determinationRules = $_POST['determination_rule']; $imagePaths = []; // 存储图片路径的数组 // 处理上传的图片 for ($i = 0; $i < count($aoiSteps); $i++) { $imagePaths = []; // 存储图片路径的数组 // 处理每个字段对应的图片 for ($j = 1; $j <= 5; $j++) { $imageField = "image" . $j; $targetDir = "D:/phpstudy_pro/WWW/192.168.1.16/images"; // 设置上传目录的路径 $fileName = uniqid() . '_' . $_FILES[$imageField]["name"][$i]; // 生成唯一文件名 $targetFile = $targetDir . '/' . basename($fileName); // 将反斜杠替换为正斜杠 $targetFile = str_replace('\\', '/', $targetFile); if (isset($_FILES[$imageField]["tmp_name"][$i]) && $_FILES[$imageField]["error"][$i] == UPLOAD_ERR_OK && move_uploaded_file($_FILES[$imageField]["tmp_name"][$i], $targetFile)) { $imagePath = $targetFile; } else { $imagePath = ""; } $imagePaths[] = $imagePath; } // 绑定参数 $stmt->bind_param("sssssssssssss", $aoiSteps[$i], $defectTypes[$i], $layerCodes[$i], $types[$i], $dpets[$i], $subcodes[$i], $codeDescriptions[$i], $imagePaths[0], $imagePaths[1], $imagePaths[2], $imagePaths[3], $imagePaths[4], $determinationRules[$i]); if ($stmt->execute()) { echo "数据插入成功"; } else { echo "数据插入失败: " . $stmt->error; } } // 关闭数据库连接 mysqli_close($conn); } ?>无法正常上传
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值