Home » Networking » Net_FTP » Bug #2467
Problems with recursive mkdir
Details
| Submitted | 2004-10-06 11:23 UTC |
|---|---|
| From | enrico dot stahn at todo dot de |
| Assigned | toby |
| Status | Closed |
| Package | Net_FTP |
| PHP Version | 4.3.6 |
| OS | FreeBSD |
| Roadmaps | (Not assigned) |
Comments
[2004-10-06 11:23 UTC] enrico dot stahn at todo dot de
Description:
------------
Dear Toby!
I had some problems with the recursive part of the mkdir method. The main problem was that the last directory in the path was not created.
Example: /htdocs/test/test2/test3
All directorys except "test3" were created. The source of the problem is the while-loop, where all parts putted in an array called $elements. You have the put the last directory in the $elements-array.
42 while (false !== ($pos = strpos($dir, '/', $pos + 1))){
43 $elements[] = substr($dir, 0, $pos);
44 }
45 $elements[] = $dir;
The other problem was that mkdir return with an pear error object if any error was indicated. That's not a good solve, so an error is also present if an directory exists and mkdir want to create it. I think if an error is present and the reason of error is not the last directory, so you have ignore this. The following code solve this problem.
46 foreach ($elements as $element){
47 $res = $this->mkdir($element, false);
48 if($elements[count($elements)-1] == $element && $res !== true) {
49 return $res;
50 }
51 }
I hope it helps.
Regards, Enrico
Reproduce code:
---------------
23 function mkdir($dir, $recursive = false)
24 {
25 $dir = $this->_construct_path($dir);
26 if ($this->pwd() == $dir) {
27 return true;
28 }
29 if ($recursive === false){
30 $res = @ftp_mkdir($this->_handle, $dir);
31 if (!$res) {
32 return $this->raiseError("Creation of '$dir' failed", NET_FTP_ERR_CREATEDIR_FAILED);
33 } else {
34 return true;
35 }
36 } else {
37 $pos = 0;
38 if(strpos($dir, '/') === false) {
39 return $this->mkdir($dir,false);
40 }
41 $elements = array();
42 while (false !== ($pos = strpos($dir, '/', $pos + 1))){
43 $elements[] = substr($dir, 0, $pos);
44 }
45 $elements[] = $dir;
46 foreach ($elements as $element){
47 $res = $this->mkdir($element, false);
48 if($elements[count($elements)-1] == $element && $res !== true) {
49 return $res;
50 }
51 }
52 return true;
53 }
54 }