php-general Digest 3 Mar 2010 17:54:26 -0000 Issue 6619

Topics (messages 302495 through 302514):

Re: When to use namespaces
        302495 by: Adam Richardson

Re: inexplicable behaviour of pre- and post-increment operators
        302496 by: Adam Richardson
        302511 by: haliphax

Re: Wondering if anyone has experince with lastRSS
        302497 by: Rene Veerman

Re: session.entropy_file and hostname
        302498 by: Sascha Wojewsky
        302502 by: Daniel Egeberg

Memory investigation
        302499 by: Larry Garfield
        302504 by: user.domain.invalid
        302513 by: Larry Garfield
        302514 by: David Otton

svg 2 gif/png
        302500 by: Michael A. Peters
        302501 by: Michael A. Peters
        302503 by: Michael A. Peters
        302505 by: Michael A. Peters

Help with exec.
        302506 by: Paul Halliday
        302507 by: Richard Quadling
        302508 by: Teus Benschop
        302509 by: Paul Halliday
        302510 by: Ian
        302512 by: Paul Halliday

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Thanks, Richard, I wasn't aware of that function.  Another nice option.

On Tue, Mar 2, 2010 at 6:18 AM, Richard Quadling
<rquadl...@googlemail.com>wrote:

> On 1 March 2010 19:34, Adam Richardson <simples...@gmail.com> wrote:
> > I use namespaces within my web framework because the framework takes a
> more
> > functional approach (no objects are created within the framework other
> than
> > from existing classes such as PDO or Exception, immutability is promoted,
> > etc.), and in this context, the namespaces felt quite natural for
> breaking
> > up the various groupings of functions.
> >
> > My framework does require PHP 5.3, and, honestly that has caused some
> pain
> > in some situations to get that supported.
> >
> > However, using namespaces does offer some flexibility that naming
> > conventions can't.  If the objects in your framework make use of frequent
> > static method calls, maybe it's worth it.  For instance, if you've used
> the
> > naming convention, you might have to call a static method like below:
> >
> > App_Util_DB_Query::Insert();
> >
> > Namespaces allow you to shorten subsequent calls, such as:
> >
> > use App\Util\DB\Query as Query;
> >
> > Query::Insert();
> >
> > Just a quick couple thoughts on the decision.  Both have their strengths
> :)
> >
> > Adam
> >
> > On Mon, Mar 1, 2010 at 11:14 AM, Auke van Slooten <a...@muze.nl> wrote:
> >
> >> Hi everyone,
> >>
> >> I'm doing a small hobby project to better my understanding of php5,
> >> specifically php5.3 and I'm wondering when a namespaced project is
> better
> >> and when it is better to simply use a prefix to all class names.
> >>
> >> I've been trying to get a feeling for what is considered the best
> practice,
> >> but most of the pages dealing with namespaces start with the assumption
> that
> >> you are building a complex application with lots of modules and say
> things
> >> like:
> >>
> >> Namespaces should be all lowercase and must follow the following
> conention:
> >>   <vendor>\<package_name>\
> >>
> >> (thats from the php.standards mailing list btw)
> >>
> >> In my case the project is a single module, single php file, with about 6
> >> classes. It is an OO wrapper for PHP's xmlrpc methods (client and
> server)
> >> and meant to be used in a number of different projects.
> >>
> >> Is it considered a good idea to use a namespace in such a case? And if
> so,
> >> what should that be? I've named the project 'ripcord', and used that as
> a
> >> namespace as well. I could probably name it 'muze.ripcord', but somehow
> that
> >> feels less 'open' to me.
> >>
> >> Thanks in advance for any thoughts,
> >> Auke van Slooten
> >> Muze (www.muze.nl)
> >>
> >> PS. The project is at http://code.google.com/p/ripcord/, the PHP5.3
> >> version is at
> >> http://code.google.com/p/ripcord/source/browse/#svn/branches/php5.3
> >>
> >>
> >> --
> >> PHP General Mailing List (http://www.php.net/)
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >>
> >
> >
> > --
> > Nephtali:  PHP web framework that functions beautifully
> > http://nephtaliproject.com
> >
>
> Something I came across which has been useful in removing the long
> names (<php5.3 namespaces), is the use of class_alias().
>
> My specific use is in DocBlocks for SOAP services. Using the Zend
> AutoLoader, my classes are structured so that
> Namespace_Package_Class_Exception is available in
> \includes\Namespace\Package\Class\Exception.php
>
> But having those long names in the SOAP WSDL file were a bit
> cumbersome. So by having a class_alias in the important class files
> (just after the class definition), I could use the alias names in the
> docblock and all is well.
>
>
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> ZOPA : http://uk.zopa.com/member/RQuadling
>



-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

--- End Message ---
--- Begin Message ---
Thanks for taking time to provide the examples, Clancy, I'll know what
potential pitfalls to wary of now :)

On Fri, Feb 26, 2010 at 11:01 PM, <clanc...@cybec.com.au> wrote:

> A week ago Dasn asked a question about converting arrays, and I quoted one
> possible way of
> achieving his task, using the operation:
>
> $i = 0; while ($i < $k) { $b[$a[$i++]] = $a[$i++];  }
>
> I added the comment that "I have always been wary of using statements like
> this because I
> was unsure when the incrementing would occur, so I tried it."
>
> I received several CC e-mails replying to this post, including one rather
> critical comment
> to the effect that pre-and post-increment were all quite simple, and I
> really ought to
> learn the fundamentals before I started trying to do anything elaborate.
>
> I posted a reply to these e-mails, but as neither they, nor my reply, or
> any follow-up
> discussion ever appeared in the discussion group I will repost this reply.
>  (I did have a
> power failure at this time, so it is conceivable that any follow-up was
> lost as a result
> of a glitch in my mailer, but I think it is more likely that there was a
> glitch in the
> discussion group server.)
>
> Unfortunately things aren't nearly as simple as this writer believes. The
> rule I have
> always used is that if you use the same variable as an index on both sides
> of an assign
> statement it is not safe to change the value of the index within the
> statement. While I
> have achieved the result I wanted in the example above (using PHP 5.1.6 --
> there is no
> guarantee it would work with other implementations of PHP) the results of
> doing this in
> the general case can be quite inexplicable.
>
> The particular case which prompted my comment was the one where you want to
> copy part of
> one array into the corresponding elements of another array.  In accordance
> with my rule, I
> normally write:
>
> $i = 0; $j=count($a); while ($i < $j) { $b[$i] = $a[$i]; ++$i; }
>
> It is tempting to try to put the increment into the assignment statement.
> Clearly the
> value of $a[$i] has to be read before it can be written to $b[$i], so the
> logical
> expression would be:
>
> while ($i < $j) { $b[$i++] = $a[$i]; }  A.
>
> However if you try this, you get $b[1] = $a[0], and so on. But if you try
> the alternative:
>
> while ($i < $j) { $b[$i] = $a[$i++]; }          B.
>
> You get $b[0] = $a[1], and so on (as you would expect).
>
> Out of curiosity, I then tried:
>
> $i = -1; $j=count($a) - 1; while ($i < $j) { $b[$i] = $a[++$i]; }
>     C
>
> This gave the desired result, and seemed moderately logical. However when I
> tried:
>
> $i = -1; $j=count($a) - 1; while ($i < $j) { $b[++$i] = $a[$i]; }
>     D
>
> This gave exactly the same result.  It is quite impossible to explain the
> results in cases
> A and D from the definitions of the pre-and post-increment operator, so I
> think I will
> stick to my safe rule!
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

--- End Message ---
--- Begin Message ---
> On Fri, Feb 26, 2010 at 11:01 PM, <clanc...@cybec.com.au> wrote:
> > while ($i < $j) { $b[$i] = $a[$i++]; }          B.
> >
> > You get $b[0] = $a[1], and so on (as you would expect).
>

Wouldn't that be $b[0] = $a[0], with the value of $i being 1 *after* the
statement was finished executing? You used a post-decrement operator on $i
at the end of your statement, so I don't think that $i would be increased
before being used to index into the $a array.


// Todd

--- End Message ---
--- Begin Message ---
i did see some useful links..

what are you trying to achieve with it?

On Tue, Mar 2, 2010 at 10:19 PM, Watson Blair <bestudios...@gmail.com> wrote:
> ya, tried that one and next to nada came up... well nothing useful
> anyways....
>
> On Sun, Feb 28, 2010 at 2:37 PM, Rene Veerman <rene7...@gmail.com> wrote:
>>
>> yea, google for "lastrss".. or get wild and google for "lastrss
>> example or tutorial"
>>
>>
>> On Sun, Feb 28, 2010 at 12:05 AM, Watson Blair <bestudios...@gmail.com>
>> wrote:
>> > Hey all,
>> > I'm looking at lastRSS as a solution for displaying an Ebay RSS feed on
>> > a
>> > website, however i'm having a hard time wrapping my head around it,
>> > could
>> > you guys suggest a good tutorial? also, if there are better solutions
>> > for
>> > what i'm trying to do, bring it on.
>> > Thanks,
>> > Watson
>> >
>
>

--- End Message ---
--- Begin Message ---
thank you for your answer,

> If you want server-unique session ids, use session_name() before
> session_start()..?

i cannot use session_name, because i've to regenarete a session id by 
permanent login.
i'm using session_regenerate_id, and session_name doesen't works with it (?)

> session.entropy_file seems to be used only with /dev/urandom,
> and seems to be used to _increase_ the differences betweeen
> session ids.

i've thought that i can uses any other files with session.entropy_file? 



--- End Message ---
--- Begin Message ---
On Wed, Mar 3, 2010 at 07:28, Sascha Wojewsky <sascha.wojew...@heinze.de> wrote:
> thank you for your answer,
>
>> If you want server-unique session ids, use session_name() before
>> session_start()..?
>
> i cannot use session_name, because i've to regenarete a session id by
> permanent login.
> i'm using session_regenerate_id, and session_name doesen't works with it (?)

It should work fine. You must change the session name (either using
the session_name() function or by changing session.name in php.ini)
before you call session_start().

Note that the session name and the session ID are two different things.

-- 
Daniel Egeberg

--- End Message ---
--- Begin Message ---
Hi folks.  I have a complicated PHP app that is eating up more memory than I 
think it should.  I have a couple of theories as to where it could be going, 
but I need some way to verify it.

There are a number of large data structures (mostly arrays) that get built up 
throughout the course of the request.  What I'd like to be able to do is at 
certain points check to see how much memory those data structures are using.  
Because they're not all built at once, the usual "check memory before, build, 
check after" routine won't work.  Plus, that gets screwed up by PHP's use of 
copy-on-write at times.

I know that it would result in essentially over-reporting, but I would ideally 
like to be able to ignore the copy-on-write issue and say "if this variable 
were the only thing in memory (and its dependents, of course, for a nested 
array), what would its memory usage be?  I just have no idea how to do that.

Anyone have a suggestion for how to accomplish that?

--Larry Garfield

--- End Message ---
--- Begin Message --- ON Linux I have kcacheGrind setup with Xdebug and I find it is a nice little thing to have. It won't tell you the memory consumed but it will find cycles and display object maps. if you have Kcachegrind it is likely you have valgrind installed.
http://www2.mandriva.com/
http://valgrind.org/
http://kcachegrind.sourceforge.net/html/Home.html
http://xdebug.org/
Larry Garfield wrote:
Hi folks. I have a complicated PHP app that is eating up more memory than I think it should. I have a couple of theories as to where it could be going, but I need some way to verify it.

There are a number of large data structures (mostly arrays) that get built up throughout the course of the request. What I'd like to be able to do is at certain points check to see how much memory those data structures are using. Because they're not all built at once, the usual "check memory before, build, check after" routine won't work. Plus, that gets screwed up by PHP's use of copy-on-write at times.

I know that it would result in essentially over-reporting, but I would ideally like to be able to ignore the copy-on-write issue and say "if this variable were the only thing in memory (and its dependents, of course, for a nested array), what would its memory usage be? I just have no idea how to do that.

Anyone have a suggestion for how to accomplish that?

--Larry Garfield

--- End Message ---
--- Begin Message --- Yep, I'm familiar with XDebug and KCacheGrind. As you say, though, it doens't (as far as I am aware) offer the particular data that I'm after. We've already got cachegrind gurus working on the code who know how to use it better than I do. :-) What I'm looking for is "see this big cache object we've been building up in memory for this whole page request? Now that we're done with the page, how big is it, really?"

--Larry Garfield

u...@domain.invalid wrote:
ON Linux I have kcacheGrind setup with Xdebug and I find it is a nice little thing to have. It won't tell you the memory consumed but it will find cycles and display object maps. if you have Kcachegrind it is likely you have valgrind installed.
http://www2.mandriva.com/
http://valgrind.org/
http://kcachegrind.sourceforge.net/html/Home.html
http://xdebug.org/
Larry Garfield wrote:
Hi folks. I have a complicated PHP app that is eating up more memory than I think it should. I have a couple of theories as to where it could be going, but I need some way to verify it.

There are a number of large data structures (mostly arrays) that get built up throughout the course of the request. What I'd like to be able to do is at certain points check to see how much memory those data structures are using. Because they're not all built at once, the usual "check memory before, build, check after" routine won't work. Plus, that gets screwed up by PHP's use of copy-on-write at times.

I know that it would result in essentially over-reporting, but I would ideally like to be able to ignore the copy-on-write issue and say "if this variable were the only thing in memory (and its dependents, of course, for a nested array), what would its memory usage be? I just have no idea how to do that.

Anyone have a suggestion for how to accomplish that?

--Larry Garfield


--- End Message ---
--- Begin Message ---
On 3 March 2010 15:49, la...@garfieldtech.com <la...@garfieldtech.com> wrote:

> Yep, I'm familiar with XDebug and KCacheGrind.  As you say, though, it
> doens't (as far as I am aware) offer the particular data that I'm after.
>  We've already got cachegrind gurus working on the code who know how to use
> it better than I do. :-)  What I'm looking for is "see this big cache object
> we've been building up in memory for this whole page request?  Now that
> we're done with the page, how big is it, really?"

You could try Memtrack (http://pecl.php.net/package/memtrack) but I
can't comment as to what state it's in.

--- End Message ---
--- Begin Message ---
I'm moving all of my dynamic image generation to svg.
Not only does it look better, but it is less resource intensive on my server allowing me to generate the svg on demand instead of pre-generating (via cron) twice a month like I had to do with gd dynamic generation.

However, some browsers *cough*IE*cough* do not support SVG, so I still need png or gif fallback (I'll decide which after investigating size difference).

Example SVG to convert -

http://www.shastaherps.org/map/map22.svg

Using convert from ImageMagick in the CLI is fast enough that I may just use the ImageMagick php module and do the fallback dynamic for the IE users, but I've run into a bit of a snag - it seems that ImageMagick doesn't understand xlink.

IE running

convert --size=800x574 map22.svg map22.png

on above file results in a nice pretty map with the county and text, but the colored hexagons are not displayed.

Is there an easy way around this? IE a php class/function that understands SVG w/ xlink and can replace the use tags with the polygons they refer to? If not, I'll have to try to write one, but I'd rather not ...

Thanks for suggestions. It is too bad ImageMagick doesn't understand the use tag and xlink, that is one of the more useful features of SVG that I have personally found, makes dynamic generation so much easier.
--- End Message ---
--- Begin Message ---
Michael A. Peters wrote:
*snip*

Is there an easy way around this? IE a php class/function that understands SVG w/ xlink and can replace the use tags with the polygons they refer to? If not, I'll have to try to write one, but I'd rather not ...

I just did, haven't tested yet, but this may work -

function use2polygon($use,$polygon) {
   // get the xy coords
   $x = 0; $y = 0;
   if ($use->hasAttribute('x') {
      $x = 0 + $use->getAttribute('x');
      }
   if ($use->hasAttribute('y') {
      $y = 0 + $use->getAttribute('y');
      }
   // clone the polygon
   if ($polygon->hasAttribute('points') {
$points = preg_replace('/\s+/',' ',$polygon->getAttribute('points'));
      $pointArray = explode(' ',$points);
      $sizeof = sizeof($pointArray);
      for ($i=0;$i<$sizeof;$i++) {
         $foo = explode(',',$pointArray[$i]);
         $foo[0] = $x + $foo[0];
         $foo[1] = $y + $foo[1];
         $pointArray[$i] = $foo[0] . ',' . $foo[1];
         }
      $points = implode(' ',$points);
      $newPolygon = $polygon->cloneNode(true);
      $newPolygon->setAttribute('points',$points);
      $use->parentNode->replaceChild($newPolygon,$use);
      }
   }

of course that requires domdocument and probably requires looping through the dom node list backwards, but hopefully that will do it.
--- End Message ---
--- Begin Message ---
Michael A. Peters wrote:
Michael A. Peters wrote:
*snip*

Is there an easy way around this? IE a php class/function that understands SVG w/ xlink and can replace the use tags with the polygons they refer to? If not, I'll have to try to write one, but I'd rather not ...

I just did, haven't tested yet, but this may work -

This does work (fixed version of function):

function use2polygon($use,$polygon) {
   // get the xy coords
   $x = 0; $y = 0;
   if ($use->hasAttribute('x')) {
      $x = 0 + $use->getAttribute('x');
      }
   if ($use->hasAttribute('y')) {
      $y = 0 + $use->getAttribute('y');
      }
   // clone the polygon
   if ($polygon->hasAttribute('points')) {
$points = preg_replace('/\s+/',' ',$polygon->getAttribute('points'));
      $pointArray = explode(' ',$points);
      $sizeof = sizeof($pointArray);
      for ($i=0;$i<$sizeof;$i++) {
         $foo = explode(',',$pointArray[$i]);
         $foo[0] = $x + $foo[0];
         $foo[1] = $y + $foo[1];
         $pointArray[$i] = $foo[0] . ',' . $foo[1];
         }
      $points = implode(' ',$pointArray);
      $newPolygon = $polygon->cloneNode(true);
      $newPolygon->setAttribute('points',$points);
      $use->parentNode->replaceChild($newPolygon,$use);
      }
   }

--- End Message ---
--- Begin Message ---
Michael A. Peters wrote:
Michael A. Peters wrote:
Michael A. Peters wrote:
*snip*

Is there an easy way around this? IE a php class/function that understands SVG w/ xlink and can replace the use tags with the polygons they refer to? If not, I'll have to try to write one, but I'd rather not ...

I just did, haven't tested yet, but this may work -

This does work (fixed version of function):

function use2polygon($use,$polygon) {
   // get the xy coords
   $x = 0; $y = 0;
   if ($use->hasAttribute('x')) {
      $x = 0 + $use->getAttribute('x');
      }
   if ($use->hasAttribute('y')) {
      $y = 0 + $use->getAttribute('y');
      }
   // clone the polygon
   if ($polygon->hasAttribute('points')) {
$points = preg_replace('/\s+/',' ',$polygon->getAttribute('points'));
      $pointArray = explode(' ',$points);
      $sizeof = sizeof($pointArray);
      for ($i=0;$i<$sizeof;$i++) {
         $foo = explode(',',$pointArray[$i]);
         $foo[0] = $x + $foo[0];
         $foo[1] = $y + $foo[1];
         $pointArray[$i] = $foo[0] . ',' . $foo[1];
         }
      $points = implode(' ',$pointArray);
      $newPolygon = $polygon->cloneNode(true);
      $newPolygon->setAttribute('points',$points);
      $use->parentNode->replaceChild($newPolygon,$use);
      }
   }


doh - for xml sanity, add this before the replaceChild

if ($newPolygon->hasAttribute('id')) {
         $newPolygon->removeAttribute('id');
         }

Now it works, and makes identical svg image that works with convert.

http://www.shastaherps.org/map/map22.svg vs
http://www.shastaherps.org/map/map22.svg?convert=true

--- End Message ---
--- Begin Message ---
I need to pipe some data to an external application.

I have this:

while ($row = mysql_fetch_array($theData[0])) {
    $src_ip[] = $row[0];
    $dst_ip[] = $row[1];
    $sig_desc[] = $row[2];

    $rec ++;
    if ( $rec == $recCount ) {
            break;
    }
}

for ($i = 0; $i < sizeof($src_ip); $i++) {
    $tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
}


The external program is called like:

cat results.csv | theprogram outputfilename

Is there a way mimic this w/o outputting $tmpResult to a file first?

Thanks.

--- End Message ---
--- Begin Message ---
On 3 March 2010 13:01, Paul Halliday <paul.halli...@gmail.com> wrote:
> I need to pipe some data to an external application.
>
> I have this:
>
> while ($row = mysql_fetch_array($theData[0])) {
>    $src_ip[] = $row[0];
>    $dst_ip[] = $row[1];
>    $sig_desc[] = $row[2];
>
>    $rec ++;
>    if ( $rec == $recCount ) {
>            break;
>    }
> }
>
> for ($i = 0; $i < sizeof($src_ip); $i++) {
>    $tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
> }
>
>
> The external program is called like:
>
> cat results.csv | theprogram outputfilename
>
> Is there a way mimic this w/o outputting $tmpResult to a file first?
>
> Thanks.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

I believe you can use popen() to open "theprogram" and pipe to it the
content and read back the results. All without writing to any files.



-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--- End Message ---
--- Begin Message ---
On Wed, 2010-03-03 at 13:04 +0000, Richard Quadling wrote:
> On 3 March 2010 13:01, Paul Halliday <paul.halli...@gmail.com> wrote:
> > I need to pipe some data to an external application.
> >
> > I have this:
> >
> > while ($row = mysql_fetch_array($theData[0])) {
> >    $src_ip[] = $row[0];
> >    $dst_ip[] = $row[1];
> >    $sig_desc[] = $row[2];
> >
> >    $rec ++;
> >    if ( $rec == $recCount ) {
> >            break;
> >    }
> > }
> >
> > for ($i = 0; $i < sizeof($src_ip); $i++) {
> >    $tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
> > }
> >
> >
> > The external program is called like:
> >
> > cat results.csv | theprogram outputfilename
> >
> > Is there a way mimic this w/o outputting $tmpResult to a file first?
> >
> > Thanks.
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> 
> I believe you can use popen() to open "theprogram" and pipe to it the
> content and read back the results. All without writing to any files.
> 

popen() either reads from a pipe or writes to a pipe, if I remember
well, not both. Teus.



--- End Message ---
--- Begin Message ---
I work by example :)

I can't find enough of an example to get me going with this.

I have this:

$glow = popen('afterglow.pl -c color.properties -s -e 3 -p 1 -l 2000 |
neato -Tpng -o /usr/local/www/test.png','r');

how do I feed my array to that?

Thanks.

On Wed, Mar 3, 2010 at 9:04 AM, Richard Quadling
<rquadl...@googlemail.com> wrote:
> On 3 March 2010 13:01, Paul Halliday <paul.halli...@gmail.com> wrote:
>> I need to pipe some data to an external application.
>>
>> I have this:
>>
>> while ($row = mysql_fetch_array($theData[0])) {
>>    $src_ip[] = $row[0];
>>    $dst_ip[] = $row[1];
>>    $sig_desc[] = $row[2];
>>
>>    $rec ++;
>>    if ( $rec == $recCount ) {
>>            break;
>>    }
>> }
>>
>> for ($i = 0; $i < sizeof($src_ip); $i++) {
>>    $tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
>> }
>>
>>
>> The external program is called like:
>>
>> cat results.csv | theprogram outputfilename
>>
>> Is there a way mimic this w/o outputting $tmpResult to a file first?
>>
>> Thanks.
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> I believe you can use popen() to open "theprogram" and pipe to it the
> content and read back the results. All without writing to any files.
>
>
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> ZOPA : http://uk.zopa.com/member/RQuadling
>

--- End Message ---
--- Begin Message ---
On 03/03/2010 13:01, Paul Halliday wrote:
> I need to pipe some data to an external application.
> 
> I have this:
> 
> while ($row = mysql_fetch_array($theData[0])) {
>     $src_ip[] = $row[0];
>     $dst_ip[] = $row[1];
>     $sig_desc[] = $row[2];
> 
>     $rec ++;
>     if ( $rec == $recCount ) {
>             break;
>     }
> }
> 
> for ($i = 0; $i < sizeof($src_ip); $i++) {
>     $tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
> }
> 
> 
> The external program is called like:
> 
> cat results.csv | theprogram outputfilename
> 
> Is there a way mimic this w/o outputting $tmpResult to a file first?
> 
> Thanks.
> 

Hi,

I have used this code to feed data to gpg and read back the encrypted
result, Im sure you can adapt it to your needs.

function Encrypt($data){
        
        # http://www.theoslogic.com/scripts/php-gpg/
        
        $gpg_command="/usr/bin/gpg $parameters";
        
        $errLog = "/tmp/errors.log";
        
        $dspecs = array(
                0=>array("pipe", "r"),
                1=>array("pipe", "w"),
                2=>array("file", $errLog, "a")
        );
        
        $encrypted="";
        $procdata="";
        
        $gpgproc = proc_open($gpg_command, $dspecs, $pipes);
        
        if (is_resource($gpgproc)) {
                fwrite($pipes[0], $data);
                fclose($pipes[0]);
                
                while($procdata = fgets($pipes[1], 1024)) {
                        $encrypted .= $procdata;
                }
                fclose($pipes[1]);
        }
        
        return $encrypted;
}

It works really well.

Regards

Ian
-- 


--- End Message ---
--- Begin Message ---
and its that easy!

it took me a minute to figure out; but all I had to do was:

if (is_resource($process)) {

    for ($i = 0; $i < sizeof($src_ip); $i++) {
        fwrite($pipes[0], "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n");
    }

    fclose($pipes[0]);
    fclose($pipes[1]);
    proc_close($process);
}

thanks.

On Wed, Mar 3, 2010 at 10:08 AM, Ian <php_l...@fishnet.co.uk> wrote:
> On 03/03/2010 13:01, Paul Halliday wrote:
>> I need to pipe some data to an external application.
>>
>> I have this:
>>
>> while ($row = mysql_fetch_array($theData[0])) {
>>     $src_ip[] = $row[0];
>>     $dst_ip[] = $row[1];
>>     $sig_desc[] = $row[2];
>>
>>     $rec ++;
>>     if ( $rec == $recCount ) {
>>             break;
>>     }
>> }
>>
>> for ($i = 0; $i < sizeof($src_ip); $i++) {
>>     $tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
>> }
>>
>>
>> The external program is called like:
>>
>> cat results.csv | theprogram outputfilename
>>
>> Is there a way mimic this w/o outputting $tmpResult to a file first?
>>
>> Thanks.
>>
>
> Hi,
>
> I have used this code to feed data to gpg and read back the encrypted
> result, Im sure you can adapt it to your needs.
>
> function Encrypt($data){
>
>        # http://www.theoslogic.com/scripts/php-gpg/
>
>        $gpg_command="/usr/bin/gpg $parameters";
>
>        $errLog = "/tmp/errors.log";
>
>        $dspecs = array(
>                0=>array("pipe", "r"),
>                1=>array("pipe", "w"),
>                2=>array("file", $errLog, "a")
>        );
>
>        $encrypted="";
>        $procdata="";
>
>        $gpgproc = proc_open($gpg_command, $dspecs, $pipes);
>
>        if (is_resource($gpgproc)) {
>                fwrite($pipes[0], $data);
>                fclose($pipes[0]);
>
>                while($procdata = fgets($pipes[1], 1024)) {
>                        $encrypted .= $procdata;
>                }
>                fclose($pipes[1]);
>        }
>
>        return $encrypted;
> }
>
> It works really well.
>
> Regards
>
> Ian
> --
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---

Reply via email to