Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to delete a file?

Posted on 2009-07-08
16
Medium Priority
?
237 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

824 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