This is a discussion on Semi-newbie Question .... streams ... within the PHP General forums, part of the PHP Programming Forums category; I'm working on one my first PHP projects, and I have extensive experience with coding, so I know what ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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=<filename> nnnn.php: <?php header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"whatever.exe\""); $txt=file_get_contents(the file you want to send) // do your editing on the string here print $txt; ?> /Per Jessen, Zürich |
|
|||
|
On Nov 15, 2007 10:13 AM, Per Jessen <per@computer.org> 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=<filename> > > nnnn.php: > <?php > header("Content-Type: application/octet-stream"); > header("Content-Disposition: attachment; filename=\"whatever.exe\""); > > $txt=file_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. -- 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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 <?php $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 |