Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning to z drive in webspace explorer script

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

I am currently developing V1.0 of a Webspace Explorer Script. The script itself is very long, and I am planning on cutting it down dramatically. However for the moment I just want to get every aspect functioning. I am having a problem with one section, though the syntax is fine and I can't see what I am doing wrong.

I have an image button (name="refresh_webspace_explorer") and when it is pressed I want the path to go back to the z drive (the initial drive, when the user first logs in). However when I click this button, I recieve an Error 500 although I am baffled why this is so...

In breakdown this is what this aspect of the script does...

Code:
#declare variables
my ($refresh_website_explorer, $drive, $initial_path);

#name of $drive
$drive = "z";

#path to server
$initial_path = MYPATH;

#Link to form to pick up buttons name
$refresh_webspace_explorerb = 
param('refresh_webspace_explorer');

#Check if the button has been pressed, and if so tell what path to open and print

if ($refresh_webspace_explorerb) {
$path = $initial_path .''. $drive;
$path_carry = $drive;
}

#use $path to open the z drive, and use $path_carry to print z into the form textbox

So this gives you can idea of what i'm trying to achieve, but obviously the above code is within other code. So below i've printed most of my script (except some of the html output and also some of the other buttons functions)...

Code:
##### Problems/Issues
# target for page or directory doesn't work - if page then should open in a new window
# printing the result of the foreach $_ (@_) onto a HTML template
# Once completed create new directory - run script to open the directory it was put into (add $directory_name)
# Once a page or directory has been opened it goes to that direc/page, I need it to return to 1 level back
# must let users know that to remove page/directory, all its contents must be deleted first

########## Opening Tags
#! /usr/bin/perl
use strict;
use CGI ':standard';

########## Declare variables
my ($initial_path, $open1, $open2, $path_carry, $openb1, $openb2, $path, $openb_target, @view_page, @directory_contents1, @directory_contents2, $directory_contents2_length, $direc_or_page, $drive);
my ($up_one_levelb, $refresh_webspace_explorerb);
my ($create_directoryb, $create_directoryb_execute, $create_directoryb_path, $directory_name);
my ($create_pageb, $create_pageb_execute, $create_pageb_path, $page_name);
my ($delete_direc_pageb, $delete_direc_pageb_execute, $delete_direc_pageb_path);
my ($rename_direc_pageb);

########## Connect to form
##### Path textboxes
$open1 = param('OpenText1');
$open2 = param('OpenText2');
##### Path open buttons
$openb1 = param('openb1');
$openb2 = param('openb2');
##### Top options
$up_one_levelb = param('up_one_level');
$refresh_webspace_explorerb = param('refresh_webspace_explorer');

$create_directoryb = param('create_directoryb');
$create_directoryb_execute = param('create_directoryb_execute');
$create_directoryb_path = param('create_directoryb_path');
$directory_name = param('directory_name');

$create_pageb = param('create_pageb');
$create_pageb_execute = param('create_pageb_execute');
$create_pageb_path = param('create_pageb_path');
$page_name = param('page_name');

$delete_direc_pageb = param('delete_direc_pageb');
$delete_direc_pageb_execute = param('delete_direc_pageb_execute');
$delete_direc_pageb_path = param('delete_direc_pageb_path');

$rename_direc_pageb = param('rename_direc_pageb');

######### Path to containing drive folder (z)
$initial_path = "MYPATH";
######### Drive label
$drive = "z";

########## Check which open button was pressed
if ($openb1) {
##### The path is...
$path = $initial_path .''. $open1;
$path_carry = $open1;
}
if ($openb2) {
##### The path is...
$path = $initial_path .''. $open1 .''. $open2;
$path_carry = $open1 .''. $open2;
}

if ($refresh_webspace_explorerb) {
$path = $initial_path .''. $drive;
$path_carry = $drive;
}

########## Create directory path and carry
if ($create_directoryb || $create_pageb  || $delete_direc_pageb) {
$path_carry = $open1 .''. $open2;
}


if ($create_directoryb_execute) {
$path = $create_directoryb_path;
$path_carry = $create_directoryb_path;
}
if ($create_pageb_execute) {
$path = $create_pageb_path;
$path_carry = $create_pageb_path;
}
##############################################################################
##### Extract component parts from file
##### And then do if delete_direc_pageb one

if ($delete_direc_pageb_execute) {
$path = $delete_direc_pageb_path;
$path_carry = $delete_direc_pageb_path;
}

########## if one of the open buttons is pressed or a button which requires contents to print etc
if ($openb1 || $openb2 || $up_one_levelb || $refresh_webspace_explorerb || $create_directoryb_execute || $create_pageb_execute || $delete_direc_pageb_execute) {

#################### If a page is being opened
if ($path =~ m/.htm/){
$openb_target = qw(_blank); 
open (LOG, "$path") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
@view_page = <LOG>;
close (LOG) || Error ('close', 'file');
print "Content-type: text/html\n\n";
print "@view_page";
}
#################### End if page

#################### If a directory is being opened
else {
$openb_target = qw(_self); 
########## Open directory
opendir (LOGDIR, "$path") || Error ('open', 'directory');
@directory_contents1 = readdir (LOGDIR);
closedir (LOGDIR);
##### Sort contents into alphabetical order
@directory_contents2 = sort @directory_contents1;
##### Remove the . and the .. from array
shift(@directory_contents2);
shift(@directory_contents2);
##### Find length of $directory_contents2
$directory_contents2_length = @directory_contents2;

########## Print contents
##### Print form at the top
print "Content-type: text/html\n\n";
print <<"HTML code";
<HTML>
<head>
<title>Webspace Explorer Control Panel</title>

<script language="javascript">
function enableField()
{ 
document.form1.OpenText1.disabled=false;
document.form1.OpenText2.disabled=false;  
}
</script>
<script language="javascript">
function disableField()
{ 
document.form1.OpenText1.disabled=true;
document.form1.OpenText2.disabled=true; 
}
</script>
</head>

<body>
<FORM ACTION="script.pl" METHOD=POST NAME="form1">
<p><input type="submit" value="+ Directory" name="create_directoryb" target="_blank">&nbsp;&nbsp;&nbsp;<input type="submit" value="+ Page" name="create_pageb" target="_blank">&nbsp;&nbsp;&nbsp;<input type="submit" value="- Direc/Page" name="delete_direc_pageb" target="_blank">&nbsp;&nbsp;&nbsp;<input type="submit" value="Rename Direc/Page" name="rename_direc_pageb" target="_blank"></p>
<p><input type="image" src="Images/UpOneLevel.bmp" name="up_one_level">&nbsp;&nbsp;&nbsp;<input type="image" src="Images/RefreshWebspaceExplorer.bmp" name="refresh_webspace_explorer"></p>
<p><input type="text" name="OpenText1" size="50" value="$path_carry" readonly="true"><input type="submit" value="Open" name="openb1"></p>
<p><input type="text" name="OpenText2" size="50" readonly="true"><input type="submit" value="Open" name="openb2"></p>
</FORM>
</body>
</HTML>
HTML code
##### Print directory contents beneath
##### if there is no contents then indicate
if ($directory_contents2_length == 0) {
print "<font face=arial size=2><P>Directory <b>$path_carry</b> is empty.</font>";
}
else {
foreach (@directory_contents2) {
##### Check if directory or page
if ($_ =~ m/.htm/){ $direc_or_page = 'HTML Page'; }
else { $direc_or_page = 'Directory'; }
##### Main
print <<"HTML code";
<HTML>
<HEAD>
</HEAD>
<BODY>
<table>
<tr>
<td width="80" align="center" bgcolor="#E4E4E4"><input type=radio name="radiobuttongroup" value="/$_" OnClick="document.form1.OpenText2.value=this.value;"></td>
<td width="400" align="center" bgcolor="#E4E4E4"><font size="2" face="Arial">$_</font></td>
<td width="140" align="center" bgcolor="#E4E4E4"><b><font face="Arial" size="2">$direc_or_page</font></b></td>
</tr>
</table>
</BODY>
</HTML>
HTML code
}
##### End no contents indication
}
#################### End if directory
}
########## End if one of the open buttons is pressed
}

########## File opening error
sub Error {
print "Content-type: text/html\n\n";
print "<font face=arial size=2><P>Cannot Open File/Directory... Error 1.</font>";
print "<font face=arial size=2><P>$open1</font>";
print "<font face=arial size=2><P>$open2</font>";
print "<font face=arial size=2><P>$create_directoryb_path$directory_name</font>";
print "<font face=arial size=2><P>$create_pageb_path$page_name.htm</font>";
exit;
}

Any ideas why I recieve an Error 500, all that should happen is that the z drive is opened, and its contents printed onto the html webspace explorer template, and the $drive (which is "z") is printed into the textbox??? If you look, openb1 and openb2 do exactly the same thing, except they get their path in a slightly different way, however they work absolutely fine.

Thanks once again, what would I do without tek-tips :p

Chris
 
Chances are you are hitting an error condition which is causing a print statement to happen before your html header. Look in the apache logs or run it from command line and you will probably see the error or move your html header upward in your script if you want to see the error on the screen. You should look at the CGI documentation to because a lot of what you are doing by hand can be done very easily by CGI calls.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Thanks travs for responding,

What do you mean when you say run it from command line?

I will have a look through the script again to see if I am hitting an error condition, although I really can't see whats wrong.

Here is the output result from a CGI Monitor

Code:
- 2 CGI returned nonzero status
/MYPATH: : command not found
/MYPATH: use: command not found
/MYPATH: : command not found
/MYPATH: use: command not found
/MYPATH: : command not found
/MYPATH: : command not found
/MYPATH: line 14: syntax error near unexpected token `($initial_path,'
/MYPATH: line 14: `my ($initial_path, $open1, $open2, $path_carry, $openb1, $openb2, $path, $openb_target, @view_page, @directory_contents1, @directory_contents2, $directory_contents2_length, $direc_or_page, $drive);'
STDOUT OK STDERR OK
Client 
Engine mshtml 
Version 7.0 
Platform win 
System winxp 
Color 32 
Width 1280 
Height 800 
Flash 9 
All 
Request-Complete 2047ms 
Server 
Server-Complete 1641ms (80%) 
HTML 
HTML-Complete 406ms (20%) 
HTML-Details 
HTML-Head 93ms (5%) 
Page-Head 0ms (0%) 
Page-Body 63ms (3%) 
Post-Exec 78ms (4%)

I'm also going to search google for CGI calls.

Thanks
 
There is little chance anyone will want to check through that mass of unformatted code you posted.

Always add:

use CGI::Carp qw/fatalsToBrowser/;

when debugging a CGI script.

you can optionally add warningsToBrowser:

use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

see the CGI::Carp module for further instructions

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
/MYPATH: line 14: syntax error near unexpected token `($initial_path,'

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
why do you concatenate your strings with an empty string in between them?

$path = $initial_path .''. $open1 .''. $open2;

should be:

$path = $initial_path . $open1 . $open2;

or:

$path = "$initial_path$open1$open2";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
is this a constant?

$initial_path = MYPATH;

later you have it quoted:

$initial_path = "MYPATH";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
If I use a syntax checker to check script, there is no syntax error. And theres nothing wrong with line 14, its just a line declaring variables. I don't understand what a token is though.

Yeah sorry, my code is a mass of mess at the moment.

I have tried using use CGI::Carp qw/fatalsToBrowser/; and also adding -x to the end of the shabang. But how do I view errors, will they get printed if there is an error?
 
Sorry Kevin,

I have gone back to an older version of the script. You corrected me last time with the .''. and I have removed them in the newer script...

MYPATH is simply the path on the server, but I don't want to display it when I post on a forum... It looks something like this

/kunden/path/system/k4444932/action



 
I can't see a problem with the indicated line. Try running your script on a differnt system and see what happens.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hey Kev,

Sorry on a different system? I am using a 1and1 hosted linux web server, and I don't have perl installed on my computer. Is that what you mean?
 
I think I have fixed my problem...

I haven't properly tested it yet, but I tried using a normal submit button rather than an image one...

So instead of...

<input type="image" src="Images/RefreshWebspaceExplorer.bmp" name="refresh_webspace_explorerb">

I've used...

<input type="submit" value="refresh_webspace_explorer" name="refresh_webspace_explorerb" target="_blank">

Is there something wrong in which the way I have used an image to submit data?

Chris
 
I think I see the problem; You need the value attribute of the <input> tag defined:

<input type="image" src="Images/RefreshWebspaceExplorer.bmp" value="refresh_webspace_explorer" name="refresh_webspace_explorerb">

 
Actually, on 2nd glance, I'm wandering if you have things back-to-front someplace:

You say your submit button is:

<input type="submit" value="refresh_webspace_explorer" name="refresh_webspace_explorerb" target="_blank">

which in effect, sends 'refresh_webspace_explorerb=refresh_webspace_explorer' to your script.

But your perl code is reading:

$refresh_webspace_explorerb = param('refresh_webspace_explorer');

which says it is looking for something like 'refresh_webspace_explorer=refresh_webspace_explorerb'

Back-to-front.

If it was just you mixing them up on the posting here, then switch the name/value on the example I gave too.
 
input type image tags send back xy coordinates, any value="" attribute is ignored. This:

<input type="image" src="Images/RefreshWebspaceExplorer.bmp" name="refresh_webspace_explorerb">

should send:

refresh_webspace_explorerb.x = nn
refresh_webspace_explorerb.y = nn

where nn is the location of the cursor over the image in pixels from the top left corner of the image.

So that may need to be taken into account when parsing the form paramaters. I am not sure if the CGI module does some magic though on input type image tags or if it follows this protocol strictly when parsing that particular form widget.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I read in one of my books that you cannot carry a value whilst using an image submit button :s.

I do not need to carry a value with my button, I just want to check if the button has been pressed therefore I only need to include the name attribute.

Sorry for the confusion with names and values, the image button code is meant to look like this...

<input type="image" src="Images/RefreshWebspaceExplorer.bmp" name="refresh_webspace_explorerb">

and the normal one...

<input type="submit" name="refresh_webspace_explorerb">

Brigmar, I just tried removing any value attributes altogether and it still didn't work :s. Confusing eh

Kevin, i'm going to have a look for a module which may help me with this.

Thanks for all your help :)

 
Either of the following lines would work for the image:

param('refresh_webspace_explorerb.x')
param('refresh_webspace_explorerb.y')

If either of those evaluates to something, then you know the button was pressed.

 
yes, the CGI module says:

Fetch the value of the button this way: $x = param('button_name.x'); $y = param('button_name.y');

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top