Semi-newbie Question .... streams ...

Semi-newbie Question .... streams ...

am 15.11.2007 15:29:47 von Paul Farr

I'm working on one my first PHP projects, and I have extensive
experience with coding, so I know what I want to do is not
outrageous.

The short version is: I need to master the download stream for a file,
while delivering it to the enduser, I need to change some bytes in the
file data. A simple search and replace. I want to do it in the stream,
don't want to create a new file each time. The data is not a string,
it is in fact a serial number being inserted into an "EXE" download.

I "get" how to do this with ASP, but the active replace in PHP has
me totally lost.

Any hints ?


--
Paul S. Farr

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

Re: Semi-newbie Question .... streams ...

am 15.11.2007 16:13:47 von Per Jessen

Paul Farr wrote:

> The short version is: I need to master the download stream for a
> file, while delivering it to the enduser, I need to change some bytes=

> in the file data. A simple search and replace. I want to do it in the=

> stream, don't want to create a new file each time.=20

Assuming you've a URL along the lines of this:

nnnn.php?file=3D

nnnn.php:
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=3D\"whatever.=
exe\"");

$txt=3Dfile_get_contents(the file you want to send)

// do your editing on the string here

print $txt;
?>


/Per Jessen, Zürich

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

Re: Semi-newbie Question .... streams ...

am 15.11.2007 16:49:43 von parasane

On Nov 15, 2007 10:13 AM, Per Jessen wrote:
> Paul Farr wrote:
>
> > The short version is: I need to master the download stream for a
> > file, while delivering it to the enduser, I need to change some bytes
> > in the file data. A simple search and replace. I want to do it in the
> > stream, don't want to create a new file each time.
>
> Assuming you've a URL along the lines of this:
>
> nnnn.php?file=3D
>
> nnnn.php:
> > header("Content-Type: application/octet-stream");
> header("Content-Disposition: attachment; filename=3D\"whatever.ex=
e\"");
>
> $txt=3Dfile_get_contents(the file you want to send)
>
> // do your editing on the string here
>
> print $txt;
> ?>
>
>
> /Per Jessen, Zürich
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

I would actually make a slight modification to Per's code and
place the header(); information post-processing the data. Otherwise
you won't be able to properly display errors in the event of a
failure.


--=20
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.

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

Re: Semi-newbie Question .... streams ...

am 15.11.2007 17:09:54 von List Manager

Paul Farr wrote:
> I'm working on one my first PHP projects, and I have extensive
> experience with coding, so I know what I want to do is not
> outrageous.
>
> The short version is: I need to master the download stream for a file,
> while delivering it to the enduser, I need to change some bytes in the
> file data. A simple search and replace. I want to do it in the stream,
> don't want to create a new file each time. The data is not a string,
> it is in fact a serial number being inserted into an "EXE" download.
>
> I "get" how to do this with ASP, but the active replace in PHP has
> me totally lost.
>
> Any hints ?
>
>

If this is an EXE, I assume it is in binary format?

If so, would not your serial number have been compiled into the software?

Wouldn't a simple search/replace fail in this case?

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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

Re: Semi-newbie Question .... streams ...

am 15.11.2007 18:40:57 von Per Jessen

Jim Lucas wrote:

> If this is an EXE, I assume it is in binary format?
>=20
> If so, would not your serial number have been compiled into the
> software?
>=20
> Wouldn't a simple search/replace fail in this case?

If the string (the serial number) is unique, and all in one place, a
simple search/replace will do fine.=20


/Per Jessen, Zürich

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

Re: Semi-newbie Question .... streams ...

am 15.11.2007 20:12:42 von List Manager

Per Jessen wrote:
> Jim Lucas wrote:
>
>> If this is an EXE, I assume it is in binary format?
>>
>> If so, would not your serial number have been compiled into the
>> software?
>>
>> Wouldn't a simple search/replace fail in this case?
>
> If the string (the serial number) is unique, and all in one place, a
> simple search/replace will do fine.
>
>
> /Per Jessen, Zürich
>

Well, then in this case, I would get the two strings that you want to work with and then do
something like this. I would try and keep the binary organized in such a way that the marker string
would be at the beginning of the file. One problem you might run into doing it this way is that you
might catch the marker string on your chunk block break point. In that case, it will not work.

By no means is this a complete script, or should it be used without some additional sanity checks

USE AT YOUR OWN RISK


$chunk_size = 1024;
$marker = 'somestring';
$serial = 'somenewstring';

// Please to sanity checks here to make sure they are not trying to get protected files :)
$filename = $_GET['filename'];

// Try and open file, if you can't, display error
if ( ( $fh = fopen($filename, 'r') === false ) {
echo "Could not get file";
die();
}

// If we are still go2go, send headers for download
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"whatever.exe\"");

// Get a line of data
while ( $data = fgets($fh, $chunk_size) ) {

// Check data for serial marker
if ( strpos($data, $marker) !== false ) {

// if found, replace marker with actual number
$data = str_replace($marker, $serial, $data);
}

// Send data to browser
echo $data;

}
exit;
?>

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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