【发布时间】: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));
}
?>
这一次,这个显示图像路径而不是图像,我想做的是显示图像本身。我只是不知道在这里做什么正确。任何帮助都可以。提前致谢!
【问题讨论】: