3

I created image uploading through a XAMPP server using PHP. But I need to know how to delete the uploaded image file from Android. How can I do that?

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;


}
?>
  • Your question need some improvment ... what have you tried to delete a file in java. There is way to many lines of code just for a deletion so I am not going to read those. First , try to delete a file, then think about when you can execute this method. Plus, the title and the really short explanation are not the same, you want to delete from android or server ? There is only the android tag so I guess only Android – AxelH Dec 28 '16 at 06:25
  • Possible duplicate of [Delete file from internal storage](http://stackoverflow.com/questions/5486529/delete-file-from-internal-storage) – AxelH Dec 28 '16 at 06:25
  • i want to create android app.that support both image uploading & image delete.i already created image uploading.so i need image deleting from server. –  Dec 28 '16 at 08:34
  • Explain _deleting from server_. Is it a way to confirm to the app that it can delete the file from SD card or internal storage, is it to delete the file ON the server. Since you didn't add the PHP tag, I doubt you want the last one. And the code you post don't explain what you have tried. Just posting the code is brutal and not helpful. What is wrong with it ? (All that explanation should be added in the question, not in comment ;) ) – AxelH Dec 28 '16 at 08:39
  • i need to delete already uploaded image from ON the server(Eg.Xampp server).it's not brutal.i'm new one in stackoverflow.so i don't know how to use this.it's innocent.@AxelH –  Dec 28 '16 at 10:36
  • If I can give you an advise, you should edit your question to add the [tag:php] tag. Then, I don't see the need of the Android code here. Since you are able to call a php page, you will only need a solution to delete a File in Php based on a parameter. But you are able to create a file so what is the problem to delete one. (by brutal I mean that posting 200lines of unnecessary code is not helpful and discourage some of us, I didn't read the code) – AxelH Dec 28 '16 at 11:38
  • ok.sorry for misunderstanding.thanks for advice next time i update my question method. –  Dec 28 '16 at 14:07
  • check it again.@AxelH –  Dec 28 '16 at 14:09

2 Answers2

1

You can get the photo path and delete it in the successful response from the server! make sure you declare permission in the manifest!

<uses-permission> android:name="android.permission.WRITE_INTERNAL_STORAGE" />

in you code..

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();
            }

        }
    });
Sadiq Md Asif
  • 882
  • 6
  • 18
  • friend this code delete already existing image can be replaced.but i want to delete image from server.that image also available in server.@Sadiq Md Asif –  Dec 28 '16 at 05:13
0

You can delete an image from your server by sending a command from your app (client) to the server asking the server to delete the file from its local storage.

Let's say you set the imageName of your image file to to hashmap to send to the server by:

HashMap<String, String> postData = new HashMap<String, String>();
postData.put("deleteImage", imageName);

and execute it:

task.execute("http://192.168.1.7/news/delete.php");

Now, you just need to see if the value of the deleteImage is set or not (on your server, in the delete.php file) by and delete the file by calling unlink method in PHP:

Delete.php

<?PHP
   if(isset($_POST['deleteImage'])){
      $imageName = $_POST['deleteImage'];
      unlink($imageName) //this deletes the image file
   }
?>
Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
  • friend thanks.i under stand the process.but i don't know the how use in coding.can you make changing use my code above please –  Dec 28 '16 at 05:10
  • Sorry, you need to apply this yourself. If you found this answer satisfying, you can choose it as your answer so that others can be helped too. – Nilesh Singh Dec 28 '16 at 05:11