奇怪的问题,同时发布图像从iPhone [英] Strange issue while posting image from iPhone
问题描述
我使用了许多PHP函数从字节接收图像,我发送的图像是一个二进制形式的httppost(多部分内容类型)。
所有代码在下面给出。以下是我已经使用过的各种PHP方法,但是他们都不工作。 //为图像名称
// =====================第一种方法============ ($ _ $ REQUEST ['image_string'])){
$ imgData = @ $ _
header('Content-Type:image / jpg; charset = utf-8');
$ file = fopen(new_arrivals_img /\".$ id。。jpg,wb);
fwrite($ file,$ binary);
fclose($ file);
}
// ========================第二种方法========= ============
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']);
}
// ==========================第五种方法======= ($ _ REQUEST ['image_string'])){
copy($ _ REQUEST ['image_string'],'new_arrivals_img /')
$ b $ if 。$ id为 PNG)。
}
// ======第六种方法=========================== ==============
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是imagedata1传递的参数,服务器url是// http://192.95.48.44/image_binary/index.php
NSData * imageData1 = UIImageJPEGRepresentation(myImage,0.2); //将图像更改为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];
$ b [body appendData:[[NSString stringWithFormat:@\r\\\
- %@ \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding ]];
[body appendData:[[NSString stringWithString:@Content-Disposition:form-data; name = \image_string\; filename = \test123.jpg\\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Type:application / octet-stream\r\\\
\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData1]];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ - \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//将帖子正文设置为reqeust
[request setHTTPBody:body];
NSLog(@body%@,body);
NSLog(@request%@,request);
//现在让连接到web
NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@returns strin%@,returnString);
NSLog(@finish);
解决方案在这个问题上两天,我终于得到了解决方案,这是我的服务器,不允许我动态或通过编程创建图像的问题。
虽然我已经给完全的权利(读,写,更新或777)我的文件夹,我正在创建图像。
我只是改变了服务器,它的工作完美,我使用的代码而AdamG提供的那个都为我工作。
I am facing a very strange issue while uploading an image from iPhone. When I upload image from iPhone,image on server gets created but with 0 size.
I have used many PHP functions to receive image from bytes but all in vain, from iPhone I am sending image an httppost in binary form (multipart content type).
All the code is given below. Following are the various PHP methods that I have used but neither of them is working .
$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 code is given below:
//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");
}
解决方案 After spending two days on this issue, i finally got the solution, it was issue in my server that was not allowing me to create image dynamically or through programming.
Although i had given full rights (read,write,update or 777) to my folder where i was creating image.
i just changed the server and it worked perfectly, the code i was using and the one provided by AdamG both worked for me.
这篇关于奇怪的问题,同时发布图像从iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!