编程语言
首页 > 编程语言> > php-无法将图像上传到服务器ios

php-无法将图像上传到服务器ios

作者:互联网

您好,我正在尝试将图像从我的IOS设备上传到服务器.

这是我上传图片的代码

- (IBAction)btnUpload:(id)sender {

    if (self.imageViewGallery.image == nil) {
        UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Wait"
                                                             message:@"Please Select An Image To Upload." delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil, nil];
        [ErrorAlert show];
        NSLog(@"error");
    }
    else{
    NSData *imageData = UIImageJPEGRepresentation(self.imageViewGallery.image, 90);
    NSString *urlString = @"http://localhost/ColorPicker/api.upload.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"];
        NSString *imgName = LIbraryImage;
        NSLog(@"image name : %@",imgName);

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

        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@\"\r\n", imgName]] 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]];

    [request setHTTPBody:body];
        NSLog(@"setRequest : %@", request);


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnstring is : %@",returnString);
    }

现在,这是我的服务器端代码.有点长

  <?php

$Image = $_FILES['files']['name'];
    print_r($_FILES);
foreach ($Image as $f => $name) {

    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $name);
    $extension = end($temp);

    if ((($_FILES['files']['type'][$f] == "image/gif") || ($_FILES['files']['type'][$f] == "image/jpeg") || ($_FILES['files']['type'][$f] == "image/jpg") || ($_FILES['files']['type'][$f] == "image/png")) && ($_FILES['files']['size'][$f] < 2000000) && in_array($extension, $allowedExts)) {
        if ($_FILES['files']['error'][$f] > 0) {
            echo "Return Code: " . $_FILES['files']['error'][$f] . "<br>";
        } else {

            if (file_exists("upload/" . $name)) {

            } else {
                move_uploaded_file($_FILES['files']['tmp_name'][$f], 'upload/' . uniqid() . "_" . $name);
            }
        }
    } else {
        $error = "Invalid file";
    }

}
?>

现在,每次我尝试上传时发生的情况都得到了此警告

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/Applications/XAMPP/xamppfiles/htdocs/ColorPicker/uploadAction.php</b> on line <b>5</b><br />

解决方法:

分段上传图像数据时,结束于$_FILES.自PHP 4.3.0起,$_ REQUEST不再包含有关以这种方式上传的文件的任何信息.

您其余的代码基于以下想法:不以多部分形式上传图像,而是以标准格式以base-64字符串形式上传图像.无需将base 64数据写入文件,您只需要从$_FILES获取有关文件的信息,然后将其移动到目标目录为move_uploaded_file.

标签:file-upload,objective-c,ios,php,image-uploading
来源: https://codeday.me/bug/20191122/2058695.html