Deleting a file

Deleting a file

Simple file delete example using a form

Migrated forum post

I received an email about deleting a file and I'm posting part of my reply here. I may expand it to be a full tutorial.

The unlink() function deletes files in PHP. http://www.php.net/manual/en/function.unlink.php

You should have read/write privileges to the directory that you're going to be deleting from (chmod 777)

This is a simple example of using a form to delete a file.

Code:

<?php
if ($submit && $deleteimage) {

function
Delete_Photo($name){
$delete = unlink($name);
if(
$delete){
print
"$name has been deleted...";
}else{
echo
"Could not delete";
}
}

Delete_Photo($deleteimage);
}else{
?>

<p>
<form action="<? print $PHP_SELF; ?>" method="post">
<input class="content" type="text" name="deleteimage">
<input class="content" type="submit" name="submit" value="delete file">
</form>
<?
}
?>

[ This Message was edited by: Dakath on 2001-10-20 13:15 ]

      Subscribe in a reader