Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP adding 2 extra bytes to my uploaded file
    primarykey
    data
    text
    <p>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).</p> <p>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.</p> <p>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?</p> <hr> <p>EDIT</p> <hr> <p>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. </p> <pre><code>&lt;?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!"; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload