Hello everyone im trying to read a directory called people which is filled with php,html,png,jpeg
and other formats. I am trying to filter the files i want the php
and html
files and then echo them out.
Here is my attempt which didn't work since it didn't echo
any files:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Khviii ICT</title>
</head>
<body>
<div align="center" ><h1>Welcome to KHVIII ICT</h1></div>
<?php
$dir="/people";
$allowed_view= array("php","html");
$files=scandir($dir);
foreach ($files as $file)
{
$filexplosion=strtolower(end(explode('.',$file)));
if (in_array($allowed_view,$filexplosion))
{
echo $file;
}
}
?>
</body>
</html>
After a few goes i have worked it out i am using gelb(). Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>khvii index</title>
</head>
<body>
<?php
foreach (glob("people/"."*.php",GLOB_BRACE) as $filename) {
$phpexplode = end(explode("/",$filename));
echo'<a href="'.$filename.'"><input type="button" value="'.$phpexplode.'"></a><br />';
}
foreach (glob("people/"."*.html",GLOB_BRACE) as $filename) {
$htmlexplode = end(explode("/",$filename));
echo'<a href="'.$filename.'"><input type="button" value="'.$htmlexplode.'"></a><br />';
}
?>
</body>
</html>
directory path is not correct and in_array arguments are not in order. Corrected php code is as below:
$dir = __DIR__ . "/people";
$allowed_view = array("php", "html");
$files = scandir($dir);
foreach ($files as $file) {
$filexplosion = strtolower(end(explode('.', $file)));
if (in_array($filexplosion, $allowed_view)) {
echo $file;
}
}
Try this:
$files=scandir($dir);
echo "I have found " . count($files) . " files<br>"; // Are you sure there are files found?
foreach ($files as $file)
{
$filexplosion = substr($file, strrpos($file, "."));
echo $filexplosion; // just to check if you're getting the right value
if (in_array($allowed_view,$filexplosion)){
echo $file;
}