php - 如何在 PHP 中使用文本框值存储多个图像?
问题描述
我想要一个程序将多个图像的路径与 html 文本框值一起存储到数据库中。我已经尝试过,但我无法为多个图像存储相同的文本值。有人可以帮帮我吗!!
解决方案
您可以按照以下步骤使用相同的输入文本值上传多个图像。
第 1 步:创建数据库表
CREATE TABLE `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text_box_value` varchar(255) NOT NULL,
`file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`uploaded_on` datetime NOT NULL,
`status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
第 2 步:数据库配置 (dbConfig.php)
<?php
// Database configuration
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "your_db_name";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
第 3 步:文件上传表单 HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Multiple Image Upload</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Input Value:<br>
<input type="text" name="text_box_value"><br><br>
Select Image Files to Upload:<br>
<input type="file" name="files[]" multiple ><br><br>
<input type="submit" name="submit" value="UPLOAD">
</form>
</body>
</html>
第 4 步:在 PHP 中上传多个文件 (upload.php)
<?php
if(isset($_POST['submit'])){
// Include the database configuration file
include_once 'dbConfig.php';
// File upload configuration
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$text_box_value = $_POST['text_box_value'];
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['files']['name']))){
foreach($_FILES['files']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['files']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
$insertValuesSQL .= "('".$text_box_value."', '".$fileName."', NOW()),";
}else{
$errorUpload .= $_FILES['files']['name'][$key].', ';
}
}else{
$errorUploadType .= $_FILES['files']['name'][$key].', ';
}
}
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL,',');
// Insert image file name into database
$insert = $db->query("INSERT INTO images (text_box_value, file_name, uploaded_on) VALUES $insertValuesSQL");
if($insert){
$errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
$statusMsg = "Files are uploaded successfully.".$errorMsg;
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
}
?>
推荐阅读
- cmake - 如何防止 CMake 从 add_subdirectory() 包含的项目中安装()-ing 目标?
- node.js - 使用没有中间件功能的 multer 上传到 S3
- python - 熊猫计算范围之间每个值的出现次数
- reactjs - NotSupportedError:无法获取 ServiceWorkerRegistration 对象:用户拒绝使用 Service Worker 的权限
- ios - 无法在 Xcode 12 上启动 iOS 13.7 模拟器
- python - VIDIOC_G_STD:失败:不适当的 ioctl doer 设备
- r - rmarkdown 加密输出丢失情节和传单情节
- python - 玩家掷 2 个骰子,直到一个达到 100
- jquery - 未捕获的 SyntaxError:意外令牌:标识符
- sql - 参数化查询(Visual Studio)