Re: code quest

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4 December 2010 20:58, Kirk Bailey <kbailey@xxxxxxxxxxxxxxxx> 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 <kbailey@xxxxxxxxxxxxxxxx>
>>>>>
>>>>>> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux