Jump to content

Is there a way to return all the .html files in a folder?


cooldude832

Recommended Posts

It should work.

The only problem is if there is more than one '.' in the filename.. such as in a file called myfile.ini.php, which I've seen before.

You could use this, which will use the last value from the explode (whgich will be the filetype).

<?php
$dir = "/subfolder";
$files = scandir($dir);
$i = 0;
foreach($files as $value)
{
$temp = explode(".",$value);
if ((end($temp) == "html") || (end($temp) == "php") || (end($temp) == "htm"))
{
$file[$i] = $value;
$i++
}
}
?>

 

or this should work.. and be a bit neater. First set an array with the filtypes in, then search the array. If the filetype isn't in the array it's ignored.

<?php
$filetype = array('html', 'htm', 'php');
$dir = "/subfolder";
$files = scandir($dir);
$i = 0;
foreach($files as $value)
{
$temp = explode(".",$value);
if(array_search(end($temp), $filetype) != false)
{
$file[$i] = $value;
$i++
}
}
?>

How does this sound for a php-4 version?

<?php
$dir = "/subfolder";
if(is_dir($dir))
{
$files = opendir($dir);
$files = readdir($files);
$i = 0;
foreach($files as $value)
{
$temp = explode(".",$value);
if ((end($temp) == "html") || (end($temp) == "php") || (end($temp) == "htm"))
{
$file[$i] = $value;
$i++
}
}
}
?>

 

I agree with you so i can add more file types easily and mod it for other types

How does this sound for a php-4 version?

<?php
$filetype = array('html', 'htm', 'php');
$dir = "/subfolder";
if(is_dir($dir))
{
$files = opendir($dir);
$files = readdir($files);
$i = 0;
foreach($files as $value)
{
$temp = explode(".",$value);
if(array_search(end($temp), $filetype) != false)
{
$file[$i] = $value;
$i++
}
}
}
?>

 

gettign errors and my chmod is set to allow all to read it

 

Warning: opendir() [function.opendir]: open_basedir restriction in effect. File(/sources) is not within the allowed path(s): (/hsphere/local/home/pira00:/usr/local/lib/php:/hsphere/shared/apache/libexec:/tmp:/var/tmp) in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 4

 

Warning: opendir(/sources) [function.opendir]: failed to open dir: Operation not permitted in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 4

 

Warning: readdir(): supplied argument is not a valid Directory resource in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 5

 

 

      Warning: Invalid argument supplied for foreach() in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 8

 

Warning: opendir(../sources) [function.opendir]: failed to open dir: No such file or directory in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 4

 

Warning: readdir(): supplied argument is not a valid Directory resource in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 5

 

 

      Warning: Invalid argument supplied for foreach() in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 8

 

 

now its saying not a dir the file its part of is an included file in the loaded doc, but the include is in the same folder as the actual page http://upperstraitscleanlake.org/About/index.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.