3

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!";
}
?>
9
  • Sounds like a BOM (Byte Order Mark). What encoding is the file? Commented Feb 1, 2011 at 4:18
  • If you replace fputs with fwrite does the problem go away? (I know they're supposed to be synonyms, but at least in C fputs adds a newline to the output; so it's possible that the PHP function has a bug making it add the newline...) Commented Feb 1, 2011 at 4:34
  • 1
    fputs($fh, $nextSlice, count($nextSlice) - 2) ? Commented Feb 1, 2011 at 4:36
  • 1
    When does the size change (\r\n added)? Is it different after move_uploaded_file()? or not until you run file_get_contents()? Or it is when you write it out that the \r\n is added? My hunch is that the browser is adding it, you'll have to strip it out upon receipt. Commented Feb 1, 2011 at 4:42
  • 1
    move_uploaded_file adds the 2 bytes (I had neglected to check that first... good catch!) Commented Feb 1, 2011 at 4:55

2 Answers 2

1

Is the file binary? I'm thinking that file_get_contents is causing problems because it's treating it like a string. Maybe you should try fread instead?

Sign up to request clarification or add additional context in comments.

6 Comments

that sounded promising, but no dice... same result. I should note that looking at hex edit, the last word of my original file is 0D0A and I'm seeing that the last two words (with the one extra word) is a repetition of that (0D0A 0D0A). I'll test now to see if that could be a coincidence (highly unlikely).
CrLF... what does that mean? I'm not sure what to do about it... I don't want it there... it's not part of what I'm sending :)
@Dr Dredel carriage return, line feed aka \r\n
@Dr.Dredel: It means you are running on Windows (ugh) and didn't include the b flag for file reads, thus making the C file api translate linefeeds into windows linebreaks.
@Dr.Dredel: Macs used to have a similar conversion scheme. Check your PHP_EOL. Otherwise this would indicate a problem with the multipart/form-data request body (e.g. client didn't send a content-length: field).
|
0

The solution turns out to be this:

fwrite($fh, $GLOBALS["HTTP_RAW_POST_DATA"]);

I may be doing something wrong in my request that when I use the method I described in the question, the file gets written with the extra 0D0A at the end, but the above method for extracting the data has it arriving intact and exactly the right length.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.