Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将图像上载到iOS不工作的服务器_Php_Ios_Http_Http Post - Fatal编程技术网

Php 将图像上载到iOS不工作的服务器

Php 将图像上载到iOS不工作的服务器,php,ios,http,http-post,Php,Ios,Http,Http Post,我试图上传一个图像到我的php服务器,但它不工作。当我试图上传文件时,屏幕只是冻结(这是预期的-我使用sendSynchronousRequest,直到我可以让它工作)大约一分钟,然后它返回一个空字符串作为响应。我已经为此工作了一天多,似乎无法使它工作。此外,映像从未实际上载到服务器(我选中)。提前谢谢 // Uploading the image -(IBAction)uploadImage:(id)sender{ [self uploadImage:UIImageJPEGRepresenta

我试图上传一个图像到我的php服务器,但它不工作。当我试图上传文件时,屏幕只是冻结(这是预期的-我使用sendSynchronousRequest,直到我可以让它工作)大约一分钟,然后它返回一个空字符串作为响应。我已经为此工作了一天多,似乎无法使它工作。此外,映像从未实际上载到服务器(我选中)。提前谢谢

// Uploading the image
-(IBAction)uploadImage:(id)sender{
[self uploadImage:UIImageJPEGRepresentation(productImageView.image, 90)    filename:@"image.jpg"];
}

- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{

// This isn't actually my url btw
NSString *urlString = @"http://www.mysite.com/uploadImage.php";


// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type

 You might want to generate a random boundary.. this is just the same
 as my output from wireshark on a valid html post
 */
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"rn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

// 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];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Responce"
                                                message:returnString
                                               delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];
return ([returnString isEqualToString:@"OK"]);
}
和我的php文件:

<?php
$uploaddir = ''; // Put in same directory as PHP file for debugging purposes
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "Your file is called {$file}";
}

?>


这肯定有效。

您错过了CRLF字符的转义,例如

[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] 

完整修复:

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // note: the preceding "\r\n" may be problematic if the server does not properly implement the "preamble" rule as specified in RFC 2046
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

@代码101我可以知道您从服务器得到的响应吗
/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // note: the preceding "\r\n" may be problematic if the server does not properly implement the "preamble" rule as specified in RFC 2046
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];