echo $_GET['test'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000000000)
) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
//insert into datab
...... ?>
在移动应用中,我使用 HTTPPOST 和 HTTP客户端来发送我的照片,问题是当我将内容类型设置为multipart时,它无法通过 php中的if()语句 我试图添加2种不同的内容类型,使用 HTTPURLConnection 而不是客户端,但我无法在此处发送我的图像是我的移动代码
private class UploadTask extends AsyncTask<Bitmap, Void, Void> {
private static final String TAG = "upload";
protected Void doInBackground(Bitmap... bitmaps) {
if (bitmaps[0] == null)
return null;
setProgress(0);
Bitmap bitmap = bitmaps[0];
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); // convert
// Bitmap
// to
// ByteArrayOutputStream
InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert
// ByteArrayOutputStream
// to
// ByteArrayInputStream
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(
"http://10.10.10.98:8888/upload_file.php"); // server
httppost.setHeader("Content-type",
"multipart/form-data; boundary=--14737809831466499882746641449");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity
.addPart("myFile", System.currentTimeMillis() + "", in);
/*
* httppost.setHeader( "Content-type",
* "multipart/form-data; boundary=--14737809831466499882746641449"
* )
*/
// httppost.setHeader("Content-type","image/jpg\r\n\r\n");
httppost.setEntity(reqEntity);
/*
* httppost.addHeader( "Content-type",
* "multipart/form-data; boundary=---------------------------14737809831466499882746641449"
* );
*/
// httppost.addHeader("Content-type","image/jpg\r\n\r\n");
// httppost.
// Header h=new Header();
Log.i(TAG, "request " + httppost.getRequestLine());
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (response != null)
Log.i(TAG, "response "
+ response.getStatusLine().toString());
} finally {
}
} finally {
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
这是我的addPart()
函数
public void addPart(final String key, final String fileName,
final InputStream fin, String type) {
writeFirstBoundaryIfNeeds();
try {
type = "Content-Type:" + "multipart/form-data; boundary="
+ boundary;
out.write(type.getBytes());
out.write(("Content-Disposition: form-data; name= file; filename=\""
+ fileName + ".jpeg\"\r\n").getBytes());
type = "Content-Type: image/jpg\r\n\r\n ";
out.write(type.getBytes());
/*
* out.write(("Content-Disposition: form-data; name=file; filename=\""
* + fileName + ".jpeg\"\r\n").getBytes());
*/
// out.write(type.getBytes());
out.write("\r\nContent-Transfer-Encoding: binary\r\n\r\n"
.getBytes());
final byte[] tmp = new byte[4096];
int l = 0;
while ((l = fin.read(tmp)) != -1) {
out.write(tmp, 0, l);
}
out.flush();
} catch (final IOException e) {
} finally {
try {
fin.close();
} catch (final IOException e) {
}
}
}