我现在面临一个很奇怪的问题,同时上载从iPhone的图像。 当我上传图片从iPhone,在服务器上的图像被创建,但与0的大小。
我已经使用了许多PHP函数接收白白从字节的图像,但所有,从iPhone我送的图像以二进制形式的httppost(多内容类型)。
所有的代码如下所示。 以下是我所使用的各种PHP方法,但都没有工作。
$id=rand() ; // for image name
//=====================1st method========================
if(isset($_REQUEST['image_string'])) {
$imgData =@$_REQUEST['image_string'];
header('Content-Type:image/jpg; charset=utf-8');
$file = fopen("new_arrivals_img/".$id.".jpg", 'wb');
fwrite($file, $binary);
fclose($file);
}
//========================2nd method=====================
if(isset($_REQUEST['image_string'])) {
$imgData =$_REQUEST['image_string'];
$im = imagecreatefromstring($imgData);
$src="new_arrivals_img/".$id.".png";
imagepng($im,$src);
}
//=======================3rd method==================
if(isset($_REQUEST['image_string'])) {
$target_path = "new_arrivals_img/";
$target_path = $target_path.basename($_FILES['image_string']['name']);
move_uploaded_file($_FILES['image_string']['tmp_name'], $target_path);
}
//===================4th method ===========
if(isset($_REQUEST['image_string'])) {
$imageString = file_get_contents($_REQUEST['image_string']);
$save = file_put_contents("new_arrivals_img/".$id.".png",$_REQUEST['image_string']);
}
//==========================5th method =================
if(isset($_REQUEST['image_string'])) {
copy($_REQUEST['image_string'], "new_arrivals_img/".$id.".png");
}
//======6th method =========================================
if(isset($_REQUEST['image_string'])) {
$url = $_REQUEST['image_string'];
$dir = "new_arrivals_img/";
$lfile = fopen($dir . basename($url), "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FILE, $lfile);
curl_exec($ch);
fclose($lfile);
curl_close($ch);
}
IOS代码给出如下:
//image_string3 is parameter in which imagedata1 passed and server url is // http://192.95.48.44/image_binary/index.php
NSData *imageData1 = UIImageJPEGRepresentation(myImage,0.2); //change Image to NSData
if (imageData1 != nil)
{
NSString *urlString = @"http://192.95.48.44/image_binary/index.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image_string\"; filename=\"test123.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData1]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSLog(@"body %@",body);
NSLog(@"request %@",request);
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"returning strin %@",returnString);
NSLog(@"finish");
}