图像没有使用php web服务上传到服务器

时间:2017-11-09 06:44:46

标签: php ios mysql objective-c

图片未上传到服务器 这是我的代码

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage * chosenImage = [info valueForKey: UIImagePickerControllerOriginalImage];

[picker dismissViewControllerAnimated:YES completion:NULL];

[_userImage setImage:chosenImage];





NSData *imageData = UIImageJPEGRepresentation(_userImage.image,0.2);

NSString * string =[imageData base64EncodedStringWithOptions:kNilOptions];

if (imageData != nil)
{

    NSString *filename = @"imageoooo";

    NSString * requestURL = @"http://localhost/webservices/myfile.php";

    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:requestURL]];

    [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=\"IdLabel\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[_IdLabel.text dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[NSData dataWithData:imageData]];


[request setHTTPBody:body];

// set the content-length
NSString * postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
//    [request setURL:requestURL];


[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
        ^(NSData *  data, NSURLResponse *  response, NSError *  error)
      {

          NSLog(@"%@",data);

        NSString * myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"Request reply: %@", myString);


dispatch_async(dispatch_get_main_queue(), ^{


    NSDictionary * items = [NSJSONSerialization JSONObjectWithData:data
                                                                      options:0
                                                                        error:nil];


    NSString * successstr =[NSString stringWithFormat:@"%@", [items valueForKey:@"success"]];

                NSLog(@"%@", successstr);
              });
          }]
        resume];

}

和我的PHP代码

<?php


require_once("config.php");

$response = array();
echo "<br>".$uploadDirectory = "http://localhost/makeintern/profile";

echo "<br>".$_POST["IdLabel"];

echo "<br>".$_FILES['photo']['name'];

if($_POST["IdLabel"] && $_POST["photo"]){


echo "<br>".$uploadDirectory = "http://localhost/makeintern/profile";

    $errors = []; 


    $fileExtensions = ['jpeg','jpg','png']; 


    echo "<br>".$fileName = $_FILES['photo']['name'];

    echo "<br>".$fileSize = $_FILES['photo']['size'];

    echo "<br>".$fileTmpName  = $_FILES['photo']['tmp_name'];

    echo "<br>".$fileType = $_FILES['photo']['type'];

    echo "<br>".$fileExtension = strtolower(end(explode('.',$fileName)));

}

echo json_encode($response);


return json_encode($response);

mysqli_close($con);

?>

错误:

  

未定义的索引: /Applications/XAMPP/xamppfiles/htdocs/webservices/test-file.php 中的照片 13

提前致谢

0 个答案:

没有答案
相关问题