Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2012, 05:50 PM   PM User | #1
markbaron15
New Coder

 
Join Date: May 2012
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
markbaron15 is an unknown quantity at this point
Deleting files using checkbox and PHP

Hi,

I got a problem on my code, im doing a file deletion using checkbox and PHP where I got everything i needed it to work(almost). but somehow it doesn't work.

Here is the code
from viewfiles.php
PHP Code:
<?php 
include 'include/session.php';
include 
'templates/include/header.php';
include 
'include/menubar.php';

include(
"config.php");
include(
"dl_functions.php");

if(isset(
$POST['submit'])){
    foreach (
$_POST['file'] as $file){
        if(isset(
$file)) {
            if (
unlink($file)) { 
            } 
        }
    }
}

if (isset(
$_GET["file"])) do_download($_GET["file"]);

?>
<div id="container">

<div id="content">
<br />
<a href="index.php">Back</a>
<br />
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table class="main_table" align="center" style="width: auto;">
  <tr>
    <td colspan="1"><b>Download files list</b></td>
    <td colspan="1"><b>Size</b></td>
    <td colspan="1"><b>Select</b></td>
  </tr>
  <?php echo get_download_files_list(); ?>
</table>
<button type="submit" name="submit">Delete selected</button>
<button type="reset" name="reset">Reset</button>
</form>

<br />
</div>
<?php include 'templates/include/footer.php'?>
</div>
and from dl_functions.php
PHP Code:
<?
function get_download_files_list()
{
 global 
$download_dirs$SLINE;
    
$files_list ""
    
$i 0;
    
$from_path dirname(__FILE__)."/".$download_dirs;
    
    if (
is_dir($from_path)) {
        
chdir($from_path);
        
$handle opendir('.');
        while ((
$file readdir($handle)) !== false)
        {
            if ((
$file != ".") && ($file != "..") && is_file($file)) {
                
                
$files_list .= '<tr><td width="75%"><a href="viewfiles.php?'.$SLINE.'&file='.htmlentities(urlencode(basename($file))).'">'.basename($file).'</a></td><td>'.set_size(filesize($file)).'</td><td><input type="checkbox" name="file[]" value="'.htmlentities(urlencode(basename($file))).'"></tr>';
            }
        }
        
closedir($handle); 
    }
 return 
$files_list;
}

?>

the problem is I'm confused on which problem i have on this, if it is about the wrong value on the checkbox, or if i got a wrong markup on this.

Please help,

Mark B.

Last edited by markbaron15; 05-02-2012 at 05:52 PM..
markbaron15 is offline   Reply With Quote
Old 05-02-2012, 07:29 PM   PM User | #2
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Check your paths for the unlink statement. If you're sending 'myfile.txt', but the file is located in 'user_files/myfile.txt', unlink will not be able to delete. Probably best to give an absolute path on an unlink command...

PHP Code:
unlink('/home/httpd/vhosts/mydomain.com/www/user_files/myfile.txt'); 
Also - VERY IMPORTANT - you should probably clean the input data before trying to delete a file, in case someone posts '/etc/passwd' to your system!
bcarl314 is offline   Reply With Quote
Users who have thanked bcarl314 for this post:
markbaron15 (05-04-2012)
Old 05-02-2012, 08:35 PM   PM User | #3
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
You are looking for a value for $_POST['submit'] to see if it's set (isset).

But you didn't define a value ...


<button type="submit" name="submit" value="submit" >Delete selected</button>
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
markbaron15 (05-04-2012)
Old 05-03-2012, 01:30 PM   PM User | #4
markbaron15
New Coder

 
Join Date: May 2012
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
markbaron15 is an unknown quantity at this point
Quote:
Originally Posted by bcarl314 View Post
Check your paths for the unlink statement. If you're sending 'myfile.txt', but the file is located in 'user_files/myfile.txt', unlink will not be able to delete. Probably best to give an absolute path on an unlink command...

PHP Code:
unlink('/home/httpd/vhosts/mydomain.com/www/user_files/myfile.txt'); 
Also - VERY IMPORTANT - you should probably clean the input data before trying to delete a file, in case someone posts '/etc/passwd' to your system!
i changed the unlink statement, into

PHP Code:
if(isset($POST['submit'])){
    foreach (
$_POST['file'] as $file){
        if(isset(
$file)) {
            if (
file_exists($file)){
                (
unlink(dirname(__FILE__)."/".$download_dirs.$file)); 
            }else{
                echo 
"Cannot delete file";
            }
        }
    }

this is my first time dealing with chechboxes and deleting files using unlink() function as a PHP newbie, sooooo challenging
markbaron15 is offline   Reply With Quote
Old 05-03-2012, 01:35 PM   PM User | #5
markbaron15
New Coder

 
Join Date: May 2012
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
markbaron15 is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
You are looking for a value for $_POST['submit'] to see if it's set (isset).

But you didn't define a value ...


<button type="submit" name="submit" value="submit" >Delete selected</button>
Already changed the value of the submit button, but nothing happens, i think the problem is on how i handle the checkbox values and the unlink function,.

Just a PHP newbie XD

here i attached a screenshot of the page,

@iBall- The <a> tag is for the download feature where users can download the file when they click it. hmmm if it is right to use it...
Attached Thumbnails
Click image for larger version

Name:	ddd.jpg
Views:	20
Size:	46.1 KB
ID:	11131  
markbaron15 is offline   Reply With Quote
Old 05-03-2012, 08:48 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Just for the heck of it ... see what the values are ....



if(isset($POST['submit'])){
foreach ($_POST['file'] as $file){
echo "File: ".$file."<br />";
if(isset($file)) {
if (file_exists($file)){
(unlink(dirname(__FILE__)."/".$download_dirs.$file));
}else{
echo "Cannot delete file";
}
}
}
}
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
markbaron15 (05-04-2012)
Old 05-04-2012, 07:50 AM   PM User | #7
markbaron15
New Coder

 
Join Date: May 2012
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
markbaron15 is an unknown quantity at this point
Oh guys many thanks, problem is solve, i manage to understand on the logical if-else statement on my isset(),

here is the code
PHP Code:

    $from_path 
dirname(__FILE__)."/".$download_dirs;
    
    if (
is_dir($from_path)) {
        
chdir($from_path);
        
$handle opendir('.');
        while ((
$file readdir($handle)) !== false)
        {
            if ((
$file != ".") && ($file != "..") && is_file($file)) {
                if(isset(
$_POST['deleteMe']) && count($_POST['deleteMe']) > 0){
                    
unlink($from_path.$file);
                    unset(
$_POST['deleteMe']);
                }
            }
        }
    } 
@iBall, thanks to your basic example, haha. many thanks. P.S. your foreach ($files as $filename) shows an invalid argument on my xampp server, dunno why? i'm using PHP 5+. BTW thanks again.
markbaron15 is offline   Reply With Quote
Old 05-05-2012, 05:36 AM   PM User | #8
markbaron15
New Coder

 
Join Date: May 2012
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
markbaron15 is an unknown quantity at this point
Quote:
Originally Posted by iBall View Post

Also, you don't need the unset() inside the while loop. It should be after all the files have been deleted (below the while loop)

Ahh, yeah you're right, i found that unset gives some unexpected behavior like even only one checkbox is clicked, the submit will delete all.

I just need to add the foreach() and remove the unset(). Thanks!

PHP Code:
$from_path dirname(__FILE__)."/".$download_dirs;
    
if (
is_dir($from_path)) {
    
chdir($from_path);
    
$handle opendir('.');
    while ((
$file readdir($handle)) !== false)
    {
        if ((
$file != ".") && ($file != "..") && is_file($file)) {
            if(isset(
$_POST['deleteMe']) && count($_POST['deleteMe']) > 0){
                foreach(
$_POST['deleteMe'] as $file){
                    if(
file_exists($from_path.$file)){
                        
unlink($from_path.$file);
                    }
                }
            }

        }
        
    }
    
//unset($_POST['deleteMe']);

it works fine now.

Last edited by markbaron15; 05-05-2012 at 05:39 AM..
markbaron15 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:53 PM.


Advertisement
Log in to turn off these ads.