<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $file = $_FILES['file']; $allowext = array("gif", "jpg", "jpeg", "png"); $temp = explode(".", $file["name"]); $extension = strtolower(end($temp)); // 将文件扩展名转换为小写字母 if ($file['error'] !== UPLOAD_ERR_OK) { $result = array('code' => 1, 'msg' => '上传失败:' . $file['error']); echo json_encode($result); } else { $filesize = $file['size'] / 1024; // 将文件大小转换为 KB if ($filesize > 1024) { // 文件大小不能超过 1MB $result = array('code' => 2, 'msg' => '上传失败:文件大小不能超过 1MB'); echo json_encode($result); } else if (!in_array($extension, $allowext)) { $result = array('code' => 3, 'msg' => '上传失败:扩展名不支持,请选择 GIF、JPG、JPEG 或 PNG 文件'); echo json_encode($result); } else { $upload_dir = '/var/www/uploads/'; // 上传文件保存的目录 if (!file_exists($upload_dir)) { mkdir($upload_dir, 0777, true); // 如果目录不存在,则创建目录 } $filename = uniqid() . '.' . $extension; // 生成唯一的文件名 $filepath = $upload_dir . $filename; // 文件上传的目标路径 if (move_uploaded_file($file['tmp_name'], $filepath)) { // 将临时文件移动到目标路径 $result = array('code' => 0, 'msg' => '上传成功', 'url' => '/uploads/' . $filename); echo json_encode($result); // 记录上传日志 $log_file = '/var/www/logs/upload.log'; // 日志文件路径 $log_content = "[" . date("Y-m-d H:i:s") . "] " . $filename . " 上传成功\n"; if (!file_exists($log_file)) { // 如果日志文件不存在,则创建日志文件 touch($log_file); chmod($log_file, 0666); } file_put_contents($log_file, $log_content, FILE_APPEND); } else { $result = array('code' => 4, 'msg' => '上传失败:移动文件时出错'); echo json_encode($result); // 记录上传日志 $log_file = '/var/www/logs/upload.log'; // 日志文件路径 $log_content 完善这段php代码
时间: 2023-05-13 11:05:25 浏览: 132
这段 PHP 代码实现了一个文件上传功能,包括文件大小限制、文件类型限制、文件上传目录的创建和文件上传日志的记录。如果上传成功,会返回一个 JSON 格式的结果,包括上传成功的信息和文件的 URL。如果上传失败,会返回相应的错误信息。
需要注意的是,上传文件时需要确保上传目录的权限正确,否则会导致上传失败。另外,为了防止文件名冲突,这里使用了 uniqid() 函数生成唯一的文件名。
相关问题
<?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); } ?>无法正常上传
在你提供的代码中,我注意到你没有创建预处理语句 `$stmt` 对象。请确保在执行绑定参数之前创建预处理语句对象。
以下是修改后的代码示例:
```php
<?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++) {
// 创建预处理语句对象
$stmt = $conn->prepare("INSERT INTO tms (aoi_step, defect_type, layer_code, type, dpet, subcode, code_description, image1_path, image2_path, image3_path, image4_path, image5_path, determination_rule) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (!$stmt) {
die("预处理失败: " . $conn->error);
}
// 处理每个字段对应的图片
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;
}
// 关闭预处理语句
$stmt->close();
}
// 关闭数据库连接
mysqli_close($conn);
}
?>
```
在上述代码中,我们在循环内部创建了预处理语句对象 `$stmt`,并在循环结束后关闭了预处理语句。
请确保你的 HTML 表单中的图片字段在多行数据时也使用了数组形式,并且你的上传目录路径 `D:/phpstudy_pro/WWW/192.168.1.16/images` 是正确的。
如果问题仍然存在,请提供详细的错误信息,以便我可以更好地帮助你解决问题。
<?php // 连接数据库 $conn = mysqli_connect("localhost", "tms", "123456", "nut"); // 检查连接是否成功 if (!$conn) { die("数据库连接失败: " . mysqli_connect_error()); }echo "111"; // 处理表单提交 if ($_SERVER["REQUEST_METHOD"] == "POST") { // 获取表单数据 $aoiStep = $_POST['aoi_step']; $defectType = $_POST['defect_type']; $layerCode = $_POST['layer_code']; $type = $_POST['type']; $dpet = $_POST['dpet']; $subcode = $_POST['subcode']; $codeDescription = $_POST['code_description']; $determinationRule = $_POST['determination_rule']; $imagePaths = []; // 存储图片路径的数组 // 处理上传的图片 for ($i = 1; $i <= 5; $i++) { $imageField = "image" . $i; $targetDir = "D:/phpstudy_pro/WWW/192.168.1.16/images"; // 设置上传目录的路径 $fileName = uniqid() . '_' . $_FILES[$imageField]["name"]; // 生成唯一文件名 $targetFile = $targetDir . '/' . basename($fileName); // 将反斜杠替换为正斜杠 $targetFile = str_replace('\', '/', $targetFile); if (isset($_FILES[$imageField]) && $_FILES[$imageField]["error"] == UPLOAD_ERR_OK && move_uploaded_file($_FILES[$imageField]["tmp_name"], $targetFile)) { $imagePath = $targetFile; } else { $imagePath = ""; } $imagePaths[] = $imagePath; } // 其他图片的处理代码,类似上面的处理方式 $stmt = $conn->prepare("INSERT INTO tms (aoi_step, defect_type, layer_code, type, dpet, subcode, code_description, image1_path, image2_path, image3_path, image4_path, image5_path, determination_rule) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); if (!$stmt) { die("预处理失败: " . $conn->error); } $stmt->bind_param("sssssssssssss", $aoiStep, $defectType, $layerCode, $type, $dpet, $subcode, $codeDescription, $imagePaths[0], $imagePaths[1], $imagePaths[2], $imagePaths[3], $imagePaths[4], $determinationRule); if ($stmt->execute()) { echo "数据插入成功"; } else { echo "数据插入失败: " . $stmt->error; } // 关闭数据库连接 mysqli_close($conn); } ?>在这个代码中,当我有多行数据时,只会上传最后一条,我需要怎么修改,才能将所有编辑的数据都进行上传
在你的代码中,你只处理了一个表单数据,并且只上传了最后一条数据。如果你想上传所有编辑的数据,你需要做以下修改:
1. 在 HTML 表单中,将表单元素的名称改为数组形式,以便能够接收多个值。例如,将 `aoi_step` 改为 `aoi_step[]`,将 `defect_type` 改为 `defect_type[]`,以此类推。这样,当你选择多个选项时,它们会以数组的形式传递给服务器。
2. 在处理表单提交的代码中,使用循环来处理每个表单元素的值。你可以使用 `foreach` 循环来遍历每个表单元素的数组,并将它们插入到数据库中。
下面是修改后的代码示例:
```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 = 1; $i <= 5; $i++) {
$imageField = "image" . $i;
$targetDir = "D:/phpstudy_pro/WWW/192.168.1.16/images"; // 设置上传目录的路径
$fileName = uniqid() . '_' . $_FILES[$imageField]["name"]; // 生成唯一文件名
$targetFile = $targetDir . '/' . basename($fileName);
// 将反斜杠替换为正斜杠
$targetFile = str_replace('\\', '/', $targetFile);
if (isset($_FILES[$imageField]) && $_FILES[$imageField]["error"] == UPLOAD_ERR_OK && move_uploaded_file($_FILES[$imageField]["tmp_name"], $targetFile)) {
$imagePath = $targetFile;
} else {
$imagePath = "";
}
$imagePaths[] = $imagePath;
}
// 插入数据
$stmt = $conn->prepare("INSERT INTO tms (aoi_step, defect_type, layer_code, type, dpet, subcode, code_description, image1_path, image2_path, image3_path, image4_path, image5_path, determination_rule) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (!$stmt) {
die("预处理失败: " . $conn->error);
}
foreach ($aoiSteps as $key => $aoiStep) {
// 绑定参数
$stmt->bind_param("sssssssssssss", $aoiStep, $defectTypes[$key], $layerCodes[$key], $types[$key], $dpets[$key], $subcodes[$key], $codeDescriptions[$key], $imagePaths[0], $imagePaths[1], $imagePaths[2], $imagePaths[3], $imagePaths[4], $determinationRules[$key]);
if ($stmt->execute()) {
echo "数据插入成功";
} else {
echo "数据插入失败: " . $stmt->error;
}
}
// 关闭数据库连接
mysqli_close($conn);
}
```
在上述代码中,我们使用了 `foreach` 循环来遍历每个表单元素的数组,并将它们绑定到 SQL 语句中。这样,每个编辑的数据都会被插入到数据库中。请确保你的 HTML 表单中的每个表单元素都以数组的形式命名,以便能够接收多个值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)