About Me

Friday 20 April 2012

PERL - Deleting Files

PERL - Deleting Files

Use the unlink function to delete specific files from your web server. The best solution is often to set a variable name equal to the URL of the file you wish to delete.

PERL Code:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #The header
$file = "newtext.txt";
if (unlink($file) == 0) {
    print "File deleted successfully.";
} else {
    print "File was not deleted.";
}

deletefiles.pl:

File deleted successfully.

PERL - Removing Multiple Files at Once

Multiple files can be removed at once if we first create an array of files to be deleted and then loop through each one. There are several ways to go about this process.

PERL Code:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #The header
@files = ("newtext.txt","moretext.txt","yetmoretext.txt");
foreach $file (@files) {
    unlink($file);
}

0 comments:

Post a Comment