Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to guess that if you looked at the web server log, you'd see this error:</p> <pre><code>PHP Catchable fatal error: Object of class BrowseFolder could not be converted to string in &lt;filename&gt; on line &lt;XX&gt; </code></pre> <p>Your <code>BrowseFolder</code> class has no <code>__toString()</code> method, so an attempt to call it in string context is throwing a fatal error (this line: <code>ta($o);</code>)</p> <p>However, that's only problem 1. Problem 2 is this:</p> <pre><code>$this-&gt;srcFolder=$srcFolder; </code></pre> <p>Since there's only a single instance of your BrowseFolder class, you're setting a property on the class and then attempting to reuse that in recursion. So basically what it's doing is drilling into the first folder, then since <code>$this-&gt;srcFolder</code> just equals subsequent children of that, it makes no attempts to go further. Replace every instance of <code>$this-&gt;srcFolder</code> with just <code>$srcFolder</code>. The recursive function will then drill down into the first, then when it reaches the end, work its way back up to the first call of <code>browse()</code>, then continue onto the second, and so on.</p> <p><strong>Edit</strong> - additional detail: It <em>shouldn't</em> be modifying the variable each time. What it's doing is taking the first folder <code>images</code>, then setting that property on the class. It goes down into the first child of that folder - call it <code>folder1</code>, then again sets the class property to that child (i.e. <code>$this-&gt;srcFolder == "images/folder1"</code>). It lists the files in that folder, then continues on. Since there are no more files or subdirectories in that folder, it's reached the end of that iteration. So then it goes back up a level to the <code>images</code> folder. It goes to the next iteration of the <code>foreach</code> loop and now sets <code>$val</code> to <code>folder2</code>. Now it tries both <code>if(is_file($this-&gt;srcFolder.$val))</code> and <code>else if (is_dir($this-&gt;srcFolder.$val)</code>. The translation of that is <code>is_file("images/folder1/folder2")</code> since <code>$this-&gt;srcFolder</code> equals <code>images/folder1</code>. As a result, this always returns <code>false</code> until it just reaches the end of the <code>foreach</code> and exits.</p> <p>If you instead call <code>$srcFolder</code> (local variable), then it will have the correct value each time it gets to the function. In the same scenario: it goes into <code>folder1</code> and just inside the second level of the <code>browse()</code> function, <code>$srcFolder == "images/folder1"</code>. It lists each element of that folder, then goes back up to <code>images</code>. Now the function still has its initial value for <code>$srcFolder</code>, which is <code>"images"</code>. So then in the second iteration of the <code>foreach</code> loop, now <code>$val</code> again equals <code>folder2</code>, so it calls <code>browse()</code> on that. Again in the second level of the function, <code>$srcFolder == "images/folder2"</code>. Using a local variable in recursion ensures that each call of the function will have the value it needs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload