Welcome to WebmasterWorld Guest from 54.167.46.29

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Can't open database file

     
12:10 pm on Jun 16, 2011 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 96
votes: 0


I'm trying to use a text type DBM file but can't extract the values from the keys. I'm getting a message in the browser that says "Can't open english.db:Permission denied". I did alot of searching on this one to no avail.

Script looks like this:

#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use DB_File;
use Fcntl;
print"Content-Type:text/html\n\n";

my(%hexcodes,$dbm,%HASH,%hash,$red);
my $dbm="colors.db";
tie (%HASH, "DB_File", $dbm)or die "Can't open $dbm:$!\n";

my $red=$hexcodes{'red'};
print"$red";

untie %hash;

colors.db looks like this(separate file)I tried using the .txt extension but getting the same results.

%hexcodes('red' => 'ff0000',
'black' => '000000',
'white' => 'ffffff'
)
5:04 pm on June 16, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Is that your full script? Where does english.db come in?

Is the .db in cgi-bin? Try storing it in a different directory and using the full path.

/cgi-bin/yourscript.cgi
/httpdocs/data/colors.db

my $dbm = $ENV{'DOCUMENT_ROOT'} . "/data/colors.db";
6:16 pm on June 16, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


Is the database-file read- and writable by the user the cgi-script runs as?
If apache is not configured with suexec/mod_suid2/mod_ruid, that's usually not the same user you upload stuff as.
Try chmod'ing the db-file to 777 (read & write for all) and see if that works.
12:51 am on June 18, 2011 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 96
votes: 0


I was living up to my name typo when I made this post. The file english.db was the mistake. Any way got that fixed. Problem now is when I open the script as posted below I get a "0"(zero) appearing on the browser screen.

#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use DB_File;
use Fcntl;
print"Content-Type:text/html\n\n";
my(%colors,$dbfile,%HASH,%hash);
my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt ";
tie (%HASH, "DB_File", $dbfile)or die "Can't open $dbfile: $!\n";

my %colors;
print"$colors{'ffffff'}";
print"$colors{'000000'}";
print"$colors{'ff0000'}";
untie %hash

colors.txt is as follows

%colors=(
'ff0000' =>'red'
'000000' => 'black'
'ffffff' => 'white'
)

If I add any html anywhere on the page it appears and the zero goes away but am getting nothing from the text file. I tried putting the .txt file in the cgi-bin and also the httdocs area with the same results.
2:44 am on June 18, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


have you tried running your cgi script from the command line to see the actual output?

also, unless i'm missing something the %colors hash appears to be unevaluated in your script.
7:56 am on June 18, 2011 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 96
votes: 0


phranque....not sure how one goes about running from the command line. Also, how would I go about evaluating the %colors hash? I'm wanting to learn. I'm just getting over the shock of making everything in the strict mode.
9:09 am on June 18, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


if your script file were named colordb.cgi and you are in the directory containing your script you could enter the following command at the shell prompt:
/usr/bin/perl colordb.cgi

and your print statements would appear on standard output.

regarding hash evaluation, TMTOWTDI:

my %colors = ( ff0000 => 'red' 000000 => 'black' ffffff => 'white' );
or
my %colors = (); $colors{ff0000} = 'red'; $colors{000000} = 'black'; $colors{ffffff} = 'white';
or
my %colors = ('ff0000','red','000000','black','ffffff','white');
or as an anonymous hash reference
my $colors_ref = { ff0000 => 'red' 000000 => 'black' ffffff => 'white' };
11:25 am on June 18, 2011 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 96
votes: 0


I tried all you mentioned to no avail. I typed in the filename following the shebang and nothing changed. Not sure if I have access to the shell prompt though as I'm on a hosted server---searched their knowledge base and found nothing on the subject.
3:19 pm on June 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


colors.txt is as follows


On further investigation, it looks to me like you're storing it in the DB file wrong. DB_FILE reads database formats - you're storing an array.

Try a text file with tab delimited values?

red ff0000
black 000000
white ffffff

Or vice versa . . . presuming you want keys on the left, values on the right . . .
12:35 am on June 21, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


I typed in the filename following the shebang and nothing changed.

the shebang is the first line of your script.
the shell prompt is what you see when you open a command shell.

if you're on a shared host you might not have shell access.

i don't see anything in your script binding the %color hash to anything or assigning any values to it.
therefore it appears you declared the hash and then tried to access a key/value that doesn't exist.
1:55 am on June 21, 2011 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 96
votes: 0


rocknbil: I like the idea of a text file with tab delimited values but how do I access the values?

phranque: I've changed the "tie" statement to:
tie (%colors,"DB_File",$dbfile, O_CREAT|O_RDWR, 0777)or die "Can't open $dbfile: $!\n";
When accessed at [teresafbc.org...] which includes the following line:
print"The opposite of $colors{'000000'} is $colors{'ffffff'}";
The text shows but $colors{'000000'} and $colors{'ffffff'} does not.
11:08 am on June 21, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


you should use DB_File to create and write the file for you:

#!/usr/bin/perl use strict; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use DB_File; use Fcntl; print "Content-Type:text/html\n\n"; my %colors; my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt "; # delete any existing colors.txt file # or remove this unlink if you are adding to an existing db file unlink $dbfile; # tie %colors hash to and create db file in color.txt # keep defaults in this case tie (%colors, "DB_File", $dbfile) or die "Can't open $dbfile: $!\n"; # (re)evaluate db in colors.txt by evaluating key/value pairs in %colors hash as follows %colors=( 'ff0000' => 'red' '000000' => 'black' 'ffffff' => 'white' ); print "$colors{'ffffff'}"; print "$colors{'000000'}"; print "$colors{'ff0000'}";

then you can reverse-engineer the colors.txt file!
=8)
4:08 pm on June 21, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


^ ^ ^ Yeah that,


rocknbil: I like the idea of a text file with tab delimited values but how do I access the values?


The module should to it for you, just create a file like I showed by example with tabs between key/value and give it a try, see what happens.
3:39 am on June 22, 2011 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 96
votes: 0


Must be something on the server end. I uploaded the script as you wrote it out and as long as the hash was on the script page I could get the values but trying to get them from the txt file only prints out a 0 on the screen unless I have html in which case the 0 goes away and the html prints. The server complained about using tabs and no commas.

I contacted the server host on the matter and their last reply was "Please note that since we are a hosting provider unfortunately we do not provide support for designing website and customerís coding related issues. I have uploaded a test script at [@#$%^...] and it is showing fine. Please verify the scripts once again from your end."

The only thing they did hwas make a basic "Hi World" type of script and told me all is well on their end. Actually, when I first uploaded the script it was the same as what phranque presented (I got it out of the perl cookbook with a few minor adjustments for the situation. The deal about english.db not being was a dumb mistake on my part which happened after the initial upload).

Maybe its time to save up and get another host. I would like to thank you so much for all your help.
9:41 am on June 22, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


your host is right.
as long as they can demonstrate that a "hello world" script works the application-specific work is yours.


ok try this.
create one script named createdb.pl:

#! /usr/bin/perl use strict; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use DB_File; use Fcntl; print "Content-Type:text/html\n\n"; my %colors; my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt "; # delete any existing colors.txt file unlink $dbfile; # tie %colors hash to and create db file in color.txt tie (%colors, "DB_File", $dbfile) or die "Can't open $dbfile: $!\n"; # (re)evaluate db in colors.txt by evaluating key/value pairs in %colors hash as follows %colors=( 'ff0000' => 'red' '000000' => 'black' 'ffffff' => 'white' ); print "$colors{'ffffff'}"; print "$colors{'000000'}"; print "$colors{'ff0000'}"; untie %colors;


then create another script named readdb.pl:

#! /usr/bin/perl use strict; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use DB_File; use Fcntl; print "Content-Type:text/html\n\n"; my %colors; my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt "; # tie %colors hash to db file in color.txt tie (%colors, "DB_File", $dbfile) or die "Can't open $dbfile: $!\n"; # access db in colors.txt by accessing keys in %colors hash as follows print "$colors{'ffffff'}"; print "$colors{'000000'}"; print "$colors{'ff0000'}"; untie %colors;


and then run the scripts in that order - they should show the same results.
12:44 am on June 23, 2011 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Feb 11, 2010
posts: 96
votes: 0


Problem solved and many thanx. An interesting phenomina taking place here. When the text file is created with the script it reads them just fine but if the text file is appended without the scipt its a no go.