0

Is there a way to remove files from the filemanager through php? What I mean is, you can upload files to the server and specify the folder you want it in using an upload algorithm, however I have not seen any way to reverse the proccess, can I delete those uploaded files through php code? And if so can anyone point me in the right direction to get started learning how?

4
Contributors
8
Replies
11
Views
4 Years
Discussion Span
Last Post by GraficRegret
0

@GraficRegret

What I mean is, you can upload files to the server and specify the folder you want it in using an upload algorithm, however I have not seen any way to reverse the proccess, can I delete those uploaded files through php code?

You can try this example:

http://www.tech-recipes.com/rx/1490/php_delete_or_unlink_a_file_on_the_server/

There's a few other ways you can delete file from server you can try these:

<?php
$tmpfile = "file.php";
if (filetype($tmpfile)) {
    unlink($tmpfile); 
} 
?>

-

<?php
$base_directory = '../www/';
if(unlink($base_directory.$_GET['file']))
    echo "File Deleted.";
?>

You have to be very careful when you used this $unlink function.

0

@GraficRegret

I will look into all that, thanks, why the need to be careful with unlink?

You can by accidently or easily delete the wrong file if that file is very important and all of the sudden you delete that file the whole website will no function.

That's why I mention careful meaning cautious.

Edited by LastMitch: grammer

0

So you have to make sure that you are taking out the right file thanks

Edited by GraficRegret

0

LastMitch is precisely correct, unlink is something you cannot reverse. For safety precaution, you need to test it on dummy files first, before allowing the script to work on real uploaded files. The use of unlink() with glob() is even dangerous than just using regular dir php function.

example of unlink codes you must avoid...I have seen this type of codes somewhere, but don't remember the site

## dangerous script and should not be used, unless it is certain that items within the directory must be deleted.

array_map('unlink', glob("/directoryName/*"));

The code above will delete everything in the directoryName .

One safe method of deleting file is to check if the file exist, and then execute unlink. For example

    $this_file = 'somefile.jpg';
    if (file_exists($this_file)) {
        unlink($this_file);
        echo $this_file .'   Has been deleted ';

    }
    else{

    echo 'This file: '. $this_file .' Does not exist. ';

    }

You can also use javascript to provide a confirmaton, before deleting the file.

0

Ok Thank you both I will he'd your warnings and will be extremely careful, the files I use when I build my pages are always dummie files. However I see the importance of making sure the files exist first I will probably use your method of checking for the file before deleting it vee.

0

Agree with everything above. As an additional 'brake', you could hijack the 'delete' link or button action and force a js confirm box. Then ajaxify the delete process. This doesn't necessarily make it safer, just give you pause to think 'did I press the right button/link?'

0

Ok thanks for the input D sorry I forgot to mark it solved earlier

Edited by GraficRegret

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.