使用 PHP 和 MySQL 上传图像文件并将其存储在数据库中
Upload and Store Image File in Database with description using PHP and MySQL
我一直在尝试将图像上传到带有图像描述的数据库,然后从数据库中获取数据并将描述放在图像中我是新手 php 我无法在互联网上找到信息我已上传到数据库并显示我只是无法让描述正常工作感谢您的帮助
index.php
<form action="upload.php" method="post" enctype="multipart/form-data">
Select Image File to Upload:
<input type="file" name="file">
<br><input type="text" name="description" placeholder="description of your image">
<input type="submit" name="submit" value="Upload">
</form>
upload.php
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$statusMsg = '';
// File upload path
$targetDir = "upload/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif','pdf');
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert image file name into database
$insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
if($insert){
$statusMsg = "The file ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
?>
您的代码没有将 description
保存到数据库中。这就是为什么你不能显示它。
首先您需要在 table images
.
中添加一个新列 description
之后,您必须像 $fileName
一样获取请求中发送的描述
$description = $_POST['description'];
您现在可以插入 images
:
$insert = $db->query("INSERT INTO images (file_name, description, uploaded_on) VALUES ('".$fileName."', '".$description."', NOW())");
我一直在尝试将图像上传到带有图像描述的数据库,然后从数据库中获取数据并将描述放在图像中我是新手 php 我无法在互联网上找到信息我已上传到数据库并显示我只是无法让描述正常工作感谢您的帮助
index.php
<form action="upload.php" method="post" enctype="multipart/form-data">
Select Image File to Upload:
<input type="file" name="file">
<br><input type="text" name="description" placeholder="description of your image">
<input type="submit" name="submit" value="Upload">
</form>
upload.php
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$statusMsg = '';
// File upload path
$targetDir = "upload/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif','pdf');
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert image file name into database
$insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
if($insert){
$statusMsg = "The file ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
?>
您的代码没有将 description
保存到数据库中。这就是为什么你不能显示它。
首先您需要在 table images
.
description
之后,您必须像 $fileName
$description = $_POST['description'];
您现在可以插入 images
:
$insert = $db->query("INSERT INTO images (file_name, description, uploaded_on) VALUES ('".$fileName."', '".$description."', NOW())");