#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595

    Upload jar file via fpassthru


    I am trying to upload a file using the following:
    PHP Code:
    $file=fopen($_POST['membernumber']."/KCBSEvents.jar","rb");
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: application/java-archive");
    header("Content-Disposition: attachment; filename=\"KCBSEvents.jar\"" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($_POST['membernumber']."/KCBSEvents.jar"));
    fpassthru($file);
    fclose($file); 
    The problem is that the file on the server is fine, I can run the jar file. However, after uploading the file to the client, when I try to run it, I get a corrupt jar file error. In examining the jar file as a zip it looks OK. What am I doing wrong? TIA.
    Last edited by gw1500se; September 29th, 2012 at 06:48 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Does the md5 hash of the transferred file match the md5 hash of the source file? That's a better way of comparing them to see whether the issue is related to the transfer.

    Your code looks OK, but make sure you're validating $_POST['membernumber']. By sticking some null bytes at the end someone could potentially pull any file from your server with that depending on your server OS.

    If the md5 hashes do not match, the most likely culprit is some whitespace at the beginning or end of your PHP code.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    Thanks for the reply and the hashes are different (not a surprise if Java thinks one is corrupt and the other not). I thought if there was whitespace before the headers they would not come out at all. I did not see any whitespace but to be sure I added ob_start to the beginning of my script and then ob_clean just before the header calls. It did not help. To verify, I copied the file over the network from the server to the same destination on the client. That did work so clearly there is something in my PHP script causing this.

    The $_POST value is checked against a database.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    With output buffering it could be possible to "output" some data before a call to header, but that is probably not the case here.

    You could write a PHP script to output each byte of the file to a line in a txt file, then using a diff tool you can see exactly where the difference is. Use the ord function to convert the byte to a number.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    It just occurred to me what the problem might be, although that I can do a network copy gives me pause. The jar file was generated on a Linux server and the client is Windows. I thought jar files are immune to this but perhaps it is a new line issue. I am using a binary upload and maybe I shouldn't.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    I now know what the problem is but not where it is coming from or how to fix it. Looking at the file on the client, it has been prepended with a record. That record appears to be the value in $file. It is in reverse byte order which is a bit of a surprise (I thought that died with DEC). Anyone have any ideas?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,601
    Rep Power
    595
    I found 2 problems and it was a struggle. I added some debug that I forgot about and that was where the prepend problem arose. I had this problem before adding that debug so removing it did not help. However, I finally found an appended record as well. As soon as I saw </body></html> in a hex editor it was obvious. Apparently, zip programs do not look past the zip EOF character so they don't care what is there. That is not the case with Java so it thought the jar file was corrupt because it looked beyond the EOF character and found the superfluous stuff.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo