Jump to content

Delete file by button (unlink)


jhb

Recommended Posts

I have a download-list, and want to have a delete-button/link at each file. I have tried different codes, but i'm new to php and need some help.

 

 

if ($handle = opendir('files/engelsk/')) {               while (false !== ($file = readdir($handle)))                  {                      if ($file != "." && $file != "..")                  {                        $en .= '» <a href="/files/engelsk/'.$file.'">'.$file.'</a>  - <i><a href="#">Delete file</a></i><br />';                      }                   }              closedir($handle);              }

 

 

It's the "<a href="#">Delete file</a>" I want to be the delete-button.

 

Please help!

 

Link to comment
Share on other sites

 

$dir = "files/engelsk/";if (!$handle = opendir($dir)) {    echo "Could not open directory ".$dir;    exit;}while (false !== ($file = readdir($handle))) {    if ($file != "." && $file != "..") {        if(isset($_GET['rm'])) {            if($_GET['rm'] == $file) {                if(unlink($dir.$file)) {                    // File removed                    echo $dir.$file." was removed.";                } else {                    // Failed to remove file                    echo $dir.$file." could not be removed. Check your paths and permissions.";                }            }        } else {            $extension = end(explode('.', $file));            $filename_no_ext = substr($file, 0, -(strlen($extension) + 1));            $en .= '» <a href="/files/engelsk/'.$file.'">'.$filename_no_ext.'</a>  - <i><a href="?rm='.$file.'">Delete file</a></i><br />';        }    }}closedir($handle);

 

Untested.

Link to comment
Share on other sites

We are passing a variable to the script via the querystring, which is the portion of the url after "?" (see this). Notice that the href has become

 

<a href="?rm='.$file.'">Delete file</a>

 

When that's clicked, we'll go to the same page, but this time setting "rm" to equal the value of $file. Then, when we go through our loop we can check to see if "rm" is set.

 

if(isset($_GET['rm']))

 

If it is, then we want to know if its value exactly equals the value of the file we are currently iterating over. If it does

 

if($_GET['rm'] == $file)

 

We can unlink it. We check to see if unlink() is TRUE, because we know from checking unlink's return values that that indicates success.

 

if(unlink($dir.$file)) {
   // File removed
   echo $dir.$file." was removed.";
} else {
   // Failed to remove file
   echo $dir.$file." could not be removed. Check your paths and permissions.";
}

Link to comment
Share on other sites

The extension bit?

 

$extension = end(explode('.', $file));

explode() is making an array from the string, using a fullstop as the delimiter. So "foo.bar" would become

 

Array("foo", "bar")

 

end() selects the last value of that array, which in your case is the file extension.

 

$arr = array("foo", "bar");
$foo= end($arr);
// $foo = bar

The last line uses substr() to remove the number of characters in the extension from the end of $file, plus one to accommodate for the "."

 

$filename_no_ext = substr($file, 0, -(strlen($extension) + 1));

Link to comment
Share on other sites

I got one problem though; when i delete a file I would want a list of the remaining files below the message "example_file.txt was removed."

 

I have tried this, but it doesn't work:

            <?php
             $dir = "files/norsk/";
	if (!$handle = opendir($dir)) {
	    echo "Kunne ikke åpne mappen ".$dir;
	    exit;
	}
	while (false !== ($file = readdir($handle))) {
	    if ($file != "." && $file != "..") {
	        if(isset($_GET['rm'])) {
	            if($_GET['rm'] == $file) {
	                if(unlink($dir.$file)) {
	                    // File removed
	                   $no1 .= $dir.$file.' was removed.';
	                   
	                   $extension = end(explode('.', $file));
	                    $filename_no_ext = substr($file, 0, -(strlen($extension) + 1));
	                    $no .= '<img src="document-icon.png" /> '.$filename_no_ext.'   <a href="/files/norsk/'.$file.'"><img src="down-icon.png" /></a> <i><a href="?rm='.$file.'" style="color:grey;"><img src="delete-icon.png" /></a></i><br />';


	                } else {
	                    // Failed to remove file
	                    $no1 .= $dir.$file.' could not be removed. Check your paths and permissions.';
	                    
	                    $extension = end(explode('.', $file));
	                    $filename_no_ext = substr($file, 0, -(strlen($extension) + 1));
	                    $no .= '<img src="document-icon.png" /> '.$filename_no_ext.'   <a href="/files/norsk/'.$file.'"><img src="down-icon.png" /></a> <i><a href="?rm='.$file.'" style="color:grey;"><img src="delete-icon.png" /></a></i><br />';
	                }
	            }
	        } else {
	            $extension = end(explode('.', $file));
	            $filename_no_ext = substr($file, 0, -(strlen($extension) + 1));
	            $no .= '<img src="document-icon.png" /> '.$filename_no_ext.'   <a href="/files/norsk/'.$file.'"><img src="down-icon.png" /></a> <i><a href="?rm='.$file.'" style="color:grey;"><img src="delete-icon.png" /></a></i><br />';
	        }
	    }
	}
	closedir($handle);
            ?>
            <p style="color:grey;vertical-align:middle;"><?=$no1?></p>
            <p style="color:black;vertical-align:middle;"><?=$no?></p>

Link to comment
Share on other sites

Try this:

 

 

$dir = "files/engelsk/";$list = '';$info = '';if (!$handle = opendir($dir)) {    echo "Could not open directory ".$dir;    exit;}while (false !== ($file = readdir($handle))) {    if ($file != "." && $file != "..") {        if(isset($_GET['rm']) && $_GET['rm'] == $file) {            if(unlink($dir.$file)) {                // File removed                $info = $dir.$file." was removed.";            } else {                // Failed to remove file                $info = $dir.$file." could not be removed. Check your paths and permissions.";            }            continue;        }        $extension = end(explode('.', $file));        $filename_no_ext = substr($file, 0, -(strlen($extension) + 1));        $list .= '» <a href="/files/engelsk/'.$file.'">'.$filename_no_ext.'</a>  - <i><a href="?rm='.$file.'">Delete file</a></i><br />';    }}closedir($handle);// Outputif(!empty($info)) {    echo "<p>".$info."</p>\n";}if(!empty($list)) {    echo $list;} else {    echo "The directory is empty.";}

 

Previously, the part that added the list item was held in an else statement that only executed if $_GET['rm'] was not set. We just needed to rearrange the logic a little and add a continue to prevent a file that has just been removed still showing up in the list.

Link to comment
Share on other sites

Easy enough, but I think you might need to work this one out on your own. The PHP manual tells you everything about rename() that you need to know, and by passing a $_GET variable in the same way as we did with the delete function you'll be able to tell the script which file to rename. Good luck.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.