php-general Digest 5 Dec 2010 11:37:23 -0000 Issue 7070

Topics (messages 309846 through 309857):

Re: code quest
        309846 by: Kirk Bailey
        309847 by: Kirk Bailey
        309848 by: Kirk Bailey
        309849 by: Matt Graham
        309850 by: Kirk Bailey
        309851 by: Kirk Bailey
        309852 by: Tamara Temple
        309853 by: Kirk Bailey
        309854 by: Richard Quadling
        309855 by: Daniel P. Brown
        309856 by: Tamara Temple

Re: Problem with "shell_exec"
        309857 by: Boyan Penev

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message --- A good point, but in this application there WILL be AT LEAST 1 legitimate directory at all times, or else the script would not be used, so this ought not be a problem.

My problem is that I understand the basic functions to implement, but have not yet aquired sufficient command of php to implement the required multi step algorithm.

Jim Lucas wrote:
On 11/26/2010 4:03 PM, Kirk Bailey wrote:
Hello all, my name is Kirk Bailey, and I am new to php, so please be forbearing.
I code in python, and am trying to learn this language as our new client runs a
web business based in it.

I need a routine that will return a list of every directory immediately under
the current directory- but nothing else, just a list of directories, 1 level
deep, NO FILES, no listing of current dir or prior dir either.

Now in python, I would use os.walk, and use the list of dirs and throw the other
2 lists away, but this ain't Kansas anymore. Does php even DO lists?

Um, a list is a 1 dimenional array, if have a list ALIST and you plug in 3, you
get back the contents of cell 3 in the list, whaqtever that content is. so if
cell 3 in a 6 celled list was "Ruby" then ALIST[3] would return the string 
"ruby".

It's easy to iterate lists. For instance:

   print '<ul>'
   for dir in ALIST:
       print '<li><a href=\"/dir>",dir,'</a>
   print '</ul>

This would let me produce an ordered list of directories, each a link to that
directory.
This way, when a client installs a new product, the home page area listing
products offered automatically updates.

Further embellishment would let me replace the dir name with a BRIEF description
from a descriptor file read from that dir. Now how to do this in php?


This should do.

The only problem that I foresee would be an empty "<ul></ul>" if you have no
directories returned by glob().

print('<ul>');
foreach ( glob('./*', GLOB_ONLYDIR) AS $dir )
  print('<li><a href=\"'.$dir.'\">'.$dir.'</a></li>');
print('</ul>');


Jim Lucas


--
end

Very Truly yours,
                - Kirk Bailey,
                  Largo Florida

kniht +-----+ | BOX | +-----+ think
--- End Message ---
--- Begin Message --- Now Now, fight nice. We don need no stinkin' @$^*$^(! woids here. :-P
Steve Staples wrote:
On Thu, 2010-12-02 at 10:07 -0500, Daniel P. Brown wrote:
On Wed, Dec 1, 2010 at 23:13, Kirk Bailey <[email protected]> wrote:
[snip!]
Can this be improved to exclude anything with a '.' or a '-' in it's name?
This will exclude the smileys and cgi-bin and such. If it can be persuaded
to read a 1 line description from each subdirectory it could then use THAT
as the text in the link, instead of the name. This could be useful in many
settings.
    Sure.  Change:

        if (is_dir($d) && $d != '.' && $d != '..') {

    To:

        if (is_dir($d) && !preg_match('/[\.\-]/',$d)) {

    Keep in mind, though, that the change will no longer show anything
that matches the below either:

        example.directory
        example-directory
        special-images
        css.files

    In other words, you may instead want to explicitly state which
directories to omit, and then drop anything that begins with a dot as
well (hidden directories on *NIX-like boxes) like so:

<?php
$excludes[] = 'cgi-bin';
$excludes[] = 'vti_cnf';
$excludes[] = 'private';
$excludes[] = 'thumbnail';

$ls = scandir(dirname(__FILE__));
foreach ($ls as $d) {
  if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
!in_array(basename($d),$excludes)) {
      echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
  }
}
?>

--
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/


damn you Daniel... I was just about to reply with almost the EXACT same
answer!!!

I think the last example would probably be the best one to use, that way
you can still have some directories with the . or - or even the _ in the
names, and still be able to display them.

Steve

ps thanks for saving me type it all out Daniel :)



--
end

Very Truly yours,
                - Kirk Bailey,
                  Largo Florida

kniht +-----+ | BOX | +-----+ think
--- End Message ---
--- Begin Message ---
my current code is as follows:

   *<ul>
   <?php # The next several lines declare an array of directories which
   are NOT to be listed!
   $excludes[] = 'images';
   $excludes[] = 'cgi-bin';
   $excludes[] = 'vti_cnf';
   $excludes[] = 'private';
   $excludes[] = 'thumbnail';

   $ls = scandir(dirname(__FILE__));
   foreach ($ls as $d) {
     if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
   !in_array(basename($d),$excludes)) {
         echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
     }
   }
   ?>
   </ul>*

The page containing this is at this url:
   http://www.howlermonkey.net/dirlisting.php
I believe this will be a starting point for the functionality I am looking for- an automatic menu of areas in a website one may go to. By excluding some folders, people don't go trespassing into the cgi-bin or images folder, or into areas reserved for administrative uses.


Daniel P. Brown wrote:
On Wed, Dec 1, 2010 at 23:13, Kirk Bailey <[email protected]> wrote:
[snip!]
Can this be improved to exclude anything with a '.' or a '-' in it's name?
This will exclude the smileys and cgi-bin and such. If it can be persuaded
to read a 1 line description from each subdirectory it could then use THAT
as the text in the link, instead of the name. This could be useful in many
settings.

    Sure.  Change:

        if (is_dir($d) && $d != '.' && $d != '..') {

    To:

        if (is_dir($d) && !preg_match('/[\.\-]/',$d)) {

    Keep in mind, though, that the change will no longer show anything
that matches the below either:

        example.directory
        example-directory
        special-images
        css.files

    In other words, you may instead want to explicitly state which
directories to omit, and then drop anything that begins with a dot as
well (hidden directories on *NIX-like boxes) like so:

<?php
$excludes[] = 'cgi-bin';
$excludes[] = 'vti_cnf';
$excludes[] = 'private';
$excludes[] = 'thumbnail';

$ls = scandir(dirname(__FILE__));
foreach ($ls as $d) {
  if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
!in_array(basename($d),$excludes)) {
      echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
  }
}
?>


--
end

Very Truly yours,
                - Kirk Bailey,
                  Largo Florida

kniht +-----+ | BOX | +-----+ think
--- End Message ---
--- Begin Message ---
From: Kirk Bailey <[email protected]>
> OK, now here's a giggle; I like ssi includes. If I put the script
> in as an ssi include, will it still work?

If you're using Apache, and you do

<!--#include virtual="something.php" -->

...the PHP in something.php will execute and produce output, but
something.php will not have any access to $_GET or $_POST or $_SESSION or any
of those things.  This is generally not what you want.  If you do

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

...then something.php will be able to see and work with the superglobals that
have been set up further up the page, which is *usually* what you want.  You
could try both approaches in a test env and see what you get.  I'll use SSI
for "dumb" blocks of text and php include for "smart" blocks of code, because
IME that tends to produce fewer instances of gross stupidity.

Note that YMMV on all this and ICBW.

-- 
Matt G / Dances With Crows
The Crow202 Blog:  http://crow202.org/wordpress/
There is no Darkness in Eternity/But only Light too dim for us to see


--- End Message ---
--- Begin Message ---
Ok, let's kick this around.

iterating an array(?; 1 dimensional listing of things) in php, I am creating a list of direcoties. I want to open and read in a file in each directory with a standard name, which contains a 1 line description of the directory and it's purpose.

Now, here's the existing code; this code is online NOW at this url:
http://www.howlermonkey.net/dirlisting.php

   * 1<?php # The next several lines declare an array of directories
which are 2NOT to be listed!
    3$excludes[] = 'images';
    4$excludes[] = 'cgi-bin';
    5$excludes[] = 'vti_cnf';
    6$excludes[] = 'private';
    7$excludes[] = 'thumbnail';
    8
    9$ls = scandir(dirname(__FILE__));
   10foreach ($ls as $d) {
   11if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
   12!in_array(basename($d),$excludes)) {
   13     echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
   14 }
   15}
   16?>*

Let's say the file to read in /elite is named 'desc.txt'.
It looks like you want me to modify line 13 to say:
echo '<li><a href="'.$d.'">'.include($d.'desc.txt').'</a><br/>'.PHP_EOL;

so, if the file '/elite/desc.txt' contains the line

   *82nd Airbourne - We are an elite unit of army Paratroopers
   *

Then that element in the list would appear as:

   * 82nd Airbourne  -We are an elite unit of army Paratroopers

And would be clickable. Let's try it and see if this hound hunts.

Matt Graham wrote:
From: Kirk Bailey <[email protected]>
OK, now here's a giggle; I like ssi includes. If I put the script
in as an ssi include, will it still work?

If you're using Apache, and you do

<!--#include virtual="something.php" -->

...the PHP in something.php will execute and produce output, but
something.php will not have any access to $_GET or $_POST or $_SESSION or any
of those things.  This is generally not what you want.  If you do

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

...then something.php will be able to see and work with the superglobals that
have been set up further up the page, which is *usually* what you want.  You
could try both approaches in a test env and see what you get.  I'll use SSI
for "dumb" blocks of text and php include for "smart" blocks of code, because
IME that tends to produce fewer instances of gross stupidity.

Note that YMMV on all this and ICBW.


--
end

Very Truly yours,
                - Kirk Bailey,
                  Largo Florida

kniht +-----+ | BOX | +-----+ think
--- End Message ---
--- Begin Message --- The hound barks, but does not yet properly hunt, and we need to bring home the bacon.

OK, here is the current code:

   <?php # The next several lines declare an array of directories which
   are NOT to be listed!
   $excludes[] = 'images';
   $excludes[] = 'cgi-bin';
   $excludes[] = 'vti_cnf';
   $excludes[] = 'private';
   $excludes[] = 'thumbnail';

   $ls = scandir(dirname(__FILE__));
   foreach ($ls as $d) {
     if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
   !in_array(basename($d),$excludes)) {
         echo '<li><a href="'.$d.'">'.include('./'.$d.'/desc.txt')
   ;#.'</a><br/>'.PHP_EOL;
     }
   }
   ?>

the url again, to view the results, is http://www.howlermonkey.net/dirlisting.php and is live right now. The results are a tad odd to say the least.




Kirk Bailey wrote:
Ok, let's kick this around.

iterating an array(?; 1 dimensional listing of things) in php, I am creating a list of direcoties. I want to open and read in a file in each directory with a standard name, which contains a 1 line description of the directory and it's purpose.

Now, here's the existing code; this code is online NOW at this url:
http://www.howlermonkey.net/dirlisting.php

   * 1<?php # The next several lines declare an array of directories
   which are      2NOT to be listed!
    3$excludes[] = 'images';
    4$excludes[] = 'cgi-bin';
    5$excludes[] = 'vti_cnf';
    6$excludes[] = 'private';
    7$excludes[] = 'thumbnail';
    8
    9$ls = scandir(dirname(__FILE__));
   10foreach ($ls as $d) {
   11if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
   12!in_array(basename($d),$excludes)) {
   13     echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
   14 }
   15}
   16?>*

Let's say the file to read in /elite is named 'desc.txt'.
It looks like you want me to modify line 13 to say:
echo '<li><a href="'.$d.'">'.include($d.'desc.txt').'</a><br/>'.PHP_EOL;

so, if the file '/elite/desc.txt' contains the line

   *82nd Airbourne - We are an elite unit of army Paratroopers
   *

Then that element in the list would appear as:

   * 82nd Airbourne  -We are an elite unit of army Paratroopers

And would be clickable. Let's try it and see if this hound hunts.

Matt Graham wrote:
From: Kirk Bailey <[email protected]>
OK, now here's a giggle; I like ssi includes. If I put the script
in as an ssi include, will it still work?

If you're using Apache, and you do

<!--#include virtual="something.php" -->

...the PHP in something.php will execute and produce output, but
something.php will not have any access to $_GET or $_POST or $_SESSION or any
of those things.  This is generally not what you want.  If you do

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

...then something.php will be able to see and work with the superglobals that have been set up further up the page, which is *usually* what you want. You could try both approaches in a test env and see what you get. I'll use SSI for "dumb" blocks of text and php include for "smart" blocks of code, because
IME that tends to produce fewer instances of gross stupidity.

Note that YMMV on all this and ICBW.



--
end

Very Truly yours,
                - Kirk Bailey,
                  Largo Florida

kniht +-----+ | BOX | +-----+ think
--- End Message ---
--- Begin Message ---

On Dec 4, 2010, at 2:15 PM, Kirk Bailey wrote:

The hound barks, but does not yet properly hunt, and we need to bring home the bacon.

OK, here is the current code:

  <?php # The next several lines declare an array of directories which
  are NOT to be listed!
  $excludes[] = 'images';
  $excludes[] = 'cgi-bin';
  $excludes[] = 'vti_cnf';
  $excludes[] = 'private';
  $excludes[] = 'thumbnail';

  $ls = scandir(dirname(__FILE__));
  foreach ($ls as $d) {
    if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
  !in_array(basename($d),$excludes)) {
        echo '<li><a href="'.$d.'">'.include('./'.$d.'/desc.txt')
  ;#.'</a><br/>'.PHP_EOL;
    }
  }
  ?>

the url again, to view the results, is http://www.howlermonkey.net/dirlisting.php and is live right now. The results are a tad odd to say the least.


Ok, I don't think that's actually the code that generated the page you link to, but let's go with what you've got.

First of all, include() does not return a string to the calling program. include() basically redirects the php interpretter to process the contents of the file. What include() returns is success or failure of the execution of the included script. To use the current setup you have with the desc.txt files, you want to do something like this:

        echo '<li><a href="'.$d.'">';
        include('./'.$d.'/desc.txt');
        echo '</a></li><br />'.PHP_EOL;

If the file desc.txt contains only text, it will get sent to the browser as is.





Kirk Bailey wrote:
Ok, let's kick this around.

iterating an array(?; 1 dimensional listing of things) in php, I am creating a list of direcoties. I want to open and read in a file in each directory with a standard name, which contains a 1 line description of the directory and it's purpose.

Now, here's the existing code; this code is online NOW at this url:
http://www.howlermonkey.net/dirlisting.php

  * 1<?php # The next several lines declare an array of directories
  which are      2NOT to be listed!
   3$excludes[] = 'images';
   4$excludes[] = 'cgi-bin';
   5$excludes[] = 'vti_cnf';
   6$excludes[] = 'private';
   7$excludes[] = 'thumbnail';
   8
   9$ls = scandir(dirname(__FILE__));
  10foreach ($ls as $d) {
  11if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
  12!in_array(basename($d),$excludes)) {
  13     echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
  14 }
  15}
  16?>*

Let's say the file to read in /elite is named 'desc.txt'.
It looks like you want me to modify line 13 to say:
echo '<li><a href="'.$d.'">'.include($d.'desc.txt').'</a><br/ >'.PHP_EOL;

so, if the file '/elite/desc.txt' contains the line

  *82nd Airbourne - We are an elite unit of army Paratroopers
  *

Then that element in the list would appear as:

  * 82nd Airbourne  -We are an elite unit of army Paratroopers

And would be clickable. Let's try it and see if this hound hunts.

Matt Graham wrote:
From: Kirk Bailey <[email protected]>

OK, now here's a giggle; I like ssi includes. If I put the script
in as an ssi include, will it still work?


If you're using Apache, and you do

<!--#include virtual="something.php" -->

...the PHP in something.php will execute and produce output, but
something.php will not have any access to $_GET or $_POST or $_SESSION or any
of those things.  This is generally not what you want.  If you do

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

...then something.php will be able to see and work with the superglobals that have been set up further up the page, which is *usually* what you want. You could try both approaches in a test env and see what you get. I'll use SSI for "dumb" blocks of text and php include for "smart" blocks of code, because
IME that tends to produce fewer instances of gross stupidity.

Note that YMMV on all this and ICBW.




--
end

Very Truly yours,
               - Kirk Bailey,
                 Largo Florida

kniht +----- + | BOX | +----- + think


--- End Message ---
--- Begin Message ---
the code is now:

   <?php # The next several lines declare an array of directories which
   are NOT to be listed!
   $excludes[] = 'images';
   $excludes[] = 'cgi-bin';
   $excludes[] = 'vti_cnf';
   $excludes[] = 'private';
   $excludes[] = 'thumbnail';

   $ls = scandir(dirname(__FILE__));
   foreach ($ls as $d) {
     if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
   !in_array(basename($d),$excludes)) {
         echo '<li><a href="'.$d.'">';
         echo include($d.'/desc.txt' );
         echo '</a><br/>'.PHP_EOL;
     }
   }
   ?>

And it works!
   BUT!!!!!
Where is the "1" coming from?!?
Please inspect the page and see what I mean. This is not in the code, and it's not in the source file. Link: http://www.howlermonkey.net/dirlisting.php

I hope this dialog is proving at least mildly interesting to the remainder of the list as an educational exercise.


Tamara Temple wrote:

On Dec 4, 2010, at 2:15 PM, Kirk Bailey wrote:

The hound barks, but does not yet properly hunt, and we need to bring home the bacon.

OK, here is the current code:

  <?php # The next several lines declare an array of directories which
  are NOT to be listed!
  $excludes[] = 'images';
  $excludes[] = 'cgi-bin';
  $excludes[] = 'vti_cnf';
  $excludes[] = 'private';
  $excludes[] = 'thumbnail';

  $ls = scandir(dirname(__FILE__));
  foreach ($ls as $d) {
    if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
  !in_array(basename($d),$excludes)) {
        echo '<li><a href="'.$d.'">'.include('./'.$d.'/desc.txt')
  ;#.'</a><br/>'.PHP_EOL;
    }
  }
  ?>

the url again, to view the results, is http://www.howlermonkey.net/dirlisting.php and is live right now. The results are a tad odd to say the least.


Ok, I don't think that's actually the code that generated the page you link to, but let's go with what you've got.

First of all, include() does not return a string to the calling program. include() basically redirects the php interpretter to process the contents of the file. What include() returns is success or failure of the execution of the included script. To use the current setup you have with the desc.txt files, you want to do something like this:

    echo '<li><a href="'.$d.'">';
    include('./'.$d.'/desc.txt');
    echo '</a></li><br />'.PHP_EOL;

If the file desc.txt contains only text, it will get sent to the browser as is.





Kirk Bailey wrote:
Ok, let's kick this around.

iterating an array(?; 1 dimensional listing of things) in php, I am creating a list of direcoties. I want to open and read in a file in each directory with a standard name, which contains a 1 line description of the directory and it's purpose.

Now, here's the existing code; this code is online NOW at this url:
http://www.howlermonkey.net/dirlisting.php

  * 1<?php # The next several lines declare an array of directories
  which are      2NOT to be listed!
   3$excludes[] = 'images';
   4$excludes[] = 'cgi-bin';
   5$excludes[] = 'vti_cnf';
   6$excludes[] = 'private';
   7$excludes[] = 'thumbnail';
   8
   9$ls = scandir(dirname(__FILE__));
  10foreach ($ls as $d) {
  11if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
  12!in_array(basename($d),$excludes)) {
  13     echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
  14 }
  15}
  16?>*

Let's say the file to read in /elite is named 'desc.txt'.
It looks like you want me to modify line 13 to say:
echo '<li><a href="'.$d.'">'.include($d.'desc.txt').'</a><br/>'.PHP_EOL;

so, if the file '/elite/desc.txt' contains the line

  *82nd Airbourne - We are an elite unit of army Paratroopers
  *

Then that element in the list would appear as:

  * 82nd Airbourne  -We are an elite unit of army Paratroopers

And would be clickable. Let's try it and see if this hound hunts.

Matt Graham wrote:
From: Kirk Bailey <[email protected]>

OK, now here's a giggle; I like ssi includes. If I put the script
in as an ssi include, will it still work?


If you're using Apache, and you do

<!--#include virtual="something.php" -->

...the PHP in something.php will execute and produce output, but
something.php will not have any access to $_GET or $_POST or $_SESSION or any
of those things.  This is generally not what you want.  If you do

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

...then something.php will be able to see and work with the superglobals that have been set up further up the page, which is *usually* what you want. You could try both approaches in a test env and see what you get. I'll use SSI for "dumb" blocks of text and php include for "smart" blocks of code, because
IME that tends to produce fewer instances of gross stupidity.

Note that YMMV on all this and ICBW.




--
end

Very Truly yours,
               - Kirk Bailey,
                 Largo Florida

kniht +-----+ | BOX | +-----+ think



--
end

Very Truly yours,
                - Kirk Bailey,
                  Largo Florida

kniht +-----+ | BOX | +-----+ think
--- End Message ---
--- Begin Message ---
On 4 December 2010 20:58, Kirk Bailey <[email protected]> wrote:
> the code is now:
>
>   <?php # The next several lines declare an array of directories which
>   are NOT to be listed!
>   $excludes[] = 'images';
>   $excludes[] = 'cgi-bin';
>   $excludes[] = 'vti_cnf';
>   $excludes[] = 'private';
>   $excludes[] = 'thumbnail';
>
>   $ls = scandir(dirname(__FILE__));
>   foreach ($ls as $d) {
>     if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
>   !in_array(basename($d),$excludes)) {
>         echo '<li><a href="'.$d.'">';
>         echo include($d.'/desc.txt' );
>         echo '</a><br/>'.PHP_EOL;
>     }
>   }
>   ?>
>
> And it works!
>   BUT!!!!!
> Where is the "1" coming from?!?
> Please inspect the page and see what I mean. This is not in the code, and
> it's not in the source file. Link:
> http://www.howlermonkey.net/dirlisting.php
>
> I hope this dialog is proving at least mildly interesting to the remainder
> of the list as an educational exercise.
>
>
> Tamara Temple wrote:
>>
>> On Dec 4, 2010, at 2:15 PM, Kirk Bailey wrote:
>>
>>> The hound barks, but does not yet properly hunt, and we need to bring
>>> home the bacon.
>>>
>>> OK, here is the current code:
>>>
>>>  <?php # The next several lines declare an array of directories which
>>>  are NOT to be listed!
>>>  $excludes[] = 'images';
>>>  $excludes[] = 'cgi-bin';
>>>  $excludes[] = 'vti_cnf';
>>>  $excludes[] = 'private';
>>>  $excludes[] = 'thumbnail';
>>>
>>>  $ls = scandir(dirname(__FILE__));
>>>  foreach ($ls as $d) {
>>>    if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
>>>  !in_array(basename($d),$excludes)) {
>>>        echo '<li><a href="'.$d.'">'.include('./'.$d.'/desc.txt')
>>>  ;#.'</a><br/>'.PHP_EOL;
>>>    }
>>>  }
>>>  ?>
>>>
>>> the url again, to view the results, is
>>> http://www.howlermonkey.net/dirlisting.php and is live right now. The
>>> results are a tad odd to say the least.
>>>
>>
>> Ok, I don't think that's actually the code that generated the page you
>> link to, but let's go with what you've got.
>>
>> First of all, include() does not return a string to the calling program.
>> include() basically redirects the php interpretter to process the contents
>> of the file. What include() returns is success or failure of the execution
>> of the included script. To use the current setup you have with the desc.txt
>> files, you want to do something like this:
>>
>>    echo '<li><a href="'.$d.'">';
>>    include('./'.$d.'/desc.txt');
>>    echo '</a></li><br />'.PHP_EOL;
>>
>> If the file desc.txt contains only text, it will get sent to the browser
>> as is.
>>
>>
>>>
>>>
>>>
>>> Kirk Bailey wrote:
>>>>
>>>> Ok, let's kick this around.
>>>>
>>>> iterating an array(?; 1 dimensional listing of things) in php, I am
>>>> creating a list of direcoties. I want to open and read in a file in each
>>>> directory with a standard name, which contains a 1 line description of the
>>>> directory and it's purpose.
>>>>
>>>> Now, here's the existing code; this code is online NOW at this url:
>>>> http://www.howlermonkey.net/dirlisting.php
>>>>
>>>>  * 1<?php # The next several lines declare an array of directories
>>>>  which are      2NOT to be listed!
>>>>   3$excludes[] = 'images';
>>>>   4$excludes[] = 'cgi-bin';
>>>>   5$excludes[] = 'vti_cnf';
>>>>   6$excludes[] = 'private';
>>>>   7$excludes[] = 'thumbnail';
>>>>   8
>>>>   9$ls = scandir(dirname(__FILE__));
>>>>  10foreach ($ls as $d) {
>>>>  11if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
>>>>  12!in_array(basename($d),$excludes)) {
>>>>  13     echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
>>>>  14 }
>>>>  15}
>>>>  16?>*
>>>>
>>>> Let's say the file to read in /elite is named 'desc.txt'.
>>>> It looks like you want me to modify line 13 to say:
>>>> echo '<li><a href="'.$d.'">'.include($d.'desc.txt').'</a><br/>'.PHP_EOL;
>>>>
>>>> so, if the file '/elite/desc.txt' contains the line
>>>>
>>>>  *82nd Airbourne - We are an elite unit of army Paratroopers
>>>>  *
>>>>
>>>> Then that element in the list would appear as:
>>>>
>>>>  * 82nd Airbourne  -We are an elite unit of army Paratroopers
>>>>
>>>> And would be clickable. Let's try it and see if this hound hunts.
>>>>
>>>> Matt Graham wrote:
>>>>>
>>>>> From: Kirk Bailey <[email protected]>
>>>>>
>>>>>> OK, now here's a giggle; I like ssi includes. If I put the script
>>>>>> in as an ssi include, will it still work?
>>>>>>
>>>>>
>>>>> If you're using Apache, and you do
>>>>>
>>>>> <!--#include virtual="something.php" -->
>>>>>
>>>>> ...the PHP in something.php will execute and produce output, but
>>>>> something.php will not have any access to $_GET or $_POST or $_SESSION
>>>>> or any
>>>>> of those things.  This is generally not what you want.  If you do
>>>>>
>>>>> <?php include("something.php"); ?>
>>>>>
>>>>> ...then something.php will be able to see and work with the
>>>>> superglobals that
>>>>> have been set up further up the page, which is *usually* what you want.
>>>>>  You
>>>>> could try both approaches in a test env and see what you get.  I'll use
>>>>> SSI
>>>>> for "dumb" blocks of text and php include for "smart" blocks of code,
>>>>> because
>>>>> IME that tends to produce fewer instances of gross stupidity.
>>>>>
>>>>> Note that YMMV on all this and ICBW.
>>>>>
>>>>>
>>>>
>>>
>>> --
>>> end
>>>
>>> Very Truly yours,
>>>               - Kirk Bailey,
>>>                 Largo Florida
>>>
>>>                     kniht                        +-----+
>>>       | BOX |                       +-----+                        think
>>
>>
>
> --
> end
>
> Very Truly yours,
>                - Kirk Bailey,
>                  Largo Florida
>
>                      kniht                        +-----+
>     | BOX |                       +-----+                        think
>

You don't need to echo the include statement.

Just use include

Unless your include looks like ...

<?php
return something.....

In which case, you can assign the result or echo it.

See example 5 on http://docs.php.net/manual/en/function.include.php

-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
On Sat, Dec 4, 2010 at 15:58, Kirk Bailey <[email protected]> wrote:
>
> And it works!
>   BUT!!!!!
> Where is the "1" coming from?!?

    No need to see the page.  You're echo'ing an include.  No need to
do that; including the file is enough, and that's what's working.
Adding the echo makes it print the status returned by 'include' ---
which is boolean TRUE, AKA 1.

-- 
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

--- End Message ---
--- Begin Message ---

On Dec 4, 2010, at 2:58 PM, Kirk Bailey wrote:

the code is now:

  <?php # The next several lines declare an array of directories which
  are NOT to be listed!
  $excludes[] = 'images';
  $excludes[] = 'cgi-bin';
  $excludes[] = 'vti_cnf';
  $excludes[] = 'private';
  $excludes[] = 'thumbnail';

  $ls = scandir(dirname(__FILE__));
  foreach ($ls as $d) {
    if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
  !in_array(basename($d),$excludes)) {
        echo '<li><a href="'.$d.'">';
        echo include($d.'/desc.txt' );

There should be no echo here

        echo '</a><br/>'.PHP_EOL;
    }
  }
  ?>

And it works!
  BUT!!!!!
Where is the "1" coming from?!?
Please inspect the page and see what I mean. This is not in the code, and it's not in the source file. Link: http://www.howlermonkey.net/dirlisting.php

I hope this dialog is proving at least mildly interesting to the remainder of the list as an educational exercise.


Tamara Temple wrote:

On Dec 4, 2010, at 2:15 PM, Kirk Bailey wrote:

The hound barks, but does not yet properly hunt, and we need to bring home the bacon.

OK, here is the current code:

<?php # The next several lines declare an array of directories which
 are NOT to be listed!
 $excludes[] = 'images';
 $excludes[] = 'cgi-bin';
 $excludes[] = 'vti_cnf';
 $excludes[] = 'private';
 $excludes[] = 'thumbnail';

 $ls = scandir(dirname(__FILE__));
 foreach ($ls as $d) {
   if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
 !in_array(basename($d),$excludes)) {
       echo '<li><a href="'.$d.'">'.include('./'.$d.'/desc.txt')
 ;#.'</a><br/>'.PHP_EOL;
   }
 }
 ?>

the url again, to view the results, is http://www.howlermonkey.net/dirlisting.php and is live right now. The results are a tad odd to say the least.


Ok, I don't think that's actually the code that generated the page you link to, but let's go with what you've got.

First of all, include() does not return a string to the calling program. include() basically redirects the php interpretter to process the contents of the file. What include() returns is success or failure of the execution of the included script. To use the current setup you have with the desc.txt files, you want to do something like this:

   echo '<li><a href="'.$d.'">';
   include('./'.$d.'/desc.txt');
   echo '</a></li><br />'.PHP_EOL;

If the file desc.txt contains only text, it will get sent to the browser as is.





Kirk Bailey wrote:
Ok, let's kick this around.

iterating an array(?; 1 dimensional listing of things) in php, I am creating a list of direcoties. I want to open and read in a file in each directory with a standard name, which contains a 1 line description of the directory and it's purpose.

Now, here's the existing code; this code is online NOW at this url:
http://www.howlermonkey.net/dirlisting.php

 * 1<?php # The next several lines declare an array of directories
 which are      2NOT to be listed!
  3$excludes[] = 'images';
  4$excludes[] = 'cgi-bin';
  5$excludes[] = 'vti_cnf';
  6$excludes[] = 'private';
  7$excludes[] = 'thumbnail';
  8
  9$ls = scandir(dirname(__FILE__));
 10foreach ($ls as $d) {
 11if (is_dir($d) && !preg_match('/^\./',basename($d)) &&
 12!in_array(basename($d),$excludes)) {
 13     echo '<li><a href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
 14 }
 15}
 16?>*

Let's say the file to read in /elite is named 'desc.txt'.
It looks like you want me to modify line 13 to say:
echo '<li><a href="'.$d.'">'.include($d.'desc.txt').'</a><br/ >'.PHP_EOL;

so, if the file '/elite/desc.txt' contains the line

 *82nd Airbourne - We are an elite unit of army Paratroopers
 *

Then that element in the list would appear as:

 * 82nd Airbourne  -We are an elite unit of army Paratroopers

And would be clickable. Let's try it and see if this hound hunts.

Matt Graham wrote:
From: Kirk Bailey <[email protected]>

OK, now here's a giggle; I like ssi includes. If I put the script
in as an ssi include, will it still work?


If you're using Apache, and you do

<!--#include virtual="something.php" -->

...the PHP in something.php will execute and produce output, but
something.php will not have any access to $_GET or $_POST or $_SESSION or any
of those things.  This is generally not what you want.  If you do

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

...then something.php will be able to see and work with the superglobals that have been set up further up the page, which is *usually* what you want. You could try both approaches in a test env and see what you get. I'll use SSI for "dumb" blocks of text and php include for "smart" blocks of code, because
IME that tends to produce fewer instances of gross stupidity.

Note that YMMV on all this and ICBW.




--
end

Very Truly yours,
              - Kirk Bailey,
                Largo Florida

kniht +----- + | BOX | +----- + think



--
end

Very Truly yours,
               - Kirk Bailey,
                 Largo Florida

kniht +----- + | BOX | +----- + think


--- End Message ---
--- Begin Message ---
Hello,

i am sorry for it took ridiculously long for me to respond, but i have been
struggling with the issue ever since. I still don't really know what the
problem was, but i have managed to solve it through a shortcut.

I have tried everything recommended from you. In php.ini the shell_exec
wasn't listed under "disabled functions". I have tried adding a new user
with the (i presume) right permissions and starting the server through it. I
have tried busybox httpd and lighttpd with the new user, as well as with the
old "nobody". I have installed bash (instead of busyboxs' shell) and even
tried installing it in the root server folder (/var/www)- still the problem
persisted.

What i have done today is compiling php through buildroot and than copy over
the binary php-cgi to the embedded system. It worked like a charm- out of
the box, without the adjustments i made previously. So it seems- to me- that
this was a cross- compiling issue on my side. I don't know what have i done
wrong with the compilation, but right now i don't have much time to look it
up- i must proceed with the project. If i find out what was causing the
problem, i will be sure to post a solution here.

So- thank you very much. Your help is indeed appreciated.

Yours,

Boyan

1.

On Sun, Oct 31, 2010 at 11:24 PM, <[email protected]> wrote:

> After going over this a thousand times in my head and on sever.
> Here are a few reasons why a shell_exec will not function.
>
> First I tested the PHP CLI
> Can I see my version of php.
> $output = shell_exec('php -v');
> Echo $output;
> If I am not getting the information about the php version. Is shell_exec
> for
> php and http on?
>
> In your php.ini file there will be a section called disable_functions.
> If you are sure that shell_exec is not in the list.
>
>
> If neither of these are an issue for you, It has to be a permissions issue.
>
>
> I do not claim to be an expert in the shell_exec area of php, I cannot
> duplicate your error.
>
> Richard L. Buskirk
>
>
> -----Original Message-----
> From: Boyan Penev [mailto:[email protected]]
> Sent: Sunday, October 31, 2010 12:26 PM
> To: [email protected]
> Cc: [email protected]
> Subject: Re: [PHP] Problem with "shell_exec"
>
> Hello Richard,
>
> thank you for the prompt reply. I just tried to follow your suggestion, but
> i couldn't solve my problem:
>
> 1. I  have started the server with user "nobody". All the binaries i was
> trying to execute(e.g. /bin/ls) were "chown"-ed to the user "nobody"
> Problem presisted. Than i "chown" -ed /var/www to the user "nobody". Than
> the php and php-cgi interpreters as well- same thing.
> I tried the same with users "adm" and even "root"
>
> 2. I than gave the executables all the permissions possible("777"- i know
> it
> is dangerous, but i am just testing)- still nothing.
>  I copied the binaries from /bin to /var/www and tried to execute them
> with all kinds of permissions and "chown"'s- didn't work
>
> 3. Than i moved to the command line interpreter: i tried to execute it in
> /var/www and in the home folder. The permissions were also adjusted. I
> executed it with root privileges- i always get "Segmentation fault". One
> more thing- if i omit the arguments in shell_exec( "$output =
> shell_exec();)
> i get "Missing parameters" but no "Segmentation fault"
>
> Boyan
>
> On Sun, Oct 31, 2010 at 4:34 PM, <[email protected]> wrote:
>
> > It seems most probably a permission problem.  If I were you I would
> > check the permissions on the bin directory and the files in it.  PHP
> > will probably run under the uid of the webserver, which, on most
> > systems is either "apache" or "nobody" depending on the configuration.
> >
> > Richard L. Buskirk
> >
> >
> > -----Original Message-----
> > From: Boyan Penev [mailto:[email protected]]
> > Sent: Sunday, October 31, 2010 11:16 AM
> > To: [email protected]
> > Subject: [PHP] Problem with "shell_exec"
> >
> > Hello,
> >
> > i have installed php 5.2.14 on an embedded system. I have run a couple of
> > test scripts and everything works fine,
> >  but i can't get "shell_exec" to work. When i invoke "shell_exec", the
> > script doesn't get interpreted, instead
> > i get prompted to download the whole .php file. I have tried both
> /bin/php
> > and /bin/php-cgi- same problem.
> > When i try to execute it per command line instead of web server, with
> both
> > php and php-cgi i get "Segmentation fault"
> > and nothing happens. I use busybox http as a web server, if it's
> relevant.
> > Does anybody have an idea what the problem
> > might be? Any help would be appreciated.
> >
> > Best regards,
> >
> > Boyan
> >
> >
>
>

--- End Message ---

Reply via email to