Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Get list of files in a specified folder on server

 
   Database Forums (Home) -> PHP RSS
Next:  DAP's & Queries  
Author Message
David

External


Since: Oct 12, 2005
Posts: 4



(Msg. 1) Posted: Tue Oct 11, 2005 10:55 pm
Post subject: Get list of files in a specified folder on server
Archived from groups: alt>comp>lang>php (more info?)

Hi All,

I'm new to PHP and am trying to get the file contents of a folder. I have
this code below but it is failing. Can a keen eye spot where I am going
wrong?

If not could you point me to a tutorial or the like?

Thanks, David


<?
session_start();
session_register("imagecount_session");
session_register("imagepath_session");
?>

<?
ListFolderContents($DOCUMENT_ROOT.$_GET["folder"]);
function ListFolderContents($path)
{
extract($GLOBALS);


$fs=$CreateObject["Scripting.FileSystemObject"]
$folder=$fs->GetFolder $path);
$imagecount_session=$folder->Files.$Count;
foreach ($folder Files as $item)
{

$i=$i+1;
$url=MapURL($item->path);
session_register("iCount".$i);
${"iCount".$i."_session"}=$item->Name;
} return $function_ret;
}
for ($j=1; $j<=$imagecount_session; $j=$j+1)
{
$a[$j]=${"iCount".$j."_session"};

}

$s=$Join[$a][","];
function MapURL($path)
{
extract($GLOBALS);

$rootPath=$DOCUMENT_ROOT."/";
$url=substr($path,strlen($path)-(strlen($path)-strlen($rootPath)));
$MapURL=str_replace("\","/",$url);
$imagepath_session=$MapURL;
return $function_ret;
}
?>

 >> Stay informed about: Get list of files in a specified folder on server 
Back to top
Login to vote
David

External


Since: Oct 12, 2005
Posts: 4



(Msg. 2) Posted: Wed Oct 12, 2005 2:55 am
Post subject: Re: Get list of files in a specified folder on server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I must be doing something wrong. I can't seem to get the code below to
execute either. My page is .php, my host has PHP4. I can do this in ASP so
it must be a syntax quibble or something simple I'm missing. I can get
simple php functions to work though.


$path = '/path/to/dir'
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)) $numfile++;
if(is_dir($file)) $numdir++;
};
};
closedir($handle);
};
echo $numfiles.' files in a directory';
echo $numdir.' subdirectory in directory';


David

 >> Stay informed about: Get list of files in a specified folder on server 
Back to top
Login to vote
Fabian Vilers

External


Since: Oct 05, 2005
Posts: 2



(Msg. 3) Posted: Wed Oct 12, 2005 9:32 am
Post subject: Re: Get list of files in a specified folder on server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Back to top
Login to vote
MikesBrain

External


Since: Oct 12, 2005
Posts: 2



(Msg. 4) Posted: Wed Oct 12, 2005 12:55 pm
Post subject: Re: Get list of files in a specified folder on server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 12 Oct 2005 01:56:35 GMT, "David" wrote:

>Hi All,
>
>I'm new to PHP and am trying to get the file contents of a folder. I have
>this code below but it is failing. Can a keen eye spot where I am going
>wrong?
>
>If not could you point me to a tutorial or the like?
>
>Thanks, David
>
>
><?
> session_start();
> session_register("imagecount_session");
> session_register("imagepath_session");
>?>
>
><?
>ListFolderContents($DOCUMENT_ROOT.$_GET["folder"]);
>function ListFolderContents($path)
>{
> extract($GLOBALS);
>
>
>$fs=$CreateObject["Scripting.FileSystemObject"]
>$folder=$fs->GetFolder $path);
>$imagecount_session=$folder->Files.$Count;
>foreach ($folder Files as $item)
> {
>
> $i=$i+1;
> $url=MapURL($item->path);
> session_register("iCount".$i);
>${"iCount".$i."_session"}=$item->Name;
> } return $function_ret;
>}
>for ($j=1; $j<=$imagecount_session; $j=$j+1)
>{
> $a[$j]=${"iCount".$j."_session"};
>
>}
>
>$s=$Join[$a][","];
>function MapURL($path)
>{
> extract($GLOBALS);
>
> $rootPath=$DOCUMENT_ROOT."/";
> $url=substr($path,strlen($path)-(strlen($path)-strlen($rootPath)));
> $MapURL=str_replace("\","/",$url);
> $imagepath_session=$MapURL;
> return $function_ret;
>}
>?>
>
>
I came across this some time ago and put it on my server, where it seems to
do the business; no doubt personal improvements can be made to it all over
the place.

<?
print("<table width=780 cellspacing=0 cellpadding=0 border=0><tr><td>");
print("Please be patient - you can sometimes be downloading a
<strong>lot</strong> of data.
<br> <br>      Entries without an
extension (the bit after the \" . \") are directories.<br> ");
print("</td></tr></table>");
print("<table width=770 align=center cellspacing=0 cellpadding=0
border=0><tr><td>");
$open=opendir (".");
while ($filename=readdir ($open))
{
$filelist[]=$filename;
$size=$size+filesize($filename);
}
closedir ($open);
sort($filelist);
$count=count($filelist);
print("<table width=650 align=center cellspacing=0 cellpadding=2
border=1>");
print("<tr><td colspan=3 align=center><strong>Total number of files and
directories=$count occupying $size Kbytes</strong></td></tr>");
for($number=2;
$number<$count;
$number++)
{
$i++;
print("<tr><td align=right width=40>$i   </td><td>     <a
href=\"$filelist[$number]\">$filelist[$number]</a></td><td width=180
align=right><font color=\"#707070\">Go to </font><a href=\"#top of
page\">top of page</a>.   </td></tr>");
}
print("</table>");
print("</td></tr></table>");
?>

Don't forget www.php.net
 >> Stay informed about: Get list of files in a specified folder on server 
Back to top
Login to vote
David

External


Since: Oct 12, 2005
Posts: 4



(Msg. 5) Posted: Wed Oct 12, 2005 3:55 pm
Post subject: Re: Get list of files in a specified folder on server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I have discovered why none of these work. I'm on a Windows server and the
PHP equivalent of the ASP Server.MapPath(Request.QueryString("folder") is
more than a bit difficult. I have found many a soul struggling with this in
my research.

I've tried many of the usual equivalents ...

$thepath = $_SERVER["PATH_TRANSLATED"]
$thepath = $_SERVER['DOCUMENT_ROOT']

Also have looked at echo phpinfo(); for more details and it looks like
Imight be able to hack the $_SERVER["PATH_TRANSLATED"] with a substring to
extract the root.

_SERVER["PATH_TRANSLATED"] and _ENV["PATH_TRANSLATED"] both return a
strange path with doubling of the slahes...
D:\\inetpub\\mydomain\\mydirectory\\mypage.php

but might be do-able.

Anyone with suggestions I'm all ears.

David
 >> Stay informed about: Get list of files in a specified folder on server 
Back to top
Login to vote
David

External


Since: Oct 12, 2005
Posts: 4



(Msg. 6) Posted: Wed Oct 12, 2005 7:03 pm
Post subject: Re: Get list of files in a specified folder on server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> If not could you point me to a tutorial or the like?
>
> Have a look to: http://www.php.net/manual/en/function.opendir.php

Fabian,

Yes I found that sight last night when studying this. Good resource.

David
 >> Stay informed about: Get list of files in a specified folder on server 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Folder name as a GET? - Hello, I currently have a website and would like to be able to give my members an alias to access their profile. At the moment, people have to go to something like www.domain.com/viewmember.php?id=3456, which isn't practical unless it's through clicking...

Find the name of the current folder - For use in a install.php What would be the best way to do it .

Determine folder a script is running in. - What is the most reliable method to determine the folder a script is running in? I was looking at the globals in $_SERVER but all the variables also listed the actual file the script was running in. Thanks Scotty

how do you get a list of all dates between two dates more .. - Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or array or all the date between and including those two dates. I want to include this list in a query so I need it in a format like: '2005-01-01', '2005-01-02',... Any ideas?..

Mailing list manager, send to list - Hi, I'm looking for a mailing list script (php) that has a function to let users to reach out to all recipient on the list by simple send the email to a specific maillist- address. Mailman has this functionn but as a just got a webserver account I can't...
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]