Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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
从iPhone向PHP上传图像_Php_Iphone_Ios_Objective C - Fatal编程技术网

从iPhone向PHP上传图像

从iPhone向PHP上传图像,php,iphone,ios,objective-c,Php,Iphone,Ios,Objective C,我正在将一个图像从iPhone上传到PHP,但在PHP中该图像不可见。图像正在以字节为单位保存 -(void)uploadMyImage:(UIImage *)img { NSData *data = UIImageJPEGRepresentation(img, 90); NSMutableString *urlString = [[NSMutableString alloc] init]; [Base64 initialize];

我正在将一个图像从iPhone上传到PHP,但在PHP中该图像不可见。图像正在以字节为单位保存

  -(void)uploadMyImage:(UIImage *)img
    {
        NSData *data = UIImageJPEGRepresentation(img, 90);
        NSMutableString *urlString = [[NSMutableString alloc] init];
        [Base64 initialize];
        [urlString appendFormat:@"image=%@",[Base64 encode:data]];
          NSString *baseurl = @"http://projectleads.info/spirits/images/imageupload1.php"  ;
        NSData *postData = [urlString dataUsingEncoding:NSUTF8StringEncoding];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        NSURL *url = [NSURL URLWithString:baseurl];
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
        [urlRequest setHTTPMethod: @"POST"];
        [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [urlRequest setValue:@"application/x-www-form-urlencoded"
          forHTTPHeaderField:@"Content-Type"];
        [urlRequest setHTTPBody:postData];
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
        [connection start];
        }

我的php代码是

<?php
    $base=$_REQUEST['image'];
     if(empty($base))
    {
        echo 'No data';
    }
    else
    {
    echo $base;
    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');
        // print($binary);
    //$theFile = base64_decode($image_data);
    $dbser='localhost';  
    $dbuser='spirit';
    $dbpwd='123456';
    $dbname='spirit';
       $db= mysql_connect($dbser,$dbuser,$dbpwd);
     $dbcon=mysql_select_db($dbname);
     function generate_chars()
    {
        $num_chars = 4; //max length of random chars
        $i = 0;
        $my_keys = "123456789abcdefghijklmnopqrstuvwxyz"; //keys to be chosen from
        $keys_length = strlen($my_keys);
        $url  = "";
        while($i<$num_chars)
        {
            $rand_num = mt_rand(1, $keys_length-1);
            $url .= $my_keys[$rand_num];
            $i++;
        }
        return $url;
    }

     function isUnique($chars)
    {
        //check the uniqueness of the chars
        global $link;
        $q = "SELECT * FROM `url` WHERE `randname`='".$chars."'";
        $r = mysql_query($q, $link);
        //echo mysql_num_rows($r); die();
        if( mysql_num_rows($r)>0 ): 
            return false;
        else: 
            return true;
        endif;
    }

     $chars = generate_chars();
     while( !isUnique($chars) )
     {
            $chars = generate_chars();
     }
    $query=mysql_query("insert into url (randname) values ('".$chars."')");
    $test=$chars.".jpg";
    $file = fopen($test, 'wb');
    fwrite($file, $binary);
    fclose($file);
    echo 'success';
    echo '<img src=test.jpg>';
    }

?>

适用于iPhone:

检查这里

还有这个

以及使用Base64编码将UIImage转换为字符串


我希望这将帮助您

您的PHP代码很好,问题在于Base64编码。我已经成功上传了你的代码图像,请执行以下操作以正确编码数据

在代码中复制并粘贴此方法(取自)

按如下所示追加编码字符串

[urlString appendFormat:@"image=%@", encodedString];

最后一个字符串响应的格式是base 64,图像也保存在服务器中,但它不可见。将base 64字符串解码为UIImage它会提供与您上载的图像相同的图像我想不,我没有解码base 64字符串到UIImage字符串响应来自服务器我认为您的客户端编码现在工作正常。服务器端有问题。他们收到了什么响应??
- (NSString*)base64forData:(NSData*) theData {
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
NSString *encodedString = [[self base64forData:data] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
[urlString appendFormat:@"image=%@", encodedString];