Hello,

I'm posting again regarding as to why my php code won't process the delete.php

Index.php:

<div id="DeleteFile">
Delete A Script:
<form action="delete.php">
</html>
<?
$path = "uploads/";
$handle = opendir($path);   
echo '<select>';     
while ($file = readdir($handle)) {
    if (substr($file,0,1) != ".") {
    echo "<option value ='$file'>$file</option>";
    }   
}
echo '</select>';
closedir($handle);
?>
<html>
<input type="submit" action="delete.php"/></form>
</div>
</div>
</body>
</html>

So thats my Index page, and I can only assume it works as it takes me to url.co.uk/delete.php?

However, It doesn't actually delete the file.

Delete.php:

<html><link href="stylesheet.css" rel="stylesheet" type="text/css">

<head>

</head>

<body>
</html>
<?php
function delete() {

    echo delete('$file');
    echo "The file $file has been deleted"; 
}
?>

Sorry for bothering you guys, thanks.

Recommended Answers

All 19 Replies

That's because in delete.php $file has no value, and although you define a function, it is never executed.

I was under the impression it would take this from a file previously loaded. So to make it automatically delete whats inside the drop down box would I need to put that PHP function within the index.php page and in action call the function rather than a page?

Thanks again,

Bradly

edit: So I just tried to make these changes after putting the delete.php snippet above the code:

<form action= delete();>
</html>
<?php
$path = "uploads/";
$handle = opendir($path);   
echo '<select>';     
while ($file = readdir($handle)) {
    if (substr($file,0,1) != ".") {
    echo "<option value ='$file'>$file</option>";
    }   
}
echo '</select>';
closedir($handle);
?>
<html>
<input type="Submit" value="Delete File" action='delete();'/></form>

I have a feeling I am doing it wrong haha...

First you check, is posted file exist or not

EXg:

$yourFile = "uploads/".$_POST['selectBoxName'];
$checkFile = fopen($yourFile, 'w') or die("can't open file");
fclose($checkFile);

If file exist, then

unlink($yourFile);

First off, you'll need to generate correct HTML code. Next, I suggest you look up how a form works. There are a lot of examples/tutorials to be found.

Hey there, you really need to learn this step by step.
The first page is the form page that allows the user to select the file that is to be deleted. This page will have a submit button which when clicked will redirect to the URL specified in the action="delete.php" of the form tag.
delete.php needs to be ready to catch the data posted with the form, in this case, the filename to delete. Then it needs to check if the file/path actually exists and respond appropriately.
Take it step by step, check each part is working before you move on to the next stage. If you are really trying and get stuck, post your problems here and people will help.

Thank you for your response everybody, I'm going through my code commenting and making it a bit easier to read at this minute. However I will cleanup the forms etc. Thats due to me chopping and dicing up what I had previously tweaked with and left debre about.

Ok, so I have improved on my code and done as you guys have said :)

I have looked up unlink and made a function which deletes a file when activated.

<?php
function delete() {
    $filename = './uploads/filetodelete.txt';
    if (@unlink($filename)) {
        echo 'File <strong>' .$filename. ' has successfully been deleted.';
        } else {
        echo 'Cannot Delete file';
        }
    }
?>

So the way I see it is $filename = '$file'; but as you guys said I have to carry the information sent from the form into delete.php, I guess this is where I am going to have issues. I've looked online and most places suggest using Mysql, which I would like to avoid.

Thanks in advance guys,

Sorry, I can't edit my previous post as I have been able to before. Basically I was wondering if I could just get a little input on what I've done.

So the form itself Creates a list of directories correctly and the delete.php deletes whatever I put in the $filename.

SO, I named the option name as 'filename'.

Index.php:

<form action="delete.php">
</html>
<?php
$path = "uploads/";
$handle = opendir($path);   

    echo '<select>';     
    while ($file = readdir($handle)) {
        if (substr($file,0,1) != ".") { 
        echo "<option name='filename' value ='$file'>$file</option>";
        }   
    }

echo '</select>';
$filename = $_POST['filename']; 
closedir($handle); // Closes the directory so it is quicker 

?>

<!-- This php is the form which grabs filenames from the directory -->

<html>
<input type="Submit" value="Delete File"/></form>

Delete.php:

<?php

$filename = $_GET['filename']; 

function delete() {
    $filename = './uploads/filetodelete.txt';
    if (@unlink($filename)) {
        echo 'File <strong>' .$filename. ' has successfully been deleted.';
        } else {
        echo 'Cannot Delete file';
        }
    }
?>

I also looked at Bachov's method. Where it takes the $_Post[''];

but its throwing an arror saying can't open file. Which I believe is because I'm not sending or receiving data accross pages properly?

Thanks for your patience everyone. I believe I am getting closer.

Index.php - fix your HTML tags, you close the page before you open it. You haven't named your select element and won't be able to access the selected file. Your form has no method.
Delete.php looks ok, but fix index before you worry about it.
You should write print_r($_POST); die(); at the top of delete.php and don't remove it until you see the filename as you want it to be sent to the function.

You need to learn how to build a form in HTML, then how to grab the data entered using PHP. This is the bread and milk of PHP, there are multitudes of tutorials online.
Good luck! :D

Hey adam, sorry for the misunderstanding about closing the page before you open it. Thats because it was a seperate part.

Here is the site: Cpanel.minepress.co.uk

Ok so I think I got it semi working :)

I did <select name="item"> which would be the area you were talking about and I also changed the form to Method="GET".

http://cpanel.minepress.co.uk/delete.php?item=employers-banner2.png

It now goes to delete.php?item=thefilenamehere

However I think the delete.php function is broken

as when I go there all I get is Array()

Which I think wrong because the array is meant to appear inside that?

Delete.php:

<html><link href="stylesheet.css" rel="stylesheet" type="text/css">

<head>

</head>

<body>
    <?print_r($_POST); die();?>
</html>

<?php
$filename = $_GET['filename']; 
    if (@unlink($filename)) {
        echo 'File <strong>' .$filename. ' has successfully been deleted.';
        } else {
        echo 'Cannot Delete file';
        }
?>

Haha, good to see you still at it :D
This one problem is simple, you added a 'get' method to the form, yet I told you to echo 'post', you can either change the form method to post, or the variable to $_GET.

Hey again! So I changed the form to Post method :P Which I have to say that was a huge dumb mistake on my end :)

Now whenever I go to http://cpanel.minepress.co.uk/delete.php from the index it posts the filename inside the array.. YAY :D

However its stopped putting ?item= at the end of it AND its not actually deleting the file.

URL:
http://cpanel.minepress.co.uk/delete.php

Contents:
Array ( [item] => employers-banner2.png )

It stopped putting your variables into the URL when you changed from get to post. Have a look at the code that declares $filename, the problem is clear :D

I've been doing this to long -.- haha,

I'm completely lost as to why the $filename declare is a problem? I also think I have it slightly messed up again.

$filename = $_GET['file']; 

is now 

    $filename = $_POST['file']; 

But I thought on the delete.php page it should be GET because it is grabbing the data that index.php is POST ing?

Forgive me for these stupid mishaps haha, I'm determined to get this working

There are two methods available with a HTML form, "get" and "post". If the form is using get method, the variables will be in the $_GET array for PHP to use, but they will also be in the URL in &name=value pairs. If the form is "post" method, the variables will be in the $_POST array and not in the URL.

I changed them all to get so that it was appearing in the URL.

I have just realised Delete.php I made a function. But I'm not actually calling to use the function am I?

<html><link href="stylesheet.css" rel="stylesheet" type="text/css">

    <?php
        $filename = $_GET['filename']; 
    function delete() {
        if (@unlink($filename)) {
            echo 'File <strong>' .$filename. ' has successfully been deleted.';
            } else {
            echo 'Cannot Delete file';
            }
        }
delete();        
echo'<br>';
      print_r($_GET);
?>

edit:

So It's not actually finding the file?

So if I'm getting this right, your saying I haven't

  1. Got the function laid out correctly
  2. Got the variables being defined as to whether or not they are local/global/static?

edit:

Warning: unlink() [function.unlink]: No such file or directory in /home/xtrapsp/public_html/Cpanel/delete.php on line 17



    <?php

$filename = $_GET['filename'];  


 function delete() {
   global  $filename;

        if (unlink($filename)) {
            echo 'File <strong>' .$filename. ' has successfully been deleted.';
            } else {
            echo 'Cannot Delete file';
            }
        }
    delete();        
    echo'<br>';
          print_r($_GET);

    ?>

Fixed it :D Thanks very much for all of your help everybody!

Hey that's great! Be aware that $_GET is a global by default, so you could reference it in your function, also you could have passed the variable to the function (prefered method) -

function delete($file){//everytime you want function to access variable... 
    if (unlink($file)){//it must referenced by the variable in the function declaration - ($file).
    //do functionary things
    }
}

and call the function with the variable passed:

$filename = $_GET['filename'];//define $filename
delete($filename);//send $filename to function.

You could also have passed $_GET['filename'] to the function -

delete($_GET['filename']);

Well done :D

commented: Helpful :) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.