hi i need help deleting files via php,

i can delete files using php, but i want to be able to delete a file by typing in the name of the file.

the directory i want to delete a file from is "users/"

here is the unlink script i have so far

<?	
$myFile = "users/file.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
fclose($fh);


$myFile = "users/file.xml";
unlink($myFile);
?>

Recommended Answers

All 12 Replies

Get the user to type the name of the file in a text box and get that into the $myFile variable using $myFile=$_GET['<textbox_name>'] .

Check if the file exists on the server and delete it.

if (file_exists("users/".$myFile)){
  unlink("users/".$myFile);
} else {
  echo "The file does not exists";
}

However, it is difficult for the user to remember the names of all the files so using a textbox as a means of getting the filename is not very user friendly.

whats the actual whole script that i need to unlink a file that i type into a text field, because your script didnt work

whats the actual whole script that i need to unlink a file that i type into a text field, because your script didnt work

i keep getting this error unlink("users/".$myFile); is not a directiry

i keep getting this error unlink("users/".$myFile); is not a directiry

What is the value of $myFile? If the value is a blank it will give that error. I assumed you would add the validation in yourself

Try this code to check

if ($myFile!=""){
  if (file_exists("users/".$myFile)){
    unlink("users/".$myFile);
  } else {
    echo "The file does not exists";
  }
} else {
  echo "Please enter a filename";
}

try

<?php
if ($_GET['file']!=''){
  if (file_exists("users/".$_GET['file']."")){
    unlink("users/".$_GET['file'].""); }
else {print 'The file does not exists.';}
else {print 'Please enter a file name.';}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
File to delete: <input type="text" name="file" />
<input type="submit" value="Submit" />
</form>

Good luck :)
Calum

it didnt work it had a unexpected error on the second else function,

p.s thanks for trying

Did you add in the "}" that Calum left out on line 6.

Oops!

<?php
if ($_GET['file']!=''){
  if (file_exists("users/".$_GET['file']."")){
    unlink("users/".$_GET['file'].""); }
else {print 'The file does not exists.';}
}
else {print 'Please enter a file name.';}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
File to delete: <input type="text" name="file" />
<input type="submit" value="Submit" />
</form>

thanks mate it worked, now how do i do the same thing but instead of unlink i want to rename it to what i type into a textfield

Oops!

<?php
if ($_GET['file']!=''){
  if (file_exists("users/".$_GET['file']."")){
    unlink("users/".$_GET['file'].""); }
else {print 'The file does not exists.';}}
else {print 'Please enter a file name.';}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
File to delete: <input type="text" name="file" />
<input type="submit" value="Submit" />
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.