Trouble dir reading in perl

software development

#1

I’m trying to read from a directory in perl using this script:

http://www.designsbythedozen.com/cgi-bin/updater.pl

It used to work on my old server. It must be some kind of permissions
thing? I’m wondering, because I’ve never had this problem with my other
account on dreamhost. It returns nothing when I’m trying to getcwd()
or it says permission denied access to (./…/…/…/) the only thing is
I’m not trying to read ./…/…/…/ I don’t know what this error means.

Sincerely,

Aaron Scott


#2

Since you can’t seem to run the script from the web, and I don’t know why, here it is.
I have opened and saved the script in pico.
I have chmod’d it 755
It still doesn’t run on the web, and when I run it under a command prompt, it works, but the scalars that should have the dir listings are empty

#!/usr/bin/perl

use Cwd;
use File::Find;
use CGI;
use CGI::Carp qw( fatalsToBrowser );
BEGIN {
sub carp_error {
my $error_message = shift;
my $q = new CGI;
print $q->start_html ( “Error” ),
$q->h1( “Error”),
$q->p( "Sorry, the following error has occured: " ),
$q->p( $q->i( $error_message ) ),
$q->end_html;
}
CGI::Carp::set_message( &carp_error );
}

print “content-type: text/html\n\n”;

if ( $ENV{REQUEST_METHOD} eq “GET” ) {
$data = $ENV{QUERY_STRING};
}
else {
read(STDIN, $data, $ENV{CONTENT_LENGTH});
}
@pairs = split(’&’, $data);
foreach $item (@pairs) {
($name, $value) = split("=", $item);
$value =~ s/+/ /g;
$value =~ s/%([0-9A-Za-z][0-9a-zA-Z])/
pack(“c”,hex($1))/ge;
$input{$name} = $value;
}
$pc = (1+ ($input{percent} / 100)); #percent increase
$input{perm} = 1;

opendir DIR, “.”;
@dirc = readdir(".");
print “\n\ndirc: @dirc \n\n”;

print “\n\ncuurent: $c \n\n”;

@directories_to_search = (
“Belly Jewelry”,
“Bracelets”,
“Custom”,
“Displays”,
“Earrings”,
“Faces”,
“FTH 925 Silver”,
“FTH Charms”,
“FTH Collegiate”,
“FTH Rings”,
“FTH Watches”,
“FTH Wholesale Gifts”,
“Keychains”,
“Kid’s Jewelry”,
“Magnetic Bracelets”,
“Necklaces”,
“Pendants”,
“Pins”,
“Shirts”,
“Stretchy Bracelets”,
“Sunglasses”,
“Theme”,
“Toe Anklets”,
“Watches”,);

chdir("…/");

finddepth(&wanted, @directories_to_search);

#END SECURITY
} else {# end password security
print “Access Denied”;
}#

sub wanted {
if (/.html?$/) {

print "updating " . $_;

print "

";
open (FILE, “< $_”);
$line = “”;
$file = “”;
while ($line = ) {
print "

=======
$_
$File::Find::name
$line
" if $line =~ / $50 /;
print "

+++++++
$line
" if $line =~ s/ $50 / $75 /;
$b = 1, next if $line =~ //i;
if ($b) {
$p = $1 if $line =~ /(<.)/;
$p = 1 if $p =~ /<p/i;
$b = 0, $p = 0 if $p =~ /</;
$b = 0, $p = 0 if $line =~ /This page uses frames/i;
if ($p) {
if ($line =~ /</p>/i || $line =~ /</td>/i) {
$ppart1 = $1, $ppart2 = $2 if $line =~ /(.</p.>)(.)/i;
$ppart1 = $1, $ppart2 = $2 if $line =~ /(.
</td>)(.*)/i;
print "

=======
$_
$File::Find::name
$tempLin$line
";
$line = $ppart1 . “\n

<span style=“text-align:center; color:red; font-size:18; font-weight:bold”>Factory direct prices. All prices on site are per item purchased as a dozen. Halve dozen and single item purchases are allowed. See order form for up charges.

” . $ppart2;
print "

+++++++
$tempLine$line
";
$p=0;
$b=0;
$tempLine = “”;
}
$tempLine .= $line if $p == 1;
}
}
print "

=======
$_
$File::Find::name
$line
", $count++ if $line =~ /fax your order to 505-265-5999/;
$line .= “\n

<font face=“Arial Black” color=”#FF0000">Due to the unique nature of our line some orders may require assembly before shipping. Please allow 7 - 10 days for order to ship.

\n" if $line =~ /fax your order to 505-265-5999/i;
print "

+++++++
$line
" if $line =~ /fax your order to 505-265-5999/;

$file = $file . $line;
}
close(FILE);

#if ($input{perm}) {

open (FILEE, “> $_”);

print FILEE $file;

close(FILEE);

#}
}
}

sub adZerosRound {
$num = $_[0];
if (index($num,".") == -1) {
$num = $num . “.00”;
return $num;
}
if (index($num,".") >= 0) {
$ind = index($num,".");
$sub = substr($num,$ind);
$len = length($sub);
if ($len < 3) {$num=$num . “0”;}
if ($len > 3) {
substr($sub,3) = “”;
substr($num,$ind) = $sub;}
return $num;
}
}


#3

Use cwd(), not getcwd().

Try this instead of finddepth:

[code]chdir("…/");

my $current_dir = cwd();

&scan_dir($current_dir);

exit;

sub scan_dir {
my ($dir_path) = @_;
my @files = (); # list of files
my @dirs = (); # list of subdirectories
print p(“Scanning $dir_path”);
if (opendir(DIR, $dir_path)) {
foreach $entry (readdir(DIR)) {
next if index($entry, ‘.’) == 0; # skip entries starting with a period (. … .htaccess, etc)
if (-d “$dir_path/$entry”) {

add subdirectory to list

push(@dirs, “$dir_path/$entry”);
}
elsif ($entry =~ /.html?$/) {

add HTML document file to list

push(@files, “$dir_path/$entry”);
}
}
closedir(DIR);
}

update files first

foreach $file (@files) {
&update_file($file);
}

recurse into subdirectories next

foreach $directory (@dirs) {
&scan_dir($directory);
}

return;
}[/code]
:cool: Perl / MySQL / HTML+CSS


#4

Thanks for the script. :wink:

~A