Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Image Files From Server

I wonder whether someone may be able to help me please.

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

I'm now wanting to add the delete functionality to each image. I've created the button and the Javascript behind it, but I'm really not sure how to link the 'button click' with the actual physical deletion of the file.

The images aren't stored in a database but are in two folder locations on my server, in the following structure:

UploadedFiles/username/locationid/imagename and

UploadedFiles/username/locationid/Thumbnails/imagename

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

But what I'm finding really difficult is telling the code to find the folders with the username and locationid folders which match the current username and locationid and then delete the files which match the $source name variable.

I just wondered whether someone could perhaps provide some guidance please on how I may go about this.

Many thanks and kind regards

like image 539
IRHM Avatar asked May 08 '12 13:05

IRHM


People also ask

How do I permanently delete files from server?

Go to the File Manager, Select your domain where you want to delete the files and click Go. Navigate though your files to locate the file you want to delete. Right click the file and select Delete from the pop up menu. Confirm the file is deleted.

How do I delete a file from an FTP server?

Type help to list all available commands that you can run on an ftp server. Type ls to list all files and directories. Type cd <folder_name> to get into a specified folder. type mdelete <filename> to delete a file or multiple files or rmdir -r <folder_name> to delete a folder.


1 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 );

Because you are interacting with your file system, you'll want to be sure and sanitize the variables (prevent someone from using ../../../ to get to unwanted parts of your file system).

$username = str_replace( array( '..', '/', '\\', ':' ), '', $username );
$imagename = str_replace( array( '..', '/', '\\', ':' ), '', $imagename );
$locationid= str_replace( array( '..', '/', '\\', ':' ), '', $locationid );
like image 122
Patrick Moore Avatar answered Sep 29 '22 08:09

Patrick Moore