Solved

How to delete a file?

Posted on 2009-07-08
16
222 Views
Last Modified: 2012-05-07
I'm trying to delete a file from a button on my page and am not having much luck. Can someone help me out with what I'm doing wrong?

Essentially this page when loaded shows a list of files in the directory ../temp/ I want the user to be able to delete any of the files in the ../temp/ directory and then have it reload.

NG,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<?php

   // user has clicked a delete hyperlink

   if($_GET['action'] && $_GET['action'] == 'delete') {

       unlink($_GET['filedelete']);

       header("Location:files.php");

       exit();

   }

?>
 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

</head>
 

<body>

<?php $path = "../temp/" . $_GET['ID'] . "/";

	$dir = dir($path);

	while($file = $dir->read())

	{

	 if($file != '.' && $file != '..')
 

echo '<a href="' . $path . $file . '">' . $file . '</a> &nbsp;&nbsp;&nbsp; <input type="button" value="Delete" name=filedelete onClick="if(confirm(Are you sure?)){ window.location=\adm_file_list.php?delete=' .$path . $file. '\'; }"><br/>';
 

}?>
 

</body>

</html>

Open in new window

0
Comment
Question by:nike_golf
  • 7
  • 6
  • 3
16 Comments
 
LVL 5

Accepted Solution

by:
kingofnines earned 500 total points
ID: 24802669
You are trying to delete your button...

unlink($_GET['filedelete']);
<input type="button" value="Delete" name=filedelete

You need to delete the file...

unlink($path . $file);


Alan
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24802682
I also don't see where you are getting $_GET['action'] from.


Alan
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24803118
try this
$filename = $_FILES['filedelete']['name'];
$deletedir = "../temp/";
unlink($deletedir . $filename);
0
 
LVL 13

Author Comment

by:nike_golf
ID: 24803141
hmmm. yes your correct about trying to delete the button... duh.

I notice that I don't get prompted with the message "Are you sure?" so I don't think its coded correctly elsewise.

NG,
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24803142
fsze... filedelete is of type "button", there wont be any $_FILE[] data returned.


Alan
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24803153
The lack of your "Are you sure" will be a javascript issue, something I cant help much with. SOrry.


Alan
0
 
LVL 13

Author Comment

by:nike_golf
ID: 24803216
Is there a better way to do this?

NG,
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24803280
onClick="if(confirm(Are you sure?)){ window.location=\adm_file_list.php?delete=' .$path . $file. '\'; }"
is there something wrong?
onClick="if (confirm(Are you sure?)) { window.location.href='\adm_file_list.php?delete=<%echo $path . $file %>' } "
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Author Comment

by:nike_golf
ID: 24803355
fsze88,

Why the 2 different scenarios?

0
 
LVL 13

Author Comment

by:nike_golf
ID: 24803473
I get -

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in .../adm_file_list.php on line 24

NG,
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24803493
my mistake, forget it

0
 
LVL 13

Author Comment

by:nike_golf
ID: 24812253
How about a slightly different spin using php and no javascript?

The while statement contains the if statement to eliminate "." and ".." from the file list but they are showing up, any ideas onhow to get rid of them?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<?php
if(isset($_GET['delete']) && $_GET['delete']=='true') {
        print_r(unlink("../temp/" . $_GET['ID'] .
"/".$_POST['filedelete']));
        exit;
   //unlink("../claims/" . $_GET['ID'] . "/".$_POST['filedelete']);
   header('location: adm_file_list.php'); die();
}
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>

<body>
<?php $path = "../temp/" . $_GET['ID'] . "/";
        $dir = dir($path);
        while($file = $dir->read())
        {
         if($file != '.' && $file != '..')

echo "<form method=\"post\" name=\"filedelete\" action="?><?php echo
$_SERVER['PHP_SELF'].'?delete=true';?><?php echo" >
         <a href= $path$file >$file</a>
         <input type=\"submit\" value=\"Delete\"><br/>";
}
?>

</body>
</html>
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24812472
replace...

 if($file != '.' && $file != '..')

with...

 if(isfile($file))



Alan
0
 
LVL 13

Author Comment

by:nike_golf
ID: 24812504
I found I was missing some brackets so I was able to solve this... sorry for the noise.

NG,
0
 
LVL 5

Expert Comment

by:kingofnines
ID: 24812548
The original question was...

"I'm trying to delete a file from a button on my page and am not having much luck. Can someone help me out with what I'm doing wrong?"

Not only did I answer the question, the original poster acknowledged that I answered it...

"hmmm. yes your correct about trying to delete the button... duh."

Just because he then asked a second question that he solved himself does not mean he can now ask to cancel the original question.


Alan
0
 
LVL 13

Author Comment

by:nike_golf
ID: 24813992
You are correct kingofnines - my appologies.

NG,
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 33
add a 1000 ms delay after each sending email operation 12 54
Not needed 13 58
php variable basic question 12 29
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now