I have a process that uploads files via PHP but the resulting files end up being 2 bytes larger than the source file. I'm not sure where these 2 bytes are coming from. (the actual process is a chunked upload where I slice up a file and upload the slices, each slice winds up arriving 2 bytes longer than it started, but I've tested with a single small file and it too arrives 2 bytes larger than the source).
I'm attaching my PHP... Is this a normal feature of PHP? I'm imagining some sort of null terminator or something (there does appear to be a \n at the end of each file that wasn't there initially). Do I need to read the file into a buffer and get rid of the last two bytes before reassembling my original? I have to imagine I'm doing something wrong, but I'm confounded as to what it would be.
If I do need to manually strip off those last two bytes what's the correct way to do that (it's a binary file) and then append the rest to the overall file I'm rebuilding?
EDIT
Each uploaded file is getting a 0D0A word added to the end as PHP saves it to the server. So... I guess the question is how to prevent this from happening.
<?PHP
$target_path = $_REQUEST[ 'path' ];
$originalFileName = $_REQUEST['original_file_name'];
$target_path = $target_path . basename( $_FILES[ 'Filedata' ][ 'name' ] );
if ( move_uploaded_file( $_FILES[ 'Filedata' ][ 'tmp_name' ], $target_path ) )
{
$newFilePath = $originalFileName; //this is the overall file being re-assembled
$fh = fopen($newFilePath, 'ab') or die("can't open file");
$nextSlice = file_get_contents($target_path); //this is the slice that's 2 bytes too big each time
fputs($fh, $nextSlice);
fclose($fh);
// unlink($target_path); //normally I'd delete the slice at this point, but I'm hanging on to it while I figure out where the heck the 2 extra bytes are coming from.
fclose($fh);
echo "SUCCESS";
}
else
{
echo "FAIL:There was an error uploading the file, please try again!";
}
?>
fputswithfwritedoes the problem go away? (I know they're supposed to be synonyms, but at least in Cfputsadds a newline to the output; so it's possible that the PHP function has a bug making it add the newline...)fputs($fh, $nextSlice, count($nextSlice) - 2)?