How to get a file listing

How to get a file listing

am 18.02.2010 20:46:46 von ceauke

Hi guys

I wanted to do a simple script to show all pictures in a specific folder.
But I don't see any functions to read all files in a folder.

The logic needs to go something like this:
- Open directory to read file list
- If file like *.gif then print filename

Any tips?
--
View this message in context: http://old.nabble.com/How-to-get-a-file-listing-tp27644565p2 7644565.html
Sent from the mod_perl - General mailing list archive at Nabble.com.

Re: How to get a file listing

am 18.02.2010 20:51:40 von mcapone

#!/usr/local/bin/perl

chdir("/path/to/my/directory");

while($filename=<*.gif>)
{
print "$filename\n";
}

# Enjoy!


ceauke wrote:
> Hi guys
>
> I wanted to do a simple script to show all pictures in a specific folder.
> But I don't see any functions to read all files in a folder.
>
> The logic needs to go something like this:
> - Open directory to read file list
> - If file like *.gif then print filename
>
> Any tips?
>

Re: How to get a file listing

am 18.02.2010 21:03:07 von ceauke

Sorry, I completely forgot to say.

This is for MOD_PERL on apache. So I want the webserver to show an unknown
amount of pictures.
--
View this message in context: http://old.nabble.com/How-to-get-a-file-listing-tp27644565p2 7644823.html
Sent from the mod_perl - General mailing list archive at Nabble.com.

Re: How to get a file listing

am 18.02.2010 21:12:09 von Dzuy Nguyen

opendir(DIR, "/your/dir");
my @gifs = grep { /\.gif$/ } readdir(DIR);

ceauke wrote:
> Hi guys
>
> I wanted to do a simple script to show all pictures in a specific folder.
> But I don't see any functions to read all files in a folder.
>
> The logic needs to go something like this:
> - Open directory to read file list
> - If file like *.gif then print filename
>
> Any tips?
>

Re: How to get a file listing

am 18.02.2010 21:20:41 von ceauke

Wow, thanks. It's working. I have a problem with the dir structure:
Perls seems to search for mydir in \cgi-bin\ and not where I defined mydir
in the apache conf file.

So my structure is like this:
\cgi-bin\myprog.cgi <---- my program
\cgi-bin\mydir1\ <----- this one can be seen by my program
\mydir2\ <----- how do I show this content?
--
View this message in context: http://old.nabble.com/How-to-get-a-file-listing-tp27644565p2 7645041.html
Sent from the mod_perl - General mailing list archive at Nabble.com.

Re: How to get a file listing

am 18.02.2010 21:26:45 von Alan Young

On Thu, Feb 18, 2010 at 13:20, ceauke wrote:
> So my structure is like this:
> \cgi-bin\myprog.cgi =A0 <---- my program
> \cgi-bin\mydir1\ =A0 <----- this one can be seen by my program
> \mydir2\ =A0 =A0<----- how do I show this content?

opendir my $DH, '\mydir2\' or die "Unable to open mydir2: $!";

The rest as before.
--
Alan

Re: How to get a file listing

am 18.02.2010 21:31:35 von mcapone

Depending on your server setup, you may or may not be able to read
\mydir2\. Whether on windows or a unix variant, apache is configured to
run as "some user" (often, "nobody" on unix); and that user needs to be
given at least read access to the \mydir2\ directory in order for this
to work.

Alan young's snippet he gave you:

opendir my $DH, '\mydir2\' or die "Unable to open mydir2: $!";

should quickly tell you if you don't have read permission on \mydir2.




ceauke wrote:
> Wow, thanks. It's working. I have a problem with the dir structure:
> Perls seems to search for mydir in \cgi-bin\ and not where I defined mydir
> in the apache conf file.
>
> So my structure is like this:
> \cgi-bin\myprog.cgi <---- my program
> \cgi-bin\mydir1\ <----- this one can be seen by my program
> \mydir2\ <----- how do I show this content?
>

Re: How to get a file listing

am 18.02.2010 21:39:38 von ceauke

Hi guys

Running apache on winxp.
The bizarre thing is in my browser I can put:

myserver.com/mydir2/test.gif and that works
myserver.com/cgi-bin/mydir/test.gif gives internal server error. but I know
the opendir command can at least read mydir. Still, how should I 'enable'
mydir2?

another issue is that even if I put xxx as the dir to open, I never get the
die text?

--
View this message in context: http://old.nabble.com/How-to-get-a-file-listing-tp27644565p2 7645284.html
Sent from the mod_perl - General mailing list archive at Nabble.com.

Re: How to get a file listing

am 19.02.2010 10:33:51 von Keywan Ghadami

Hi i asume you using xamp and test.gif is a simple git picture?

The apache webserver will try to execute

myserver.com/cgi-bin/mydir/test.gif

put the picture anywhere else (eg C:\xampp\htdocs\mydir\).
Maybe there are some more infos in your log file (eg.
C:\xampp\apache\logs\error.log)

You can configure the directories and there behavior in
C:\xampp\apache\conf\httpd.conf.

best regards


> Hi guys
>
> Running apache on winxp.
> The bizarre thing is in my browser I can put:
>
> myserver.com/mydir2/test.gif and that works
> myserver.com/cgi-bin/mydir/test.gif gives internal server error. but I know
> the opendir command can at least read mydir. Still, how should I 'enable'
> mydir2?
>
> another issue is that even if I put xxx as the dir to open, I never get the
> die text?
>
>

Re: How to get a file listing

am 20.02.2010 19:01:59 von ceauke

Hi there

Here is my code. I get the IMG displayed but also the perl error: No such
file or directory.

"
#!E:\ea12\apps\tech_st\10.1.2\perl\5.6.1\bin\MSWin32-x86\per l.exe
print "Content-type: text/html \n\n";

print "Test:";
print " /Ski/temp.jpg ";

opendir(DIR, "/Ski") or die print "Error $!";

print "Dir list: ";
while( ($filename = readdir(DIR))){
print("$filename");
}
closedir DIR; "

It seems like the webserver (apache) knows what /Ski is but perl doesn't.
It's looking for /ski in cgi-bin. my /ski folder is out of the root. I have
an alias, directory and location defined in httpd.conf for it. How do I get
perl to see it?
--
View this message in context: http://old.nabble.com/How-to-get-a-file-listing-tp27644565p2 7668252.html
Sent from the mod_perl - General mailing list archive at Nabble.com.

Re: How to get a file listing

am 20.02.2010 19:07:26 von Brad Van Sickle

Apache knows the context, PERL does not.

Fully qualify that directory name and it should work.





On 2/20/2010 1:01 PM, ceauke wrote:
> Hi there
>
> Here is my code. I get the IMG displayed but also the perl error: No such
> file or directory.
>
> "
> #!E:\ea12\apps\tech_st\10.1.2\perl\5.6.1\bin\MSWin32-x86\per l.exe
> print "Content-type: text/html \n\n";
>
> print "Test:";
> print " /Ski/temp.jpg ";
>
> opendir(DIR, "/Ski") or die print "Error $!";
>
> print "Dir list: ";
> while( ($filename = readdir(DIR))){
> print("$filename");
> }
> closedir DIR; "
>
> It seems like the webserver (apache) knows what /Ski is but perl doesn't.
> It's looking for /ski in cgi-bin. my /ski folder is out of the root. I have
> an alias, directory and location defined in httpd.conf for it. How do I get
> perl to see it?
>

Re: How to get a file listing

am 20.02.2010 19:16:23 von ceauke

No,
that doesn't work either. I used opendir (DIR, "G:\FTP\Ski") ...
I've tried the slashes in all directions, and with and without preceding and
slashes in the end.
e.g. \Ski, \Ski\, /ski, /ski/

How do I get perl to understand the context? is there some function that
points to the root? (even that will be pointless as I'm on WINXP where I
have multiple drives.



Apache knows the context, PERL does not.

Fully qualify that directory name and it should work.

--
View this message in context: http://old.nabble.com/How-to-get-a-file-listing-tp27644565p2 7668907.html
Sent from the mod_perl - General mailing list archive at Nabble.com.

Re: How to get a file listing

am 20.02.2010 19:18:44 von ceauke

Sorry,

got it working with your advice:
opendir(DIR, "G:/FTP/Ski") or die print "Error $!"; worked in the end...
Damn these slashes :-D

Thanks for the help
--
View this message in context: http://old.nabble.com/How-to-get-a-file-listing-tp27644565p2 7668922.html
Sent from the mod_perl - General mailing list archive at Nabble.com.