Skip to main content
ElizabethGailLittle
Inspiring
January 17, 2021
Answered

Deleting a php file

  • January 17, 2021
  • 3 replies
  • 9191 views

I have a process which asks for a selected folder and then displays the files in the folder.  I have a DEL option by each file name, & when selected, go to another page passing the folder name and the file name.  Display file name & ask for confirmation.  All ok so far.  But when I confirm the delete using unlink, I get "no such file or directory in ....public_html/FileDel.php.  I've tried using unlink(realpath($fname)), & everything I can think of.  Nothing I've tried works.  I've also tried using an ftp connection & ftp_delete.  No success.  Any suggestions will be appreciated!

This topic has been closed for replies.
Correct answer ElizabethGailLittle

Yes.  And I've tried unlink(realpath($fname));  Do you have any idea why there would be a space added to the end of the name?


FINALLY!  Tracked down the space added at the end of the name & changed code.  Delete using unlink($fullname) WORKS!!!

 

Thanks for all your comments and for being so patient!

3 replies

Legend
January 18, 2021

Well you probably need to use a php SESSION to retain the filename.

 

Let's work through this:

 

1) You have gotten as far as fetching the file names from your specific folder onto a page as a list, using something like the php code below? (You don't really need the folder name, but I'll pass it to the deleteFile.php page anyway.

 

<?php

// assign the name of your folder to the $directory php variable
$directory = "products";
// Scan directory for files
$files = scandir($directory);

// loop through the $files array and echo file name and delete link
foreach($files as $fileName) {
echo "<p>$fileName</p>";
echo "<a href='deleteFile.php?folder=products&fileName=$fileName'>Delete</a>";
}
?>

 

Now in the deleteFile.php page start a php SESSION:

 

<?php
// Start the session
session_start();
?>

 

 

Then add the following php code where you like: If you want to confirm the deletion of a file then you have to pass that information back to a page, in this case the same page where the file to delete was listed, that is the deleteFile.php page. You can hard code your folder name in the php $unlink variable, as shown below "products" in this case.

 

<?php
$action = $_GET['action'];
if($action === 'confirmDelete') {
$fileNameToDelete = $_GET['fileNameToDelete'];
$unlink = unlink("products/".$fileNameToDelete);
echo $fileNameToDelete." Has been deleted";
}
else {
$fileName = $_GET['fileName'];

echo $fileName;
$_SESSION['fileName'] = $_GET['fileName'];
echo "<a href='deleteFile.php?action=confirmDelete&fileNameToDelete=".$_SESSION['fileName']."'>Confirm Delete File?</a>";
}
?>

 

 

Style it how you want when you have it working.

 

 

EDITED:

 

Infact, having tried this out after writing the code you don't need the php SESSION at all - you could just go with the following in the deleteFile.php page:

 

 

<?php
$action = $_GET['action'];
if($action === 'confirmDelete') {
$fileNameToDelete = $_GET['fileNameToDelete'];
$unlink = unlink("products/".$fileNameToDelete);
echo $fileNameToDelete." Has been deleted";
}
else {
$fileName = $_GET['fileName'];
echo $fileName;
echo "<a href='deleteFile.php?action=confirmDelete&fileNameToDelete=".$fileName."'>Delete File</a>";
}
?>

ElizabethGailLittle
Inspiring
January 20, 2021

Thanks!  That's not quite the approach I took, but it's close.

 

I displayed the file name & used a Cancel button & a Confirm button.  

When the page opens, I retrieve the path & filename passed from the directory display page.

If the confirm button is selected, I perform the delete.

 

if (isset($_GET['fname']) && ($_GET['fname'] != ''))
{
$fname=$_GET['fname'];
}
if (isset($_GET['fpath']))
{
$fpath = $_GET['fpath'];
if ($fpath == './') {
$fpath = '';
$fullname=$fname;
} else {
$fullname = $fpath.'/'.$fname;
}
}
if (isset($_POST['confirm'])) {
echo $fullname.'<br>';
$OK = unlink($fullname);
$done="Y";
}

 

In the body I checked for done & OK.  If both, display success.  If done but not OK, display failure.

I used the echo before the unlink command only to troubleshoot.  The system error message let me know there was a trailing blank in the filename which was the problem.

 

I appreciate your help.

Legend
January 20, 2021

There's literally dozens of ways to achieve exactly the same result. In the end it all comes down to what you find the simplest and easiest to understand and adapt. There's no right or wrong.

 

Glad you got your issue sorted in the end and found a way.

BenPleysier
Community Expert
Community Expert
January 18, 2021

As the message states, the file is not in the directory that you have specified in the unlink statement. Check the path.

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Nancy OShea
Community Expert
Community Expert
January 17, 2021

Deleting file from what exactly -- the database, the server, your computer's hard drive, something else? 

 

Nancy O'Shea— Product User & Community Expert
ElizabethGailLittle
Inspiring
January 17, 2021

The server.

Nancy OShea
Community Expert
Community Expert
January 17, 2021
Nancy O'Shea— Product User & Community Expert