0

I have a problem when my uploads folder has been deleted after I delete directory in that uploads folder with this code below

function delete_files($target,$target_thumb) {

if(is_dir($target)){
    $files = glob( $target . '*', GLOB_MARK ); 

    foreach( $files as $file )
    {
        delete_files( $file );      
    }

    rmdir( $target );
} elseif(is_file($target)) {
    unlink( $target );  
}

 if(is_dir($target_thumb)){
    $files = glob( $target_thumb . '*', GLOB_MARK ); 

    foreach( $files as $file )
    {
        delete_files( $file );      
    }

    rmdir( $target_thumb );
} elseif(is_file($target_thumb)) {
    unlink( $target_thumb );  
}
}

All I want to do is to delete category and files in it from uploads folder.. What it does, it deletes all uploads folder with all files in it.. Have no idea why. Any suggestions would be helpful.

EDIT 1:

I see.. $target shows as "../uploads/" after I echo it..

$cat_id = $_GET['cat_id'];
$cat_data = data_cat($cat_id,'name');

if (empty($cat_id)){
header("Location: categories.php");
exit;
}

if (isset($_GET['cat_id'])){
$cat_id = $_GET['cat_id'];

$query = mysql_query("DELETE FROM cattegories WHERE cat_id = $cat_id");

$target = ('../uploads/'.$cat_data['name']);
$target_thumb = ('../uploads/thumbs/'.$cat_data['name']);
delete_cat($cat_id);
delete_files($target,$target_thumb);
header("Location: categories.php");
exit();
}

It seems that my function cant grab the name of the category..

EDIT 2:

Thats my data_cat function:

function data_cat($cat_id){
    $cat_id = (int)$cat_id;

    $args = func_get_args();
    unset($args[0]);
    $fields = '`'.implode('`,`', $args).'`';

    $query = mysql_query("SELECT $fields FROM `cattegories` WHERE `cat_id` = $cat_id");
    $query_result = mysql_fetch_assoc($query);
    foreach ($args as $field){
        $args[$field] = $query_result[$field];
    }
    return $args;

}

P.S. Name cattegories is correct, I just made a mistake in db naming it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AlwaysConfused
  • 729
  • 2
  • 12
  • 37

1 Answers1

3

Because you have rmdir( $target ); and rmdir( $target_thumb ); in your script. Get rid of it and you will have your directories left as you want.

PHP Manual: rmdir — removes directory

Attempts to remove the directory named by dirname. The directory must be empty, and the relevant permissions must permit this. A E_WARNING level error will be generated on failure.

EDIT 1:

According you your last comment, you should debug your $target and $target_thumb variables:

echo '<pre>';
print_r( $target );
echo '</pre>';

What is the output for that? Make an edit to you question and provide the output and I will be able to help you out.

EDIT 2:

After looking at your posted code, I would strongly suggest to restructure it, as it's vulnurable to SQL injections: SQL injections in ADOdb and general website security

Community
  • 1
  • 1
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • You misunderstood me.. I want them to delete.. When I remove category from my website I want these folders with the name of that category be removed from uploads folder.. – AlwaysConfused Mar 12 '14 at 10:19
  • Lets say I have 1 category named "Glass", category is created in uploads/glass and uploads/thumbs/glass and that name is inserted in db.. When I want to delete this category named Glass I want to delete it from db, which is doing ok and then remove folder from uploads/ and uploads/thumbs and thats where all uploads folder is deleting for no reason.. – AlwaysConfused Mar 12 '14 at 10:22
  • 1
    @StuckBetweenTrees What is `data_cat` function look like? – Ilia Ross Mar 12 '14 at 10:39
  • @StuckBetweenTrees At last what is the *URL* parametrs `.php?cat_id=`... Provide an example. – Ilia Ross Mar 12 '14 at 10:46
  • Ok it works..My stuuuupid mistake, I misspelled my db name on that data_cat func.. Thanks Ilia Rostovtsev for pointing to the right direction!! – AlwaysConfused Mar 12 '14 at 10:47
  • @StuckBetweenTrees You are welcome. I also wanted to point out that you pass `'name'` in `data_cat($cat_id,'name');` and inside of `data_cat` function you don't process it at all. – Ilia Ross Mar 12 '14 at 10:54
  • Well i do, as this fields 'name' acts as a $fields in that function and selects only what you call in data_cat, in this case its a name, so it select $fields as a 'name' from db and returns it..if thats what you meant. – AlwaysConfused Mar 12 '14 at 12:00