我试图将我的图像保存到根文件夹内的文件夹和数据库中图像的名称。现在,我将如何检索特定id
的特定图像?
在我的代码中,我只能检索所有图片,但我只想在他的id
和image_name
这可能吗?
这是我的代码:
以下是将image
上传到文件夹,将image_name
上传到数据库的代码:
<?php
include '../session.php';
require_once 'config.php';
if (isset($_POST['submit'])) {
$img_dir = "../updated_photo/";
$target_file = $img_dir . basename($_FILES["image"]["name"]);
$imageName = $_FILES["image"]["name"];
$imageData = file_get_contents($_FILES["image"]["tmp_name"]);
$imageType = $_FILES["image"]["type"];
if (substr($imageType, 0,5) == "image") {
$query = "UPDATE `crew_info` SET `image_name` = ? WHERE `id` = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, 'si', $imageName, $_POST['id']);
mysqli_stmt_execute($stmt);
file_put_contents($target_file, $imageData);
$id = $_POST['id'];
header("Location: ../admin/view_all_info.php?id=$id");
}
else {
echo "Image not Uploaded!";
}
}
?>
以下是检索image
和image_name
<?php
include '../session.php';
require_once 'config.php';
if (isset($_REQUEST['id'])) {
$query = "SELECT `id`, `image_name` FROM `crew_info` WHERE `id` = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, 'i', $_REQUEST['id']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $image_name);
while (mysqli_stmt_fetch($stmt)) {
echo sprintf("%s", $image_name);
}
$img_dir = "../updated_photo/";
$images = glob($img_dir."*.jpg");
foreach ($images as $img) {
echo '<img src="'.$img.'" />';
}
}
else {
echo "No Image";
}
?>
在我的检索代码中,我尝试使用if statement
代替foreach
,如下面的代码
if (count($images) > 0) {
$img = $images[0];
echo '<img src="'.$img.'" />';
}
答案 0 :(得分:0)
此语句将从该目录中获取所有jpg照片
onChange={this.handleCheck.bind(this)}
如果您只想显示一张照片,可以在从数据库
获取image_name后执行此操作git copy source-path target-path