Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TuffRivers's avatar

Vanilla PHP Code Review

Hi All,

I hacked together a vanilla PHP script that takes files uploaded to a folder and displays them on a webpage.

Basically i am making a very light weight CMS for a small project, wanted some advice/criticisms on this script and if there is a better way to do this

$dir = '/test/images';

//scan the directory
$files = scandir($dir, 0);

//remove . and .. from scandir
$files = array_diff($files, array('.','..'));

//for each filename in the directory place in html
foreach ($files as $file)

{

echo ('<img src="images/' .$file. '">');

}
0 likes
4 replies
jlrdw's avatar

How about formatting with some CSS.

TuffRivers's avatar

@jlrdw

i am using bootstrap and fancy box to display images, this is just purely to prove i can read contents of directory and put into browser

martinbean's avatar

@TuffRivers You’re not doing any validation of files you’re fetching.

Why not just use a framework if you’re building a CMS? Otherwise you’re going to be doing a lot of things from scratch if doing it in vanilla PHP.

TuffRivers's avatar

@martinbean i agree i could use an existing CMS but i have some time and this is a very small project so i would like to challenge myself to learn more vanilla PHP.

Case and point your first comment about not validating the files im fetching, something i would not have thought of but now i realize is extremely important.

Please or to participate in this conversation.