如何从特定人员的文件夹中检索图像?

时间:2016-06-03 03:57:34

标签: php image mysqli

我试图将我的图像保存到根文件夹内的文件夹和数据库中图像的名称。现在,我将如何检索特定id的特定图像?

在我的代码中,我只能检索所有图片,但我只想在他的idimage_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!";

        }

}

?>

以下是检索imageimage_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.'" />';

} 

1 个答案:

答案 0 :(得分:0)

此语句将从该目录中获取所有jpg照片

onChange={this.handleCheck.bind(this)}

如果您只想显示一张照片,可以在从数据库

获取image_name后执行此操作
git copy source-path target-path