I’ve made a script that will list directories and pdf files on our intranet. Everything is working so I don’t need anything debugged. I was basically just wondering if I’ve violated any best practices or how I could improve on things. Did I do anything that isn’t considered good programming?
The script allows users who have access to the folder structure to create and delete new folders and update pdf documents.
I’ve replaced confidential paths with the word “example”
wow! I’ve seen code from others who were new to PHP that was way worse! I’m guessing you already knew another programming language?
Overall the code looks pretty good, well laid out, good to follow.
Some pointers
if (isset($_GET[‘dir’])) { … } if (!isset($_GET[‘dir’])) { … }
can be replaced by if (isset($_GET[‘dir’])) { … } else { … } as the two conditions are mutually exclusive
I don’t really see the need to put paths in an array, strings would work just as well, but that’s more a matter of taste then functionality
I don’t like the $ii, because it’s a recipe for disaster. One day you’ll find yourself debugging your code for hours, only to find to you used $ii instead of $i or the other way around. I mostly use descriptive variables names like $numDirs. Again, also a matter of taste I suppose
$ii is redundant - you’re not changing the size of the $folders (or $files) array inside the loop, so putting the count($folders) directly into the for statement is sufficient. For that matter, a FOREACH would accomplish this without the use of the extra variables at all!
if (isset($_GET['dir'])) {
This should be the ELSE clause from the previous IF.
Be careful when using file structures that you dont pull . or … (Havent used glob enough to know if it avoids them intentionally)