• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Building a function from code in a php file

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
Simon336697
Asked:
Simon336697
  • 41
  • 16
  • 5
  • +1
3 Solutions
 
Ali KayahanFull Stack DeveloperCommented:
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
 
Ali KayahanFull Stack DeveloperCommented:
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
 
Simon336697Author Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
afzzCommented:
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
 
hieloCommented:
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
 
hieloCommented:
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
 
Simon336697Author Commented:
You guys are the greatest Ill report back very shortly :>)
0
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
afzzCommented:
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
 
Simon336697Author Commented:
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
 
hieloCommented:
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
 
afzzCommented:
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
 
afzzCommented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
I dont think the $ENV{QUERY_STRING} is being read properly.
0
 
afzzCommented:
i understand what you want. I'll go through your code and provide you a solution soon.
0
 
Simon336697Author Commented:
My correction....it is, but when I try and upload, it falls over.
0
 
hieloCommented:
>>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
 
hieloCommented:
>>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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
I have set it in the prefs.cgi file as follows..

$PREF{uploaded_files_dir}                               = $ENV{thePath};
0
 
hieloCommented:
>>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
 
Simon336697Author Commented:
hielo,
I used your function..
0
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
Sorry, im including the function in menu4_it_hware_dell_focusHERE.php as well.
0
 
Simon336697Author Commented:
hielo,

For $ENV{thePath};

this should be /simeswiki/it/hware/dell

if the filename im viewing is
menu4_it_hware_dell_focusHERE.php

0
 
hieloCommented:
/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
 
hieloCommented:
>>For $ENV{thePath}; ...
Correct. That is what hte function does.
0
 
hieloCommented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
hieloCommented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
hieloCommented:
$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
 
Simon336697Author Commented:
Its being seen in filechucker_focushere.cgi
But is it being seen in filechucker_focushere-prefs.cgi ?
0
 
Simon336697Author Commented:
Sorry,
Not filechucker_focushere-prefs.cgi, but filechucker_focushere_prefs.cgi
0
 
Simon336697Author Commented:
filechucker_focushere_prefs.cgi is where thePath=<?=getIncludePath()?> needs to be seen.
0
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
hieloCommented:
>>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
 
Simon336697Author Commented:
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
 
hieloCommented:
>>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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
It looks like its just the $filepath part.
0
 
Simon336697Author Commented:
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
 
hieloCommented:
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
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
If i set
$PREF{uploaded_files_dir}=$filepath;

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



0
 
Simon336697Author Commented:
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
 
Simon336697Author Commented:
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
 
hieloCommented:
which file is including/importing /cgi-bin/filechucker_focushere_prefs.cgi?
0
 
hieloCommented:
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
 
Simon336697Author Commented:
hielo mate.
Im going to allocate points now and go back to the drawing board.
Thanks so much for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 41
  • 16
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now