This is a discussion on Re: Binary? I don't know within the PHP Language forums, part of the PHP Programming Forums category; Lee wrote: >Hi, I am reading in a huge post request that represents an image with >the php ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Lee wrote:
>Hi, I am reading in a huge post request that represents an image with >the php file below. The resultant file should be in jpg format, but it >comes in weird. [...] >$imageString = $_POST['image']; The fact that your image data is present in $_POST seems to suggest that you didn't set the form's encoding type properly (unless something has changed after version 4.1.2, which is what I'm using). When uploading files, the form should look something like: <form name=frm action="page.php" enctype="multipart/form-data" method=post> Note the encoding type of multipart/form-data. Now, information about the submitted data from a file input is placed in the $_FILE array, as Richard Levasseur mentioned. The file contents is already placed in a temporary file. >$fpImage=fopen($imageFile,'w+b'); //remove the binary? Image data is binary data, so don't leave the "b" out. HTH, Gertjan. -- Gertjan Klein <gklein@xs4all.nl> |
|
|||
|
There is something I didn't mention: I am getting the POST from a java
applet, not a form. What's happening is that I created a jpeg inside the program, made a string out of it, converted it with urlencode, and then made a post request. On the php side, I want to decode it and write it to a binary file. Gertjan Klein wrote: > Lee wrote: > > >Hi, I am reading in a huge post request that represents an image with > >the php file below. The resultant file should be in jpg format, but it > >comes in weird. > > [...] > > >$imageString = $_POST['image']; > > The fact that your image data is present in $_POST seems to suggest that > you didn't set the form's encoding type properly (unless something has > changed after version 4.1.2, which is what I'm using). When uploading > files, the form should look something like: > > <form name=frm action="page.php" enctype="multipart/form-data" > method=post> > > Note the encoding type of multipart/form-data. Now, information about > the submitted data from a file input is placed in the $_FILE array, as > Richard Levasseur mentioned. The file contents is already placed in a > temporary file. > > >$fpImage=fopen($imageFile,'w+b'); //remove the binary? > > Image data is binary data, so don't leave the "b" out. > > HTH, > Gertjan. > -- > Gertjan Klein <gklein@xs4all.nl> |
|
|||
|
Lee wrote:
>There is something I didn't mention: I am getting the POST from a java >applet, not a form. >What's happening is that I created a jpeg inside the program, made a >string out of it, converted it with urlencode, and then made a post >request. If you are really doing a POST (as opposed to a GET), then why would you urlencode it? Posted data can basically be anything, including binary data. I suggest trying to remove the urlencode in the java applet, and check if that works. If not, the first thing to find out is where exactly things go wrong. You can use a tool like tcptrace to monitor what is being sent by your applet. Probably the best thing to do is to try and mimic what a browser sends when a file is being uploaded. That way, php will manage any encoding you use (in this case, you do have to use an encoding, perhaps base64), and save the data in a file for you. HTH, Gertjan. -- Gertjan Klein <gklein@xs4all.nl> |
|
|||
|
The only way that the entire string would transfer in the post request
was if I used urlencode. (this is where I was talking about the java applet if you're curious: http://groups.google.com/group/comp....861b228b?hl=en ) I have another clue to the puzzle in case it helps. If I view the string in notepad, it looks like what I want. However, if I view it in any other application (e.g. wordpad or firefox), it looks like what I posted. This is a major reason why I think this is something like a "binary" problem. Gertjan Klein wrote: > Lee wrote: > > >There is something I didn't mention: I am getting the POST from a java > >applet, not a form. > >What's happening is that I created a jpeg inside the program, made a > >string out of it, converted it with urlencode, and then made a post > >request. > > If you are really doing a POST (as opposed to a GET), then why would you > urlencode it? Posted data can basically be anything, including binary > data. I suggest trying to remove the urlencode in the java applet, and > check if that works. If not, the first thing to find out is where > exactly things go wrong. You can use a tool like tcptrace to monitor > what is being sent by your applet. > > Probably the best thing to do is to try and mimic what a browser sends > when a file is being uploaded. That way, php will manage any encoding > you use (in this case, you do have to use an encoding, perhaps base64), > and save the data in a file for you. > > HTH, > Gertjan. > -- > Gertjan Klein <gklein@xs4all.nl> |
|
|||
|
Lee wrote:
>The only way that the entire string would transfer in the post request >was if I used urlencode. That doesn't sound like the right approach to me though. PHP will have to have the entire image in a string, instead of just writing it to a file. This may give problems on larger images. I'd still suggest you change your applet to use the standard post method that browsers use. You'll need a header stating the Content-Type is multipart/form-data. The posted contents needs to be formatted like a MIME message. As far as I can tell you don't need to encode the data in any way. (A quick test using tcptrace shows that at least firefox posts image data unencoded.) I am in no way qualified to give Java advice (and this is not the newsgroup to give it in), but you may want to try using the apache commons HTTP client class: http://jakarta.apache.org/commons/httpclient/ I have used it successfully in the past to post data to a server. It takes care of all the low-level details for you, and can post the image contents in a browser-compatible way. Doing it this way you know that the PHP side will be trivial, as PHP will save the posted data in a file for you. HTH, Gertjan. -- Gertjan Klein <gklein@xs4all.nl> |
|
|||
|
Thanks, I'll try this!
Gertjan Klein wrote: > Lee wrote: > > >The only way that the entire string would transfer in the post request > >was if I used urlencode. > > That doesn't sound like the right approach to me though. PHP will have > to have the entire image in a string, instead of just writing it to a > file. This may give problems on larger images. > > I'd still suggest you change your applet to use the standard post method > that browsers use. You'll need a header stating the Content-Type is > multipart/form-data. The posted contents needs to be formatted like a > MIME message. As far as I can tell you don't need to encode the data in > any way. (A quick test using tcptrace shows that at least firefox posts > image data unencoded.) > > I am in no way qualified to give Java advice (and this is not the > newsgroup to give it in), but you may want to try using the apache > commons HTTP client class: > > http://jakarta.apache.org/commons/httpclient/ > > I have used it successfully in the past to post data to a server. It > takes care of all the low-level details for you, and can post the image > contents in a browser-compatible way. Doing it this way you know that > the PHP side will be trivial, as PHP will save the posted data in a file > for you. > > HTH, > Gertjan. > > -- > Gertjan Klein <gklein@xs4all.nl> |
|
|||
|
While I'm trying this, is there any other help...?
Lee wrote: > Thanks, I'll try this! > > Gertjan Klein wrote: > > Lee wrote: > > > > >The only way that the entire string would transfer in the post request > > >was if I used urlencode. > > > > That doesn't sound like the right approach to me though. PHP will have > > to have the entire image in a string, instead of just writing it to a > > file. This may give problems on larger images. > > > > I'd still suggest you change your applet to use the standard post method > > that browsers use. You'll need a header stating the Content-Type is > > multipart/form-data. The posted contents needs to be formatted like a > > MIME message. As far as I can tell you don't need to encode the data in > > any way. (A quick test using tcptrace shows that at least firefox posts > > image data unencoded.) > > > > I am in no way qualified to give Java advice (and this is not the > > newsgroup to give it in), but you may want to try using the apache > > commons HTTP client class: > > > > http://jakarta.apache.org/commons/httpclient/ > > > > I have used it successfully in the past to post data to a server. It > > takes care of all the low-level details for you, and can post the image > > contents in a browser-compatible way. Doing it this way you know that > > the PHP side will be trivial, as PHP will save the posted data in a file > > for you. > > > > HTH, > > Gertjan. > > > > -- > > Gertjan Klein <gklein@xs4all.nl> |