Solved

How to delete a file?

Posted on 2009-07-08
16
226 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

740 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