Forum Moderators: coopster

Message Too Old, No Replies

unlink function

         

Mark1978

6:00 pm on Jun 13, 2010 (gmt 0)

10+ Year Member



Hi,

I need help with the following code below. I have created the following scripts below, that scans a folder and lists the contents into a drop down box. I then want to able to delete the selected file via a delete button.


<h2>Delete existing File</h2>
<p><select name="id"></p>
<option value="">-- Choose a file --</option>
<?php
echo "<form>";
//Looks into the directory and returns the files, no subdirectories
echo "<select name='yourfiles'>";

//The path to the file directory
$dirpath = "uploads";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {

//Don't list subdirectories
if (!is_dir("$dirpath/$file")) {

//Truncate the file extension and capitalize the first letter
echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';}

}

echo "</select></p>";
echo '<p><input type="submit" value="Delete" class="submit" />';
echo "</form>";

closedir($dh);

?>


delete.php

<?php
$dirpath = "uploads";
$file_to_delete = $_POST['yourfiles']; //as named in your <select>
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
}
else {
echo "Error.";
}
?>


The error I receive is: "Notice: Undefined index: yourfiles on line 6" (delete.php);

Can anyone help me as to why I am getting this error or is my code not right for what I want to do.

Thanks

Tommybs

6:56 pm on Jun 13, 2010 (gmt 0)

10+ Year Member



Did you copy and paste the above or did you hand write it? If you hand wrote it, check the name of the select and make sure it is yourfiles in both. Your form doesn't have an action set to be delete.php either so I don't see how you're posting the data to delete.php.

Is it possible to see the code that executes when the form is submitted?

Matthew1980

7:58 pm on Jun 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Mark1978,

The undefined index refers to the fact that the $_POST global key hasn't been set or assigned the right name when in use in the php processing file, ie they don't match. Use print_r($_POST); to see what is being set/sent when the form is submitted - that should set you in the right direction.

In the processing file you could do with some error/process handling, what you will need to do is something like this:-
(Typed on the fly but should be fine ;))
<?php
error_reporting(E_ALL);//remove this line when going live with the code/site
if(isset($_POST['yourfiles']) && !empty($_POST['yourfiles'])){
//process data here as successful post
}else{
//redirect back to form as error has occurred
}
?>

As Tommybs asks though, posting the form code could be helpful here too :)

Cheers,
MRb

[edited by: Matthew1980 at 8:19 pm (utc) on Jun 13, 2010]

rocknbil

8:05 pm on Jun 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The error I receive is: "Notice: Undefined index: yourfiles on line 6" (delete.php);


Did you set the form method to post?

<form action="whatever.php" method="post">

Try this and see what it says. Also use the FULL PATH to the file, a relative path **might** work but this disambiguates a lot of stuff.


<?php
$dirpath = $_SERVER['DOCUMENT_ROOT'].'/'."uploads";
if (isset($_POST['yourfiles'])) {
$file_to_delete = $_POST['yourfiles'];
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
}
else {
echo "Delete Failed";
}
}
else {
echo "<p>No post variable found.</p>"
foreach ($_POST as $key => $value) {
echo "<p>key: $key value $value<p>";
}
?>


Hope this isn't a public script, horribly insecure . . .