Removing image files from the server

I wonder if anyone can help me.

I put together this page , which allows users to view their uploaded images in gallery format.

Now I want to add deletion functionality to each image. I created a button and Javascript behind it, but I'm really not sure how to associate a β€œclick on a button” with the actual physical deletion of the file.

Images are not stored in the database, but are in two folders on my server, in the following structure:

UploadedFiles/username/locationid/imagename and

UploadedFiles/username/locationid/Thumbnails/imagename

I am relatively new to PHP, and now I am reaching the limits of my knowledge, but of course I want to learn. From the documentation I read, I think I'm right in saying that the unlink method is the right command to use?

But it's hard for me to find the code to find folders with username and locationid folders that match the current username and locationid , and then delete the files that match the $source variable name.

I just wondered if anyone could possibly give some recommendations on how I can do this.

Many thanks and good wishes

+7
source share
2 answers

If you can pass the username, locationid and imagename variables to your script, you can delete the files using unlink() :

 $path = 'UploadedFiles/' . $username . '/' . $locationid . '/'; unlink( $path . $imagename ); unlink( $path . 'Thumbnails/' . $imagename ); 

Since you interact with your file system, you need to be sure and misinform the variables (prevent someone from using .. /../../ to get into undesirable parts of your file system).

 $username = str_replace( array( '..', '/', '\\', ':' ), '', $username ); $imagename = str_replace( array( '..', '/', '\\', ':' ), '', $imagename ); $locationid= str_replace( array( '..', '/', '\\', ':' ), '', $locationid ); 
+4
source

Obviously, your javascript (on the client side) will have to call the URL (on the server side) to remove the image selected by the user. I suggest that at the moment you are doing this statically (if later you want to move on to something more dynamic, the step of becoming ajax in a rather small size.

As Set Sail Media said, you will need to transfer the username and location identifier to your server when you click the delete button. One way to do this:

When rendering your gallery in HTML / javascript for each image, you have one under it that will contain the necessary information, and the submit button will simply cause the script to be deleted from your server. An example of a form you can do is:

  <form name="deleteImageX" method="GET" target="your.server.org/deleteImage.php"> <input type="hidden" name="userName" value="theUserNameAssociatedWithThePicture" /> <input type="hidden" name="locationId" value="locationOfThePicture" /> <input type="submit" value="delete"/> </form> 

This form will save the required value in hidden fields that will not be displayed on the web page, but will still be sent to the server when you click the submit button.

(for a little history, the method used here is GET, because HTML AFAIK does not support the DELETE method (which would be suitable in our case)).

The GET method will call the script "your.server.org/deleteImage.php". In this script, you will have all the necessary information (username / locationId) to delete the image using the variables $ _GET ['username'] and $ _GET ['locationId']. Then, as you mentioned, you will need to use the unlink method to actually delete the file from the server.

Finally, once this is done, you need to redirect the php script so that after deleting the image you will again display the gallery (for example). This can be done by calling the script if you have some kind of template engine or the php header function is called.

I hope this thread was what you expected, and I hope it was useful.

Yours faithfully,

Bear

0
source

All Articles