Solved

Building a function from code in a php file

Posted on 2008-06-15
64
287 Views
Last Modified: 2008-06-15
Hi guys,

I have the following code snippet that id love to pull out of this file and make into its own file.
Id then just like to call this function by including the file in this php file where the code is currently.

The following is the file where I have the code snippet currently that Id like to turn into a function, and place in its own file.

====================192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php

<!-- =================== Below is the start of the code snippet Id like to turn into a function -->
 <?php
 $self = $_SERVER['PHP_SELF'];
 $path_arr=explode("_",$self);
 $path="";
 for($i=1;$i < count($path_arr)-1;$i++){
 $path .="/".$path_arr[$i];
 }
$uploaded_files_dir="/simeswiki".$path;
?>
<!-- Below is there the $uploaded_files_dir is placed.... -->
<!--
<div id="upload" style="float:left" margin:0; padding:0;>
<!-- THE FOLLOWING WILL PASS THE VALUE OF #uploaded_files_dir to the cgi script, so it can use it. -->
<iframe src = "/cgi-bin/filechucker_focushere.cgi?<?=$uploaded_files_dir?>" width="490px" height="650px"> </iframe>
</div>

=====================================================
###############################
WHAT I HAVE TRIED WITH NO LUCK:
###############################

All my php files that I want to include the "to be made function file" are in the following path:
/var/www/html/simeswiki

My included files are in the following path:
/var/www/html/simeswiki/includes

I tried doing the following but this doesnt work........

=========================== /var/www/html/simeswiki/includes/focushere_functions.php
<?php
 $self = $_SERVER['PHP_SELF'];
 $path_arr=explode("_",$self);
 $path="";
 for($i=1;$i < count($path_arr)-1;$i++){
 $path .="/".$path_arr[$i];
 }
$uploaded_files_dir="/simeswiki".$path;
?>
=====================================================

Then, in my php file, I did the following......

==============http://192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php
<head>
<?php include("includes/focushere_functions.php"); ?>
</head>
<div id="upload" style="float:left" margin:0; padding:0;>
<!-- THE FOLLOWING WILL PASS THE VALUE OF #uploaded_files_dir to the cgi script, so it can use it. -->
<iframe src = "/cgi-bin/filechucker_focushere.cgi?<?=$uploaded_files_dir?>" width="490px" height="650px"> </iframe>
</div

==========================================
The page loads but nothing is displayed.
I dont get any errors but my suspicion is, is that the  $self = $_SERVER['PHP_SELF']; is now not acting on the file I want, but executing from the location of focushere.php instead of the current php page where I have included it...which is what I need.

Any help on this would be greatly appreciated.
The code if left in the http://192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php file works fine.
The reason I want to turn it into a function, is because I want to be able to use the code on multiple php files.
Thank you everyone.
0
Comment
Question by:Simon336697
  • 41
  • 16
  • 5
  • +1
64 Comments
 
LVL 14

Expert Comment

by:ali_kayahan
ID: 21788762
I really dont get what are you trying to achieve, You didnt define a function above,and whats the main purpose of this code below ;
 $self = $_SERVER['PHP_SELF'];
 $path_arr=explode("_",$self);
 $path="";
 for($i=1;$i < count($path_arr)-1;$i++){
 $path .="/".$path_arr[$i];
 }
$uploaded_files_dir="/simeswiki".$path;
 If you inform me about your goal ,i will try to compile a function as you need...
0
 
LVL 14

Assisted Solution

by:ali_kayahan
ali_kayahan earned 50 total points
ID: 21788794
The code below is the function version of your script ;
 function myFunc($str) {
 $path_arr = explode("_",$str) ;
 $path ="" ;
        for($i=1;$i < count($path_arr)-1;$i++){
       $path .="/".$path_arr[$i];
       }
return $path;
 }
it needs 1 argument $_SERVER['PHP_SELF'] and it returns path as value , the sample usage of it ;
$path = myFunc($_SERVER['PHP_SELF']) ;
   And if you save it in a php file for ex ; myfunctions.php you should reffer it like ;
require_once('myfunctions.php') ;//Assume that the file is under same folder.
$path = myFunc($_SERVER['PHP_SELF']) ;
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788835
Hi ali,
Thanks for your help.
This is what Im trying to do.
Sorry im not a good coder.
Please ignore the above code as Im now having problems with it, so Ill start from scratch here.

I have a file called:
192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php
The location of the above is /var/www/html/simeswiki
My document web root is /var/www/html
My included files are in /var/www/html/simeswiki/includes
My cgi-bin folder is in /var/www/cgi-bin

All my php files that I would like to use the below ability are placed in:
/var/www/html/simeswiki
So if i include any functions in these php files, it would be:
<?php include("includes/<functionfile>.php");

-------------------------------
WHAT IM TRYING TO DO:
-------------------------------
In my cgi-bin folder, I have 2 files called:
1) filechucker_focushere_prefs.cgi
and
2) filechucker_focushere.cgi

The filechucker_focushere_prefs.cgi is a preferences file that I use to set the path to my uploads folder.
This is the only configuration I make to the above 2 files, and you can see it below..
Currently, Im having to hard code this configuration as follows.

================================ /var/www/cgi-bin/filechucker_focushere_prefs.cgi
$PREF{uploaded_files_dir}  = '/simeswiki/it/hware/dell';

-----------------------------------
In the above, I dont want to have to hard code this path for $PREF{uploaded_files_dir}
I want to be able to generate this $PREF{uploaded_files_dir} according to what php page im currently viewing.

----------------
EXAMPLES:
---------------
For example, as all my php pages are located in /var/www/html/simeswiki, and follow the SAME naming convention, I want to do the following.

1) If im viewing the following page..
192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php
I want the $PREF{uploaded_files_dir} to be set to the following in the /var/www/cgi-bin/filechucker_focushere_prefs.cgi file, as follows...

$PREF{uploaded_files_dir}  = '/simeswiki/it/hware/dell';

2) If im viewing the following page..
192.168.2.100/simeswiki/menu4_it_auditing_disk_focusHERE.php
I want the $PREF{uploaded_files_dir} to be set to the following in the /var/www/cgi-bin/filechucker_focushere_prefs.cgi file, as follows...

$PREF{uploaded_files_dir}  = '/simeswiki/it/auditing/disk';

--------------------------------
You can see there is a pattern in the format....
All my php pages are in the naming format below...
192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php
192.168.2.100/simeswiki/menu4_it_auditing_disk_focusHERE.php
That is,
<server>/simeswiki/menu4_it_<category>_<subcategory>_focusHERE.php
What I want to be able to do, is to generate the $PREF{uploaded_files_dir} for
/var/www/cgi-bin/filechucker_focushere_prefs.cgi
when I view one of these php pages, by pulling apart its filename
eg. menu4_it_hware_dell_focusHERE.php
and transforming it to the below format:
$PREF{uploaded_files_dir}  = '/simeswiki/it/auditing/disk';  // for example.

--------------------------------------
WHAT THESE PHP PAGES DO:
--------------------------------------
Here is an example of 192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php

=============================================== menu4_it_hware_dell_focusHERE.php
<div id="container">

        <div id="titlecontent">
                <h1><?php echo $title ?></h1>
                <h4><script src="includes/function_date.js"></script></h4>
        </div>

        <div id="maincontent">

                <div id="upload" style="float:left" margin:0; padding:0;>
                        <iframe src = "/cgi-bin/filechucker_focushere.cgi?" width="490px" height="650px"> </iframe>
                </div>
                <div id="download" style="position:fixed">
                        <iframe src = "/cgi-bin/filechucker_focushere.cgi?action=listfiles" width="750px" height="650px"> </iframe>
                </div>
        </div>
</div>

======================================== And in the prefs.cgi file, I have hardcoded....
$PREF{uploaded_files_dir}  = '/simeswiki/it/hware/dell';
See how in the above, it is the folder that follows the naming convention used for my php files  eg. simeswiki/menu4_it_hware_dell_focusHERE.php
That is,
there is a folder called /var/www/html/simeswiki/it/hware/dell

I hope this makes sense.
0
 
LVL 4

Assisted Solution

by:afzz
afzz earned 150 total points
ID: 21788852
Hi do the following in focushere_functions.php

and include as you have done. Then for the iframe code do this

<iframe src = "/cgi-bin/filechucker_focushere.cgi?<?=get_path($_SERVER['PHP_SELF'])?>" width="490px" height="650px"> </iframe>

<?php

function get_path($self){

 $path_arr=explode("_",$self);

 $path="";

 for($i=1;$i < count($path_arr)-1;$i++){

 $path .="/".$path_arr[$i];

 }

return "/simeswiki".$path;

}

?>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 300 total points
ID: 21788890
Try this:

 <?php
 $self = explode("/",$_SERVER['PHP_SELF']);
 $path_arr=explode("_",$self[count($self)-1]);
 $path="";
 for($i=1;$i < count($path_arr)-1;$i++){
 $path .="/".$path_arr[$i];
 }
$uploaded_files_dir="/simeswiki".$path;
echo $uploaded_files_dir;
?>
0
 
LVL 82

Expert Comment

by:hielo
ID: 21788892
If you prefer a function:
 <?php
 function getIncludePath()
 {
 $self = explode("/",$_SERVER['PHP_SELF']);
 $path_arr=explode("_",$self[count($self)-1]);
 $path="";
 for($i=1;$i < count($path_arr)-1;$i++){
 $path .="/".$path_arr[$i];
 }
$uploaded_files_dir="/simeswiki".$path;
return $uploaded_files_dir;
}
echo getIncludePath();
?>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788900
You guys are the greatest Ill report back very shortly :>)
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788927
Hi hielo!

Im trying your one at the moment.

With your code....

 <?php
 function getIncludePath()
 {
 $self = explode("/",$_SERVER['PHP_SELF']);
 $path_arr=explode("_",$self[count($self)-1]);
 $path="";
 for($i=1;$i < count($path_arr)-1;$i++){
 $path .="/".$path_arr[$i];
 }
$uploaded_files_dir="/simeswiki".$path;
return $uploaded_files_dir;
}
echo getIncludePath();
?>


=========================================
I placed your code into a file called /var/www/html/simeswiki/focushere_functions.php
Then I included it into my php page called
192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php
with
<?php include("focushere_functions.php");?>

In this same page, I build an iframe to house the uploader form as follows...

<div id="container">

        <div id="titlecontent">
                <h1><?php echo $title ?></h1>
                <h4><script src="includes/function_date.js"></script></h4>
        </div>

        <div id="maincontent">

                <div id="upload" style="float:left" margin:0; padding:0;>
                        <iframe src = "/cgi-bin/filechucker_focushere.cgi?" width="490px" height="650px"> </iframe>
                </div>
                <div id="download" style="position:fixed">
                        <iframe src = "/cgi-bin/filechucker_focushere.cgi?action=listfiles" width="750px" height="650px"> </iframe>
                </div>
        </div>
</div>

=========================================
It is in the cgi-bin/filechucker_focushere_prefs.cgi file that the
$PREF{uploaded_files_dir}  is defined.

Do I have to call your function in my php page in addition to including it?
That is, how will my cgi-bin/filechucker_focushere_prefs.cgi file know that I have set the
$PREF{uploaded_files_dir} in it?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788932
Also, sorry, what do I set
 $PREF{uploaded_files_dir}  to be equal to within the cgi-bin/filechucker_focushere_prefs.cgi file itself?

0
 
LVL 4

Expert Comment

by:afzz
ID: 21788933
what more you want. try this it works
/var/www/html/simeswiki/focushere_functions.php

====================================

<?php

function get_path($self){

 $path_arr=explode("_",$self);

 $path="";

 for($i=1;$i < count($path_arr)-1;$i++){

 $path .="/".$path_arr[$i];

 }

return "/simeswiki".$path;

}

?>
 

192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php

========================================================
 

<?php include("focushere_functions.php");?>
 

AND
 
 

<div id="container">
 

        <div id="titlecontent">

                <h1><?php echo $title ?></h1>

                <h4><script src="includes/function_date.js"></script></h4>

        </div>
 

        <div id="maincontent">
 

                <div id="upload" style="float:left" margin:0; padding:0;>

                        <iframe src = "/cgi-bin/filechucker_focushere.cgi?<?=get_path($_SERVER['PHP_SELF'])?>" width="490px" height="650px"> </iframe>

                </div>

                <div id="download" style="position:fixed">

                        <iframe src = "/cgi-bin/filechucker_focushere.cgi?action=listfiles" width="750px" height="650px"> </iframe>

                </div>

        </div>

</div>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788935
If I dont set $PREF{uploaded_files_dir} to anything, or comment it out completely in the prefs.cgi file, i get the following error..
Error: you haven't set $PREF{uploaded_files_dir}.

Do I have to, as afzz suggest, pass it somehow as a query string in the iframe?
But then the iframe is pointing to the form eg.to filechucker_focushere.cgi, and not to filechucker_focushere_prefs.cgi, so I dont know how to pass the $PREF{uploaded_files_dir} to the prefs file.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21788938
Instead of:
================================ /var/www/cgi-bin/filechucker_focushere_prefs.cgi
$PREF{uploaded_files_dir}  = '/simeswiki/it/hware/dell';

Try:
$PREF{uploaded_files_dir}=getIncludePath();
0
 
LVL 4

Expert Comment

by:afzz
ID: 21788943
filechucker_focushere.cgi reads the prefs from filechucker_focushere_prefs.cgi. since you want that set dynamically by the php script, you have passed it into the cgi file.

I had provided you the code to set the $PREF{uploaded_files_dir}=$ENV{QUERY_STRING}, so you are setting the variable, only dynamically
0
 
LVL 4

Expert Comment

by:afzz
ID: 21788945
Quote
_______
Instead of:
================================ /var/www/cgi-bin/filechucker_focushere_prefs.cgi
$PREF{uploaded_files_dir}  = '/simeswiki/it/hware/dell';

Try:
$PREF{uploaded_files_dir}=getIncludePath();
_________

hielo, that is a cgi file not php. Your function went into the php. the code works only that he wanted it to be a function now.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788955
afzz,

Im doing your code exactly....

And im getting the right path....but when trying to upload, im getting the following..

Error: your settings for $PREF{uploaded_files_dir} and $PREF{uploaded_files_dir_is_in_docroot} result in $PREF{uploaded_files_realpath} being set to '/var/www/htmlaction=incoming&/simeswiki/it/hware/dell&serial=b2d3423e288f347bf1ec9a16c1f331725f8a882d&items=1', but that path does not exist.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788967
afzz,
If I leave everything you have done alone, except comment out the following in the prefs.cgi file..

#$PREF{uploaded_files_dir}                              = $ENV{QUERY_STRING};

and change it to

$PREF{uploaded_files_dir}                               = '/simeswiki/it/hware/dell';

it works, with the included function file and query string still in the iframe.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788976
I dont think the $ENV{QUERY_STRING} is being read properly.
0
 
LVL 4

Expert Comment

by:afzz
ID: 21788978
i understand what you want. I'll go through your code and provide you a solution soon.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788982
My correction....it is, but when I try and upload, it falls over.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21788983
>>hielo, that is a cgi file not php
Ahh, yes. Change the iframe to this:

<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=getIncludePath();?>" width="490px" height="650px"> </iframe>

and your cgi to this:
$PREF{uploaded_files_dir}=$ENV{thePath};
0
 
LVL 82

Expert Comment

by:hielo
ID: 21788986
>>I dont think the $ENV{QUERY_STRING} is being read properly.
I think the QUERY_STRING contains more than just the path you are after. It sounds like your url has some other parameters as well. You need to "name" your path parameter and extract just that. That's why I used "thePath=..."
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21788997
hielo,

Im getting a completely blank page now.

View source in the php page is...

<div id="upload" style="float:left" margin:0; padding:0;>
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<br />
<b>Fatal error</b>:  Call to undefined function getIncludePath() in <b>/var/www/html/simeswiki/menu4_it_hware_dell_focusHERE.php</b> on line <b>25</b><br />

Line 25 is..
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=getIncludePath();?>" width="490px" height="650px"> </iframe>

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789007
My apologies......forget the above.....i had commented it out.

After uncommenting the function, and loading the page...im getting..

Error: you haven't set $PREF{uploaded_files_dir}.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789009
I have set it in the prefs.cgi file as follows..

$PREF{uploaded_files_dir}                               = $ENV{thePath};
0
 
LVL 82

Expert Comment

by:hielo
ID: 21789011
>>Call to undefined function getIncludePath()
Did you use the function I gave you? Notice that afzz called his function get_path and you need to pass it a parameter. I named my function getIncludePath and you don't need to pass anything.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789016
hielo,
I used your function..
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789023
Things used currently..................

======================= /var/www/cgi/filechucker_focushere_prefs.cgi
$PREF{uploaded_files_dir}                               = $ENV{thePath};

======================= /var/www/cgi/focushere_functions.php
<?php

function getIncludePath()
{
$self = explode("/",$_SERVER['PHP_SELF']);
$path_arr=explode("_",$self[count($self)-1]);
$path="";
for($i=1;$i < count($path_arr)-1;$i++){
$path .="/".$path_arr[$i];
}
$uploaded_files_dir="/simeswiki".$path;
return $uploaded_files_dir;
}
echo getIncludePath();
?>

============================= /var/www/html/simeswiki/menu4_it_hware_dell_focusHERE.php
<div id="container">

        <div id="titlecontent">
                <h1><?php echo $title ?></h1>
                <h4><script src="includes/function_date.js"></script></h4>
        </div>

        <div id="maincontent">


                <div id="upload" style="float:left" margin:0; padding:0;>
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=getIncludePath();?>" width="490px" height="650px"> </iframe>





                </div>

                <div id="download" style="position:fixed">
                        <iframe src = "/cgi-bin/filechucker_focushere.cgi?action=listfiles" width="750px" height="650px"> </iframe>
                </div>

        </div>

</div>
=================
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789024
Sorry, im including the function in menu4_it_hware_dell_focusHERE.php as well.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789054
hielo,

For $ENV{thePath};

this should be /simeswiki/it/hware/dell

if the filename im viewing is
menu4_it_hware_dell_focusHERE.php

0
 
LVL 82

Expert Comment

by:hielo
ID: 21789056
/var/www/html/simeswiki/menu4_it_hware_dell_focusHERE.php
should:
include('/var/www/cgi/focushere_functions.php');

also, get rid of:
echo getIncludePath();

that line was just for you to test the function.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21789062
>>For $ENV{thePath}; ...
Correct. That is what hte function does.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21789072
To clarify matters, this:
Call to undefined function getIncludePath()
means you are not including the file where the function is defined. Since you left:
echo getIncludePath();

and are not seeing  /simeswiki/it/hware/dell anywhere, that is further proof that you are not including the file.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Simon336697
ID: 21789086
Im wondering if im doing the following wrong..

 <?php
 function getIncludePath()
 {
 $self = explode("/",$_SERVER['PHP_SELF']);
 $path_arr=explode("_",$self[count($self)-1]);
 $path="";
 for($i=1;$i < count($path_arr)-1;$i++){
 $path .="/".$path_arr[$i];
 }
$uploaded_files_dir="/simeswiki".$path;
return $uploaded_files_dir;
}
?>

Ive put the entire above in focushere_functions.php

Should

$uploaded_files_dir="/simeswiki".$path;
return $uploaded_files_dir;
}
there be the last } here?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789099
Still saying

Error: you haven't set $PREF{uploaded_files_dir}.

View source is:

            <div id="upload" style="float:left" margin:0; padding:0;>
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=/simeswiki/it/hware/dell" width="490px" height="650px"> </iframe>

            </div>

            <div id="download" style="position:fixed">
                  <iframe src = "/cgi-bin/filechucker_focushere.cgi?action=listfiles" width="750px" height="650px"> </iframe>
            </div>
      
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789174
I think the problem is that the
            <div id="upload" style="float:left" margin:0; padding:0;>
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath='/simeswiki/it/hware/dell'"

In the above, this is not being placed in the /cgi-bin/filechucker_focushere_prefs.cgi file
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789205
In the prefs.cgi file,
It looks like the variable is only defined if you have a single quote around the name..

$PREF{uploaded_files_dir}                               = $ENV{thePath};   // this doesnt set it

Error if set it like above...

Error: you haven't set $PREF{uploaded_files_dir}.

but

$PREF{uploaded_files_dir}                               = '$ENV{thePath}';

the above does but it is still wrong

Error: your settings for $PREF{uploaded_files_dir} and $PREF{uploaded_files_dir_is_in_docroot} result in $PREF{uploaded_files_realpath} being set to '/var/www/html$_GET{thePath}', but that path does not exist.

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789211
View source is

            <div id="upload" style="float:left" margin:0; padding:0;>
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath='/simeswiki/it/hware/dell'" width="490px" height="650px"> </iframe>

This isnt being passed to the $PREF{uploaded_files_dir} it looks like
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789229
Guys THANK YOU SO MUCH FOR YOUR HELP ON ALL OF THIS.

Ive given up on this.
Im going to post another question on this but in a different way.
You have been more than generous and patient with me.
I will split points but wish i could just give each of you the full.
Cant thank you enough.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21789256
View source is:
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath='/simeswiki/it/hware/dell'"
There should NOT be apostrophes around the value of thePath. It shoud NOT be:
'/simeswiki/it/hware/dell'

I should just be:
/simeswiki/it/hware/dell

Now you are clearly including the file correctly. Now, in filechucker_focushere.cgi are you including /cgi-bin/filechucker_focushere_prefs.cgi? IF not, which file is including /cgi-bin/filechucker_focushere_prefs.cgi?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789285
hielo, ok

both files...
are located in /cgi-bin.

That is..
filechucker_focushere.cgi
and
filechucker_focushere_prefs.cgi

I did not include the prefs file in filechucker_focushere.cgi since when I hard code the path to
the dir, it worked.

I have opened up the prefs.cgi file to find the below...


   1980         # Load the external prefs.
   1981         #
   1982         my ($prefs_basename) = ($ENV{SCRIPT_NAME} =~ m!.*?[/\\]?([^/\\]+)\.[^/\\\.]+!);
   1983         my @prefs_files = ("${prefs_basename}_prefs_new.cgi", "${prefs_basename}_prefs_new.pl", "${prefs_basename}_prefs.cgi", "${prefs_basename}_pref        s.pl", "${prefs_basename}_prefs_debug.cgi", "${prefs_basename}_prefs_debug.pl");
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789325
Youre right tho....i dont know what is referencing the prefs.cgi file, so no wonder the $ENV{thePath} is not being populated with the right thing.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789331
Would it be true to say that before the iframe is created,

27  <iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=getIncludePath();?>" width="490px" height="650px"> </iframe>


That $ENV{thePath} has to be set correctly in the filechucker_focushere_prefs.cgi file?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789396
hielo,
In my prefs.cgi file...the following works...

 $PREF{uploaded_files_dir}                               = '/simeswiki/it/hware/dell';

If im on page http://192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php
this is what I want.

If im on a different page, eg.
http://192.168.2.100/simeswiki/menu4_it_audit_disk_focusHERE.php

Can i use something like a case statement in the prefs.cgi file

For example:

use
 $PREF{uploaded_files_dir}                               = '/simeswiki/it/hware/dell';
if the page im on is http://192.168.2.100/simeswiki/menu4_it_hware_dell_focusHERE.php
use
 $PREF{uploaded_files_dir}                               = '/simeswiki/it/audit/disk';
if the page im on ishttp://192.168.2.100/simeswiki/menu4_it_audit_disk_focusHERE.php

and so on and so on.

I spose what Im saying is.....
Is there a way to list alternatives in the prefs file itself and use one based on what page im viewing.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21789397
$ENV{thePath} will exist/"be seen" in filechucker_focushere.cgi because of the query parameter thePath=<?=getIncludePath()?>
So, within filechucker_focushere.cgi you can see $ENV{thePath}, but NOT in the php file that is defining that iframe.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789412
Its being seen in filechucker_focushere.cgi
But is it being seen in filechucker_focushere-prefs.cgi ?
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789413
Sorry,
Not filechucker_focushere-prefs.cgi, but filechucker_focushere_prefs.cgi
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789417
filechucker_focushere_prefs.cgi is where thePath=<?=getIncludePath()?> needs to be seen.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789421
Im only building the iframe with filechucker_focushere.cgi (not filechucker_focushere_prefs.cgi) because it loads the form for upload......but the definition for the path needs to be seen in filechucker_focushere_prefs.cgi
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789442
In my php page, i have included the function, plus

  <iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=getIncludePath();?>" width="490px" height="650px"> </iframe>

and in the filechucker_focushere_prefs.cgi    (PREFERENCES file)

I have

$PREF{uploaded_files_dir}                               = $ENV{thePath};
#$PREF{uploaded_files_dir}                              = '/simeswiki/it/hware/dell';

If i load the php page and view its source with the above settings...

Error: you haven't set $PREF{uploaded_files_dir}.

View Source is..

            <div id="upload" style="float:left" margin:0; padding:0;>
 <iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=/simeswiki/it/hware/dell" width="490px" height="650px"> </iframe>

The path looks right......but the src is not the prefs file...so I dont think in the prefs file, the

$PREF{uploaded_files_dir}                               = $ENV{thePath};

is being set, which explains the message "Error: you haven't set $PREF{uploaded_files_dir}"

If I do the following in the prefs.cgi file..

#$PREF{uploaded_files_dir}                               = $ENV{thePath};
$PREF{uploaded_files_dir}                              = '/simeswiki/it/hware/dell';

and even if i keep the iframe code and include the function,

I get the upload form correctly. But I dont want to keep it like this because I want to dynamically define the path.

The view source for this is.

            <div id="upload" style="float:left" margin:0; padding:0;>
 <iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=/simeswiki/it/hware/dell" width="490px" height="650px"> </iframe>

It looks exactly the same.



0
 
LVL 82

Expert Comment

by:hielo
ID: 21789460
>>Is there a way to list alternatives in the prefs file itself and use one based on what page im viewing
IF you can modify the cgi, why not do this directly on that file:

@currentpage =  split(/[\/]/,$ENV{SCRIPT_NAME});
@path_arr = split(/_/,pop(@currentpage));
$path="";
$limit=$#path_arr+1;
 for($i=1;$i < $limit ;$i++){
 $path .="/".$path_arr[$i];
 }
$PREF{uploaded_files_dir}="/simeswiki".$path;

if you do it directly on prefs.cgi you will not need to put that parameter on the iframe
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789490
So hielo, should I do the following?

In my php page....where i have...

  <iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=getIncludePath();?>" width="490px" height="650px"> </iframe>

Should I change this to be..

  <iframe src = "/cgi-bin/filechucker_focushere.cgi?" width="490px" height="650px"> </iframe>

In the prefs cgi file, I have placed

@currentpage =  split(/[\/]/,$ENV{SCRIPT_NAME});
@path_arr = split(/_/,pop(@currentpage));
$path="";
$limit=$#path_arr+1;
 for($i=1;$i < $limit ;$i++){
 $path .="/".$path_arr[$i];
 }
$PREF{uploaded_files_dir}="/simeswiki".$path;

When I load the page, I get the following.....

Error processing your prefs file ('filechucker_focushere_prefs.cgi'): Global symbol "@currentpage" requires explicit package name at (eval 11) line 93. Global symbol "@path_arr" requires explicit package name at (eval 11) line 94. Global symbol "@currentpage" requires explicit package name at (eval 11) line 94. Global symbol "$path" requires explicit package name at (eval 11) line 95. Global symbol "$limit" requires explicit package name at (eval 11) line 96. Global symbol "@path_arr" requires explicit package name at (eval 11) line 96. Global symbol "$i" requires explicit package name at (eval 11) line 97. Global symbol "$i" requires explicit package name at (eval 11) line 97. Global symbol "$limit" requires explicit package name at (eval 11) line 97.



0
 
LVL 82

Expert Comment

by:hielo
ID: 21789505
>>Should I change this to be..
>>  <iframe src = "/cgi-bin/filechucker_focushere.cgi?" width="490px" height="650px"> </iframe>
Yes

Try this (copy and paste):
my @currentpage =  split(/[\/]/,$ENV{SCRIPT_NAME});
my @path_arr = split(/_/,pop(@currentpage));
my $filepath="";
my $limit=$#path_arr+1;
 for(my $i=1;$i < $limit ;$i++){
 $filepath .="/".$path_arr[$i];
 }
$PREF{uploaded_files_dir}="/simeswiki".$filepath;
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789523
Ok making progress :>)

The $PREF{uploaded_files_dir} looks like it is now being defined, but still an error as follows..

Error: your settings for $PREF{uploaded_files_dir} and $PREF{uploaded_files_dir_is_in_docroot} result in $PREF{uploaded_files_realpath} being set to '/var/www/html/simeswiki/focushere.cgi', but that path does not exist.


Im my php page, i have the iframe as..

<iframe src = "/cgi-bin/filechucker_focushere.cgi?" width="490pxr height="650px"> </iframe>

So it looks like it is not getting the filepath

This php page is located in

/var/www/html/simeswiki/

and is called

menu4_it_hware_dell_focusHERE.php

The 2 cgi files are located in
/var/www/cgi-bin

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789527
If i do the following hielo..

100 $PREF{uploaded_files_dir}="/simeswiki".'/it/hware/dell';

instead of

$PREF{uploaded_files_dir}="/simeswiki".$filepath;

it works......WE MUST BE GETTING CLOSE TO KILLING THIS :>)
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789530
It looks like its just the $filepath part.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789542
The view source for the above that works is

 <iframe src = "/cgi-bin/filechucker_focushere.cgi?" width="490px" height="650px"> </iframe>

That is, when i put

$PREF{uploaded_files_dir}="/simeswiki".'/it/hware/dell';
0
 
LVL 82

Expert Comment

by:hielo
ID: 21789546
Try changing the iframe to:
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=$_SERVER['PHP_SELF']?>" width="490px" height="650px"> </iframe>

and in prefs.cgi change this:
my @currentpage =  split(/[\/]/,$ENV{SCRIPT_NAME});

to this:
my @currentpage =  split(/[\/]/,$ENV{thePath});
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789563
Ok

No errors this time, but the upload path is just to /simeswiki instead of /simeswiki/it/hware/dell.

View source is....

<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=/simeswiki/menu4_it_hware_dell_focusHERE.php" width="490px" height="650px"> </iframe>

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789594
If i set
$PREF{uploaded_files_dir}=$filepath;

I get
Error: you haven't set $PREF{uploaded_files_dir}.



0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789635
hielo,

is there any way to direct

thePath=<?=$_SERVER['PHP_SELF']?>"

as in below

<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=$_SERVER['PHP_SELF']?>" width="490px" height="650px"> </iframe>

to filechucker_focushere_prefs.cgi instead, as i dont think the $filename part in the prefs is being defined, or do you think its something else?
It just seems like that we cant pass thePath=<?=$_SERVER['PHP_SELF']?>"  to the prefs file
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21789648
Or the function?


      1 <?php
      2
      3 function getIncludePath()
      4 {
      5 $self = explode("/",$_SERVER['PHP_SELF']);
      6 $path_arr=explode("_",$self[count($self)-1]);
      7 $path="";
      8 for($i=1;$i < count($path_arr)-1;$i++){
      9 $path .="/".$path_arr[$i];
     10 }
     11 $uploaded_files_dir = "/simeswiki" . $path;
     12 return $uploaded_files_dir;
     13 }
     14 ?>

The path where this file is in /var/www/html/simeswiki/includes/focushere_functions.php

0
 
LVL 82

Expert Comment

by:hielo
ID: 21789676
which file is including/importing /cgi-bin/filechucker_focushere_prefs.cgi?
0
 
LVL 82

Expert Comment

by:hielo
ID: 21789733
based on your comment above (ID: 21789563) try this in prefs.cgi:

use CGI;
my @currentpage =  split(/[\/]/,param('thePath'));
my @path_arr = split(/_/,pop(@currentpage));
my $filepath="";
my $limit=$#path_arr+1;
 for(my $i=1;$i < $limit ;$i++){
 $filepath .="/".$path_arr[$i];
 }
$PREF{uploaded_files_dir}="/simeswiki".$filepath;

with this:
<iframe src = "/cgi-bin/filechucker_focushere.cgi?thePath=<?=$_SERVER['PHP_SELF']?>" width="490px" height="650px"> </iframe>
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21790570
hielo mate.
Im going to allocate points now and go back to the drawing board.
Thanks so much for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to dynamically set the form action using jQuery.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now