• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Deleting a php file

Participant ,
Jan 17, 2021 Jan 17, 2021

Copy link to clipboard

Copied

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!

TOPICS
Code , Error , How to , Server side applications

Views

382

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Jan 19, 2021 Jan 19, 2021

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!

Votes

Translate

Translate
Community Expert ,
Jan 17, 2021 Jan 17, 2021

Copy link to clipboard

Copied

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

 

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 17, 2021 Jan 17, 2021

Copy link to clipboard

Copied

The server.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 17, 2021 Jan 17, 2021

Copy link to clipboard

Copied

See this:

https://www.geeksforgeeks.org/how-to-delete-a-file-using-php/

 

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 18, 2021 Jan 18, 2021

Copy link to clipboard

Copied

Yes, I've used unlink and also tried php_delete. I just wrote a long response to help which included screen shots, but it refused to authenticate when I tried to POST.  At any rate, when I select the file to delete, we go to FileDel.php where the path & file are displayed and there are options to cancel or confirm.

 

When confirmed, the unlink operation is called, and a message is displayed to inform user whether or not file was deleted successfully.  I have error msgs enabled, so I get a system message saying Warning: unlink(includes/error_log 😞 No such file or directory in /home1/website/public_html/FileDel.php on line 24 (unlink).  Please notice there is a SPACE at the end of the filename in the error message, and I can't find out why!  includes is a folder within public_html, by the way.

 

Thanks to all for the suggestions.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2021 Jan 18, 2021

Copy link to clipboard

Copied

Did you try an absolute URL instead?

https : //  yoursite . com / folder / filename.php

 

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

Is there a way to post screen shots here?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

When I try https://website.com/folder as the path, it fails the following test:

if (is_dir($fpath) && (is_readable($fpath))) {

So I never even get to the display page.  I CAN use /home1/site/public_html/folder as path, the files are displayed, I can select one for deletion, and on the delete page

/home1/site/public_html/folder/filename is displayed.  But when unlink is called, I get the same old system error message.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 19, 2021 Jan 19, 2021

Copy link to clipboard

Copied

In case anyone else ever struggles with something like this, in the call to FileDel.php, I had:

<a href="FileDel.php?fname=<?php echo $fname[$i];?> &fpath=<?php echo

$fpath;?>" >DEL</a>

instead of:

<a href="FileDel.php?fname=<?php echo $fname[$i];?>&fpath=<?php echo

$fpath;>" >DEL</a>

 

I can't tell you how many times I looked at the code without seeing the space!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 18, 2021 Jan 18, 2021

Copy link to clipboard

Copied

I typed a space and ) where the previous message shows the emoticon.  I have no idea where that came from!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2021 Jan 18, 2021

Copy link to clipboard

Copied

The community platform changes ascii to emoticons 🙂 

When posting code here, use the insert code </> icon.

 

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 17, 2021 Jan 17, 2021

Copy link to clipboard

Copied

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

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 17, 2021 Jan 17, 2021

Copy link to clipboard

Copied

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>";
}
?>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 20, 2021 Jan 20, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 20, 2021 Jan 20, 2021

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines