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
Solved

How to delete a file?

Posted on 2009-07-08
16
225 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

792 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