Jump to content

Direct listing - won't delete files.


cixdk

Recommended Posts

Spent a good 3-4 hours playing around still can't get this to work properly. I'm fairly new to php in general.

 

Purpose of code: Read files in a directory and display image icon, name, and a checkbox. The check box is to delete the images. It works fine except it doesn't delete any selected files, or give me an error. Any help would be.. well helpful :P

 

<?php
$path = "/home/cix/public_html/uploads";

$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Files in this Directory: <form enctype='multipart/form-data' action='list.php' method=POST><br/>";

while ($file = readdir($dir_handle))
{

if($file!="." && $file!="..")
echo "
<img src='http://cixdev.info/uploads/$file' height='50' width='50' alt='$file'/>
<a href='http://cixdev.info/uploads/$file'>http://cixdev.info/uploads/$file</a>
<input type=checkbox name=$file><br/>
";

}

echo "<input type=submit name='GO'></form>";

closedir($dir_handle);

if(isset($_POST[$file]))
{
$checkbox = $_POST[$file];
if($checkbox == on)
{
if(!unlink($file)) die("Couldn't Delete file");
}
}


?>

Link to comment
Share on other sites

What's this: $_POST[$file];

 

Where is $file set?

 

There's no point in sending a form when you don't know what the names of the fields are going to be. The values should be variable not the field names.

 

$file is set by the name of the file, so for the check boxes name=$file; make the name of checkbox the same name as the file? so if a check box was checked named corresponding  to a file you wish to delete, you would be passing the name of the file to delete $_POST[$file]

 

I am probably completely wrong though.

Link to comment
Share on other sites

$dir_handle = @opendir($path) or die("Unable to open $path");

 

This doesn't make sense. By using the '@' sign, you are telling it to skip over this function if there is an error, but then you are telling it to die if there is an error. You need to pick one - I would go with 'die'. The '@' sign is bad programming, as it allows you to skip over errors rather than fixing them. You really should pretty much never use it.

 

<form enctype='multipart/form-data' action='list.php' method=POST>

 

You need to enclose the method in quotes.

 

<input type=checkbox name=$file>

 

The type and name need to be enclosed in quotes.

 

<input type=submit name='GO'>

 

The type needs to be enclosed in quotes.

 

if(isset($_POST[$file]))

 

The only way this would work is if you enclose the 'if' in a while statement while reading through the directory again, as you did when echoing out the images in the first place. You will have to rethink this statement.

 

 

Link to comment
Share on other sites

So I re-worked it, and decided using $_get and a simple link would be a whole lot easier. Still accomplishes what I wanted too!

 

<?php
$file = $_GET['file'];
$upload_dir = './uploads/';
$file_path = $upload_dir . $file;
if(is_file($file_path)) {
unlink($file_path);
echo "File " . $file . " deleted.";
}


$path = "./uploads/";

$dir_handle = opendir($path) or die("Unable to open $path");

echo "Files in this Directory: <br/>";

while ($file = readdir($dir_handle))
{

if($file!="." && $file!="..")
{
echo "<img src='http://cixdev.info/uploads/$file' height='50' width='50' alt='$file'/>
<a href='http://cixdev.info/uploads/$file'>http://cixdev.info/uploads/$file</a>
<a href='http://cixdev.info/list.php?file=$file'>Delete</a><br>


";
}

}

closedir($dir_handle);
?>

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.