大家好,希望有人能帮忙-要完成我的android应用程序,我只需要完成课程,用户就可以将图像上传到服务器(MySql)。
除了图像的格式是application/octet流而不是从我的android应用程序发送的图像/jpeg之外,所有的都工作得很好?我是否可以打开文件应用程序/八位字节流并将其更改为图像/jpeg类型??
php文件上传。。。
$fileName = $_FILES['uploaded']['name'];
$tmpName = $_FILES['uploaded']['tmp_name'];
$fileSize = $_FILES['uploaded']['size'];
$fileType = $_FILES['uploaded']['type'];
$fp = fopen($tmpName, 'rb');
$content = fread($fp, $fileSize);
$content = addslashes($content);
适用于表单上传表单,没有问题。但是,当从android应用程序中使用错误的图像类型时,它会将blob文件保存为空0,$fileSize显示真实大小,但当创建$content时,它根本不显示
177 rome 2.jpg image/jpeg 6876[BBLOB-6.7 KiB]p(这来自网页)215 myImage.jpg应用程序/八位字节流1066[BLOB-0 B](来自手机)
Thx guys
application/octet-stream
或image/jpeg
只是HTTP客户端(浏览器)添加的mime类型,作为二进制文件数据本身旁边的附加信息。
所以哑剧类型在这里应该没有任何问题。只是在程序失败的情况下,您可以看到请求之间的差异,但这一定不是问题的原因。
所以你基本上看错地方了。
相反,您应该在代码中添加错误和条件检查。特别是先决条件检查,这样您就知道脚本可以正确地操作它所提供的数据。
从你提出的问题来看,没有什么比这更具体的了。希望这无论如何都有帮助。
此外,PHP手册中有一节关于文件上传,其中包含一些有用的信息,包括处理错误案例。
除此之外,您的代码可能只是在如何将数据插入数据库方面存在问题,因此从技术上讲,这个问题可能与文件上传完全无关。
一些机器人确实使用mime类型application/octet-stream
而不是正确的类型上传。
为此,我制作了以下内容:
//the uploaded file
$img = $_FILES['img'];
//array for type if octet
$realMime = array(
'useNew' => false,
'type' => ''
);
if($img['type'] == "application/octet-stream"){
$imageMime = getimagesize($img['tmp_name']); // get temporary file REAL info
$realMime['type'] = $imageMime['mime']; //set in our array the correct mime
$realMime['useNew'] = true; //set to true for next if
}
//now check if we will use the realMime or the one sent by browser
//$mimeCheck is the things you want to check. In my case i just wanted PNG or JPG
if($realMime['useNew'] == true){ //if true, use realMime to check
$mimeCheck = ($realMime['type'] != "image/png" && $realMime['type'] != "image/jpeg" && $realMime['type'] != "image/jpg");
}else{ //if not using real mime, go with the one browser sent us
$mimeCheck = ($img['type'] != "image/png" && $img['type'] != "image/jpeg" && $img['type'] != "image/jpg");
}
//returns error if image not png/jpg/jpeg
if($mimeCheck){
//return some error
}
有了这个你就可以检查了。当然,必须为getimagesize()
制作一个错误处理程序,因为用户可以上传一个不是真正图像的八位位组流文件。
有关更多信息,请阅读PHP.net getimagesize()doc
在Android端,为了进行正确的调用,我们应该进行如下的web服务调用:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("https://someserver.com/api/path/");
postRequest.addHeader("Authorization",authHeader);
//don't set the content type here
//postRequest.addHeader("Content-Type","multipart/form-data");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
reqEntity.addPart("parm-name", new InputStreamBody(fileInputStream,"image/jpeg","file_name.jpg"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpclient.execute(postRequest);
}catch(Exception e) {
Log.e("URISyntaxException", e.toString());
}
我知道现在有点晚了,但我认为这个答案也可以帮助其他人。
我已在以下位置找到此:http://php.net/manual/en/features.file-upload.php
// DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['upfile']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
因此,如果你想检查mime类型,你应该选择finfo
,而不是$_FILE[type]
给出的mime类型。这可能是错误的,所以你可以自己猜测哑剧类型。特别是当设备不同时,比如在这种情况下的android。