繁体   English   中英

如何通过iOS应用将图像插入远程数据库?

[英]How to insert image into remote database by iOS app?

在我的iOS应用中,我必须将图像存储在远程数据库中。 我已经通过BLOB做到了,但是我只想通过存储其路径来做到这一点。 我已经尝试了很多,但是我没有编写客户端代码。

客户端代码:

NSData *imageData = UIImageJPEGRepresentation(self.imgview.image,0.2);     //change Image to NSData

if (imageData != nil)
{
    NSString *filenames = @"catttt.jpeg";      //set name here
    NSLog(@"%@", filenames);
    NSString *urlString = @"http://localhost/insertIntoImage.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=\".jpeg\"\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]];
    // 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];
    NSLog(@"%@",returnString);
    NSLog(@"finish");
}

远端代码:

    $target_path = "uploads/";
    $target_file = $target_path . basename( $_FILES['userfile']['filenames']);

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_file)) {
        echo "The file ". basename( $_FILES['userfile']['filenames']) . " has been uploaded";
    } else {
        echo "There was an error uploading the file, please try again!";
    }
?>

首先将图像上传到服务器,然后将图像名称/路径更新到数据库

下面是将图像存储到服务器的功能

这里$ image是来自IOS APP的图像数据

$image = $_POST['image_name_from_app'];
$image = str_replace("<", "", $image);
$image = str_replace(">", "", $image);
$image = str_replace("&lt;", "", $image);
$image = str_replace("&gt;", "", $image);
$image = preg_replace('/[\s\W]+/', '', $image);
$bin = hex2bin($image);
$path = "user_images/";
$imageName = time() .".png";
$path = "user_images/";
if (file_put_contents($path . $imageName, $bin)) {
  return $imageName;
}
return false;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM