我正在尝试使用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
//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']."' />";
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句