我通过json获取php文件中的图片编码数据。我的要求是将图像存储在服务器中的一个文件夹中,前提是应为每个图像分配一个唯一的名称。我对如何将图像存储在具有唯一名称的文件夹中然后再次将图像的路径存储在数据库中产生了很多疑问。我看过几个 StackOverflow 问题和在线资源,但无法清楚地了解它们。对于从事 PHP 工作的人来说,这个问题似乎很简单。但是作为 php 的新手和 Android 开发人员,我无法理解那些不太详细的答案。因此,如果有人可以帮助我提供代码片段和解释,我将不胜感激。我试图用尽可能清晰的注释来解释问题和代码。如果有任何错误,请轻描淡写。以下是我尝试并在某些点卡住的代码。提前致谢..
<?php
$response = array();
// check for required fields
if (isset($_POST['mailid']) && isset($_POST['category']) && isset($_POST['description']) && isset($_POST['contactNum']) && isset($_POST['lookingto']) && isset($_POST['image'])) {
$usermail = $_POST['mailid'];
$category = $_POST['category'];
$description = $_POST['description'];
$contactNum = $_POST['contactNum'];
$lookingto = $_POST['lookingto'];
$base=$_POST['image'];
$binary=base64_decode($base);
$folder = "images/"; // This is my folder "images" in which pics have to be stored.
$file = fopen('storepic.jpg', 'wb'); // Here I have to give name dynamically to each pic provided that should be unique. Here I mentioned pic name as storepic.jpg, a static name.
fwrite($file, $binary);
fclose($file);
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO details(usermail, category, description, contactnumber,posting_for) VALUES('$usermail', '$category', '$description','$contactNum','$lookingto')");
//// Even after giving dynamic name how can we store the path of the dynamic named image into database in the above query. For that what should be done here..
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
// echoing JSON response
echo json_encode($response);
}
} else {
$response["success"] = 0;
// echoing JSON response
echo json_encode($response);
}
?>
即使在其他 php 文件中,我也通过选择查询检索数据。我能够获得我插入的正常数据,可以在 Android 客户端应用程序上获得它。但话又说回来如何从路径中获取图像,以便稍后转换为 base64 编码数据,然后作为 json 响应回显..
注意:-我的 UI 不是表单。这是安卓用户界面。。