【问题标题】:How to retrieve image file paths from mysql database and display the images using php如何从 mysql 数据库中检索图像文件路径并使用 php 显示图像
【发布时间】:2014-02-28 11:12:53
【问题描述】:

我正在尝试使用 php 从 mysql 数据库中检索图像文件路径,并将其显示为一个表单,然后用于将图像发送到我们的 android 应用程序。我到处搜索,找到了一个看起来像我的问题但尚未解决的问题,因此我正在尝试寻找其他帮助。

这是我上传图片的 php 表单: inSTAGram.php

<?php

require('admin.config.inc.php');

if(isset($_POST['upload'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];

$path = "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name";

if($image_name==''){
echo "Don't just click! select an image please .";
exit();
}
else{
move_uploaded_file($image_tmp_name, $path);
$mysql_path = $path."/".$image_name;
$query = "INSERT INTO `inSTAGram`(`image_name`,`path`) VALUES ('$image_name','$mysql_path')";

$query_params = array(
    ':image_name' => $image_name,
    ':mysql_path' => $path,
    );

//execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage());

    //or just use this use this one:
    $response["success"] = 0;
    $response["message"] = "Database Error. Couldn't Upload Image!";
    die(json_encode($response));
}

$response["success"] = 1;
$response["message"] = "Image Uploaded Succesfully!";
echo json_encode($response);
}
}   
?>

<form action="inSTAGram.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" >
<input type="submit" name="upload" value="Upload" >
</form>

这个效果刚刚好!

所以这是我用于显示图像的 php 表单; inSTAGramDisplay.php

<?php
require("admin.config.inc.php");

//initial query
$query = "Select * FROM inSTAGram";

try {
$stmt   = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Image Available!";
$response["images"]   = array();

foreach ($rows as $row) {
    $rows             = array();
    $rows['image'] = 'http://www.stagconnect.com/StagConnect/admin/pictures?image_id=' . $rows['image_id'];
    $rows[] = $rows;        

    //update our repsonse JSON data
    array_push($response["images"], $image);
}

// echoing JSON response
echo json_encode($rows);


} else {
$response["success"] = 0;
$response["message"] = "No Image Available!";
die(json_encode($response));
}

?>

这一次,这个显示图像路径而不是图像,我想做的是显示图像本身。我只是不知道在这里做什么正确。任何帮助都可以。提前致谢!

【问题讨论】:

    标签: php android mysql image


    【解决方案1】:

    只需回显一个图像标签并将图像变量作为它的源属性:

    <?php
    //I hard coded the array but pull the images id from the db
    $rows  = array();
    $rows = array("070415togepi.jpg",
                  "1384904_681730675185076_523601772_n.jpg",
                  "1388517_681730668518410_508160046_n.jpg",
                  "1394889_681730678518409_1155435015_n.jpg", 
                  "385-jirachi-g.jpg"); 
    
    foreach ($rows as $value){
     $rows['img'] = 'http://www.stagconnect.com/StagConnect/admin/pictures/'.$value;
     echo "<img src='".$rows['img']."' />";
    }
    
    ?>
    

    【讨论】:

    • 嗨!我刚刚做了你上面所说的,你完全从数据库中取出了数据。如果我没记错的话,对于大量图像,您必须将其存储在文件系统中并将路径存储在数据库中,这就是我所做的。如果我错了,请纠正我。您的建议中的问题是,它显示了路径本身而不是图像。还有其他建议或帮助吗?
    • 谢谢!那成功了。但我只想问你,插入数据库的数据将来自用户,这意味着图像将不断增加。我想知道是否需要将所有图像文件名编码到数组中才能正常工作?
    • @IamMe。是的,在检索插入的数据时,请确保使用图像名称填充数组,在您的情况下,该字段是 image_name。如果它始终是相同的路径,那么您可以像我上面所做的那样将路径放入循环中
    【解决方案2】:

    如果不对其进行测试,我会说您的问题之一可能是您需要附上图像的位置:

    $rows['image'] = 'url(http://www.stagconnect.com/StagConnect/admin/pictures?image_id=' . $rows['image_id'] . ')';
    

    试一试。

    【讨论】:

    • 嗨!我试过你上面所说的,但我只是显示了整个网址。仍然不是图像。但无论如何谢谢!如果您有其他意见或建议,请再次告诉我。 :)
    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2018-11-08
    • 2014-10-02
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多