-2

I'm having the path like "AAA/BBB/CCC/DDD/test" in this path I want to delete the last folder "/test" using PHP. I have tried the below code.

Code

$dirname = 'AAA/BBB/CCC/DDD/test'
remove_directory();
function remove_directory() {
    rmdir($dirname)
}

Thanks in advance.

4 Answers4

2

You need to pass the variable to the function:

$dirname = 'AAA/BBB/CCC/DDD/test'
remove_directory($dirname);
function remove_directory($dirname) {
    rmdir($dirname)
}

Of course, if your function has only one single line, you can simply use the built-in PHP function instead of defining your own that does the same thing.

I strongly suggest you take a few minutes and read the manual page on variable scope.

Mike
  • 23,542
  • 14
  • 76
  • 87
2

Use basename() function: this will give you the required folder name.

$dirname = 'AAA/BBB/CCC/DDD/test';
function remove_directory($dirname) {
    $dirname = basename($dirname);
    rmdir($dirname);
}
remove_directory($dirname);
Pupil
  • 23,834
  • 6
  • 44
  • 66
2

What is the complete path? Since you don't start your path with for instance C:\ on Windows or / on Linux you are trying to remove a directory relative to the current working directory. This probably does not evaluate to the directory you are trying to delete.

If you want to access your $dirname variable inside your function you will have to use the global keyword. But better practice is to pass the directory as a variable to your function.

Also, as a matter of good practise, you should have the function implementation before trying to call it. Usually, it doesn't matter, but there are a few exceptions, particularly when the existence of a function is conditional on some other code being executed.

inquam
  • 12,664
  • 15
  • 61
  • 101
  • 1
    If this is a full path or relative path can be an implementation specific choice, you don't have to have a full path for this to work. You can also define the function and call in any order you wish. – Nigel Ren Mar 13 '19 at 07:36
  • Mike, that is why I didn't say must but should since it's a good practice to follow. Usually, it doesn't matter, but there are a few exceptions, particularly when the existence of a function is conditional on some other code being executed. – inquam Mar 13 '19 at 07:40
  • Nigel: Yes, what I'm saying is that this might be unintentional for the OP hence the question about what the path he/she is trying to delete is.OP could have used a rellative path without understanding it. – inquam Mar 13 '19 at 07:42
  • But the answer misses the important part that the variable with the path is out of scope of the function. The rest may be useful comments but does not provide a solution to OP's problem. – Nigel Ren Mar 13 '19 at 07:43
  • Nigel: I was just editing the answer to include information about the global keyword (which I initially missed) and then I saw this comment. Feels like this should have produced a very easy to understand error message though that OP should have been able to identify. – inquam Mar 13 '19 at 07:49
1

The problem here is that you have created a function remove_directory() without an argument. So the variable inside your function $dirname has no content since it is out of scope of where you originally defined it. Possible solutions would be to ditch the function and write this:

$dirname = 'AAA/BBB/CCC/DDD/test'¨
rmdir($dirname)

or to correct your mistake:

$dirname = 'AAA/BBB/CCC/DDD/test'
remove_directory($dirname);
function remove_directory($dirname) {
    rmdir($dirname)
}
Mr.Yellow
  • 692
  • 5
  • 15