我使用PHP通过XAMPP服务器创建了图像上传。但我需要 知道如何从Android删除上传的图像文件。我怎么能这样做?
php file(upload) :
<?PHP
if(isset($_POST['image'])){
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHisu');
$upload_folder = "upload/";
$path = "$upload_folder/$id.jpeg";
$image = $_POST['image'];
if(file_put_contents($path, base64_decode($image)) != false){
echo "uploaded_success";
exit;
}
else
{
echo "Sorry, your file is too large.";
echo "upload_failed";
exit;
}
}
else{
echo "image_not_in";
exit;
}
?>
答案 0 :(得分:0)
您可以在服务器的成功响应中获取照片路径并将其删除! 确保你在清单中声明权限!
<uses-permission> android:name="android.permission.WRITE_INTERNAL_STORAGE" />
在您的代码..
PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {
@Override
public void processFinish(String s) {
if (s.contains("uploaded_success")) {
File photoDelete = new File(selectedPhoto);
if (photoDelete.exists()) {
if (photoDelete.delete()) {
Log.d("DELETE", "deleted:" + selectedPhoto);
}
}
Toast.makeText(getApplicationContext(), "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Error while uploading...", Toast.LENGTH_SHORT).show();
}
}
});
答案 1 :(得分:0)
您可以通过从应用程序(客户端)向服务器发送命令从服务器中删除图像,要求服务器从本地存储中删除该文件。
假设您将图像文件的imageName设置为hashmap,以便通过以下方式发送到服务器:
HashMap<String, String> postData = new HashMap<String, String>();
postData.put("deleteImage", imageName);
并执行它:
task.execute("http://192.168.1.7/news/delete.php");
现在,您只需要查看deleteImage的值是否已设置(在您的服务器上,在delete.php文件中),并通过在PHP中调用unlink方法删除该文件:
<强> Delete.php 强>
<?PHP
if(isset($_POST['deleteImage'])){
$imageName = $_POST['deleteImage'];
unlink($imageName) //this deletes the image file
}
?>