Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top