Jump to content

Recommended Posts

Would this work?

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.