?
Solved

Creating web page template based on directory of image.jpg's

Posted on 2003-03-28
30
Medium Priority
?
274 Views
Last Modified: 2010-03-05
Can someone create a script that looks a every image file in a directory and creates a web page template from it, named after the suffix of the jpg?

I know this sounds weird, and maybe Perl is not the best place to put this question, but I always seem to get some kind of a solution here.

In summary.  I would put the pictures in a folder on the unix web server.  And run the program from the cgi bin.

In the folder there could be these images:

foobar.jpg
barfoo.gif
foofoo.jpg

The perl script would take those file names and create:

foobar.html
barfoo.html
foofoo.html

Within the HTML that is created would be the usual header and footer info.  Nothing special there.  But centered in the middle of the body text would be the name of the image. I.E.  foobar.jpg

So each image would have its own webpage named after it and be featured in the center of that page.  The resulting web pages could be stored in the same directory or a different directory.

I think that covers it. :)

Thanks,

Rowby
0
Comment
Question by:Rowby Goren
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 19
  • 11
30 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8228484
This should work.  You didn't mention wanting the image displayed in the web page.  If you do, just throw the html into the print below.

use CGI qw(:all);

opendir DIR, 'the_directory_where_the_images_are' or die $!;
while (my $file = readdir DIR) {
    next unless $file =~ /^(.*)\.(?:jpg|gif|png)$/; # make sure it is an image
    my $base = $1;
    open HTML, "output_directory/$base.html" or die $!;
    print HTML start_html, # html headers
        br x 4, # space down some vertically
        div({-align => 'center'}, $file), # center horizontally
        end_html; # end tags
    close HTML;
}
closedir DIR;
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8228553
HI,

Yes, I should have mentioned I want the image displayed.  Can you include that in the code.  THe image would be the same name as the html page, but with the extension jpg instead of html.

Also if it is no trouble, include in your code the word "Click here to close window" under the image.  Once you do that I can add the other refinements to the page.

Also can you give me the complete script -- I don't know how to write perl, I just copy and paste :).   What I mean is the header that starts with #!/usr/bin/perl
all the way to the close of the script.

THANKS for your quick reply!
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8228573
P.S. The extension that is visible would be either jpg or gif depending on the original image.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Accepted Solution

by:
wilcoxon earned 1200 total points
ID: 8228610
#!/usr/bin/perl
# might also be /usr/local/bin/perl or something else

use strict;
use warnings;
use CGI qw(:all);

# replace strings in the below code with meaningful values
# for example, the_directory_where_the_images_are needs to be the real path
# to the images

opendir DIR, 'the_directory_where_the_images_are' or die $!;
while (my $file = readdir DIR) {
    next unless $file =~ /^(.*)\.(?:jpg|gif|png)$/; # make sure it is an image
    my $base = $1;
    open HTML, ">output_directory/$base.html" or die $!;
    print HTML start_html, # html headers
        br, br, br, br # space down some vertically (4 lines)
        div({-align => 'center'}, # center horizontally
            img({-src => $file}), # change $file to "path_to_images/$file" if images are in different directory than html
            $file),
        end_html; # end tags
    close HTML;
}
closedir DIR;
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8228670
Thanks

I'll put it on the server first thing in the a.m. (getting to fuzzy brained now) and will award the points then.

Rowby
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8229364
Hi

I'm working on the script and getting a server configuration error.  I have the path to perl correct. I assume I am not entering the path correctly.  By the way, the HTML does indeed need to be in the same directory as the images.

#!/usr/bin/perl -T

use strict;
use warnings;
use CGI qw(:all);

# replace strings in the below code with meaningful values
# for example, the_directory_where_the_images_are needs to be the real path
# to the images

opendir DIR, 'http://www.foobar.com/store/images_products/closeups' or die $!;
while (my $file = readdir DIR) {
   next unless $file =~ /^(.*)\.(?:jpg|gif|png)$/;
# make sure it is an image
   my $base = $1;
   open HTML, "http://www.foobar.com/store/images_products/closeups/$base.html" or die $!;
   print HTML start_html,
# html headers
       br, br, br, br
# space down some vertically (4 lines)
       div({-align => 'center'},
# center horizontally
           img({-src => $file}),
# change $file to "path_to_images/$file" if images are in different directory than html
           $file),
       end_html;
# end tags
   close HTML;
}
closedir DIR;
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8229371
(for example, do I need a closing / for
'http://www.foobar.com/store/images_products/closeups'

But maybe I need to use server paths etc. ???

I also notice ">output in your code.  I did not include the > -- thought maybe it was a typo???

THanks
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8231208
I ran the program thorough my server's perl checker.  It recommended I remove the -T which I did.  Here is the results of the 'successful' check-- but I am still getting the internal server error.  I have asked the hosting company to activate error log reading, so I may have more info soon.

TEST 1: script and permissions
Passed Test 1!

TEST 2: script file format
Passed Test 2!

TEST 3: check perl path
Passed Test 3!

TEST 4: perl syntax
   createhtml.cgi syntax OK
Passed Test 4!

Your script has passed all 4 tests.

If your script passes these tests, and still won't run,
you might look for one or more of these common problems:
   1) make sure it can find any files or external programs it needs
   2) make sure any included perl programs also check out
   3) make sure you are writing a "Content-type:" line
      at the beginning of your output

Check your error log. Often, details of the problem can be found there.
Consult your favorite Perl/CGI book for more information.

0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8231628
To answer your questions/comments:
1) You do need to use the physical server paths (not URLs).
2) The '>' on the open of the HTML file was NOT a type.  The '>' tells perl to open it for writing (instead of reading).  If no operator is specified to open, a '<' is implied (open for reading).

If you did change the paths to physical server paths, I'm guessing the missing '>' is causing your "internal server error" as it is dieing when it tries to open (for reading) the non-existant html file (that you really want it to write).

Another option (besides checking the server logs) is to add this to your script:

use CGI::Carp qw(fatalsToBrowser);

That will send all fatal errors back to the browser (as well as put them in the logs).
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8231721
Hi,

I put in the >.   Here is a message I'm getting.  (Server logs are not avail to me yet.)


Permission denied at /home/ELErcs/www/cgi-bin/rowby-scripts/createhtml.cgi line 17.

I tried making the folder 755 and 777 but still get the above error.

If it means anything, you will see that the image-html directory is not in the cgi-bin, but in the regular web folder.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8231724
Here is the version I am using now:

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);

# replace strings in the below code with meaningful values
# for example, the_directory_where_the_images_are needs to be the real path
# to the images

opendir DIR, '/home/ELErcs/www/store/images_products/closeups' or die $!;
while (my $file = readdir DIR) {
   next unless $file =~ /^(.*)\.(?:jpg|gif|png)$/;
# make sure it is an image
   my $base = $1;
   open HTML, ">/home/ELErcs/www/store/images_products/closeups/$base.html" or die $!;
   print HTML start_html,
# html headers
       br, br, br, br
# space down some vertically (4 lines)
       div({-align => 'center'},
# center horizontally
           img({-src => $file}),
# change $file to "path_to_images/$file" if images are in different directory than html
           $file),
       end_html; # end tags
   close HTML;
}
closedir DIR;
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8231738
The web server probably runs cgi scripts as someone other than you.  I think this will fix it:

chmod 777 /home/ELErcs/www/store/images_products/closeups

The problem appears to be that the cgi script can't write to the images/html directory (which the above should fix).
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8231750
You might be able to get by with setting the directory to 775 (if you and the cgi account are in the same group (which you probably aren't)).

You might also want to try adding the below to the end of the loop (after "close HTML;").  It will change the ownership of the html file to you (change the below if your username is not ELErcs).

system 'chown', 'ELErcs', "$base.html";

You might need to change chown to the full path of chown.  Some systems also will not allow you to change the ownership of a file (so it might not work).
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8231755
Hmmm.. I did change the permissions to the images_products/closeups  directory to 777.  

I just remembered, I can also upload files not just via ftp, but thorugh an internal upload system.  I'll try that.

And will report back.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8231759
Hi, we're overlapping -- I just read your chown suggestion.

I'll try the built in file uploading console first.

Rowby
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8231786
If /home/ELErcs/www/store/images_products/closeups is already chmod'd 777 and you are still getting the "Permission Denied" error, the problem may be the files.  Are there any html files in that directory already that the script would be trying to over-write?  If so, this could be a problem (again because the cgi script probably runs as a user other than you).  Try "chmod 666 *.html" in the images_products/closeups directory (if there are already html files in there).
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8231796
There are only images in that directory.

I think you are correct about the ownership issue.  I triewd the chown suggestion (but not using the path, since I don't know it.)

However the hosting company indicated that they can change ownership of files.  And they would install perl scripts.  They are pretty helpful, so I will ask them to install the script.  

Since I am asking them to upload I will remove the chown command fromt the script.

But before I do that here is a copy and paste of the script that is currently on the server -- with the chown command included -- in case I didn't put it in the correct place...

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);

# replace strings in the below code with meaningful values
# for example, the_directory_where_the_images_are needs to be the real path
# to the images

opendir DIR, '/home/ELErcs/www/store/images_products/closeups' or die $!;
while (my $file = readdir DIR) {
   next unless $file =~ /^(.*)\.(?:jpg|gif|png)$/;
# make sure it is an image
   my $base = $1;
   open HTML, ">/home/ELErcs/www/store/images_products/closeups/$base.html" or die $!;
   print HTML start_html,
# html headers
       br, br, br, br
# space down some vertically (4 lines)
       div({-align => 'center'},
# center horizontally
           img({-src => $file}),
# change $file to "path_to_images/$file" if images are in different directory than html
           $file),
       end_html; # end tags
   close HTML;

system 'chown', 'ELErcs', "$base.html";
}
closedir DIR;

0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8231821
Looks good to me.  I've run out of ideas if the images_products/closeups directory is 777 and there are no html files in it.  I think you'll probably have better luck working the remaining problem out with your hosting company (if they are helpful) as it's not really a perl problem.

Let me know if there's anything else I can help with.  I'll be checking my mail off-and-on today (and checking this topic if it's been updated) but probably won't be on at all tomorrow.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8231881
I'll see if they can upload it during the weekend.  More likely it will be online by Monday.

And I'll report back to you.

But first should I remove the chown command?




Rowby
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8231990
Probably.  You can always add it back in if you find you really need it, but you're better off skipping any system calls.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8232214
Hi

I put the script on a different server. I adjusted the path to perl.  But got "no such file or directory".  

There is one thing I want to make sure I have correct on the script. I do not have a trailing / at the end of opendir DIR, '/home/sites/site2/web/rc-images'

There are no html pages in the directory.

I assume the script is not looking for any HTML page in the directory to start with.  (You can see how ignorant I am of perl!)

Rowby


Below is a copy and paste of the current script:  


#!/usr/local/bin/perl-T
 
use strict;
use warnings;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);

# replace strings in the below code with meaningful values
# for example, the_directory_where_the_images_are needs to be the real path
# to the images

opendir DIR, '/home/sites/site2/web/rc-images' or die $!;
while (my $file = readdir DIR) {
   next unless $file =~ /^(.*)\.(?:jpg|gif|png)$/;
# make sure it is an image
   my $base = $1;
   open HTML, ">/home/sites/site2/web/rc-images/$base.html" or die $!;
   print HTML start_html,
# html headers
       br, br, br, br
# space down some vertically (4 lines)
       div({-align => 'center'},
# center horizontally
           img({-src => $file}),
# change $file to "path_to_images/$file" if images are in different directory than html
           $file),
       end_html;
# end tags
   close HTML;
}
closedir DIR;
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8232338
It shouldn't matter if there is a slash at the end of the directory.

Which line is causing the "no such file or directory" error?  Still line 17?

Perl doesn't expect anything unless you tell it, so it's only expecting the directory .../rc-images to exist (it doesn't care if there are any files in it).

I think some web servers can be setup so that the root directory (/) isn't the real root directory.  If that's the case, you'll need to talk to your hosting company and find out what your path needs to be.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8232430
I sent the script to the hosting company and I think he'll have it up this weekend.

Rowby
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8238814
Hi

As I mentioned, while waiting for the other hosting company to fix the "ownership" of the perl file, I temporarily put the file on another server, specifically, one with www.rowby.com

Here is the path to the folder where the images are.

/home/sites/site2/web/rc-images

If you click on this link you will see an image in that directory.

http://www.rowby.com/rc-images/ALP7874.gif

Herer is the link to the perl script:

http://www.rowby.com/cgi-bin/rowbycreatehtml.cgi

The error message says folder or file not found.  I'm wondering if the script is looking for a file, not just a folder????

Thanks

Rowby
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8238837
Here's a copy and paste of the version on rowby.com

#!/usr/local/bin/perl-T
 
use strict;
use warnings;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);

# replace strings in the below code with meaningful values
# for example, the_directory_where_the_images_are needs to be the real path
# to the images

opendir DIR, '/home/sites/site2/web/rc-images' or die $!;
while (my $file = readdir DIR) {
   next unless $file =~ /^(.*)\.(?:jpg|gif|png)$/;
# make sure it is an image
   my $base = $1;
   open HTML, ">/home/sites/site2/web/rc-images/$base.html" or die $!;
   print HTML start_html,
# html headers
       br, br, br, br
# space down some vertically (4 lines)
       div({-align => 'center'},
# center horizontally
           img({-src => $file}),
# change $file to "path_to_images/$file" if images are in different directory than html
           $file),
       end_html;
# end tags
   close HTML;
}
closedir DIR;
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8239556
Hmm.  They are using cgi-wrap so there is no ownership problem on rowby.com (the cgi script will run as "you").

The error reporting on rowby.com isn't so great - it doesn't give you the line number of the error.

I'm guessing that cgi-wrap changes the effective root directory when it is running.

Try putting this above the opendir statement (for debugging) - you'll need to remove it again later.

print start_html;
chdir '/' or print("failed to cd to /<BR>");
chdir '/home' or print("failed to cd to /home<BR>");
chdir '/sites' or print("failed to cd to /sites<BR>");
chdir '/site2' or print("failed to cd to /site2<BR>");
print end_html;
exit;

Re-upload it and let me know what the output is.

Also, you should probably add this below the closedir statement (and keep it there):

print start_html, br, br, br, "The script finished successfully", end_html;
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8240421
The hosting company at the non-rowby site is looking into the perl issue there.

In that case it was a permissions error.  So I'll stay focused on that one instead of the rowby.com one.

(The cgi-wrap returned the same info on the rowby.com even with your additional code.  But since we are not concerned about the rowby.com server, I'm just letting you know this fyi.)

Rowby
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8240629
Well it's working. The problem was I needed to change the permissions of the images in the directory.

Thanks for all your patience.

You have given me a perfect solution!  I have increased the points to 300

Regards

Rowby
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 8240639
Again, thanks!  You've saved me hours if not days of work of having to hand make hundreds of html pages.

Rowby
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 8240708
You're welcome.  I'm glad I was able to help you out.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question