IOS:将图像上传到服务器Php从ios设备不工作?
问题描述:
我用这个代码的设备:IOS:将图像上传到服务器Php从ios设备不工作?
NSURL *url = [NSURL URLWithString:@"http://localhost/phd-admin//testing.php"];
NSData *imageData=[NSData dataWithContentsOfURL:url];
;
NSLog(@"data length %d",[imageData length]);
// UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
if (imageData != nil)
{
NSString *filenames = [NSString stringWithFormat:@"Deal.png"]; //set name here
NSLog(@"%@", filenames);
NSString *urlString = @"http://localhost/php-admin//receiveFile.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------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 stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"Deal.png" 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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSLog(@"requereting %@",request);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
NSLog(@"response %@",returnString);
NSLog(@"finish");
我用这个代码用于服务器端:
<?php
$myparam = $_POST['userfile']; //getting image Here
$mytextLabel= $_POST['filenames'] ; //getting textLabe Here
echo $myparam;
echo $mytextLabel;
$target_path = "upload/";
$target_path = $target_path . basename($_FILES['myfile']['name']);
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['myfile']['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!".$myparam."val".$mytextLabel;
}
?>
,但它不工作?
请提出您的答案。 谢谢,s。
答
我相信你有太多的线路,并不需要这一切。尝试只是这不是所有的appendData
:
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filenames] 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]];
你PHP:
<?php
$file = $_FILES['userfile'];
$target_path = "upload/" . $label;
if (move_uploaded_file($file['tmp_name'], $target_path)) {
echo "The file ". basename($file['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!" . var_dump($file);
}
?>
尝试这种方式。
请发布您遇到的错误。 –
没有错误,但所有的PHP回声将是空的。 –
PHP中的Var_dump($ _ POST)和var_dump($ _ FILES ['userfile']) –