Solved

How to delete a file?

Posted on 2009-07-08
16
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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