简体   繁体   English

将图片从iOS应用发送到php脚本到mySQL DB

[英]Send a picture from iOS app to php script to mySQL DB

I've a problem: I want to send a picture from my iOS app to a php script to insert this image in a mysql db. 我有一个问题:我想将图片从我的iOS应用发送到php脚本,以将该图像插入到mysql数据库中。 The field in mysql db for the image is LONGBLOB. 图像的mysql db中的字段为LONGBLOB。 The image that is sending is _photoImageView.image The method I created in Xcode is as follows: 正在发送的图像是_photoImageView.image我在Xcode中创建的方法如下:

NSData *dataForImage = UIImagePNGRepresentation(_photoImageView.image);


    NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://localhost/myfff/join.php?username=%@&password=%@&photo=%@",_txtUsername.text,_txtPassword.text, dataForImage];
[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strURL]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

In the php script in the affected part, I do this: 在受影响部分的php脚本中,我这样做:

$data = addslashes(fread(fopen($_FILES[photo], "rb"))); $query = "INSERT INTO mytb VALUES (' ','$username','$password'','$data')";

The insertion does not occur .. where am I wrong? 插入不会发生..我在哪里错? Help me please! 请帮帮我!

If you are getting other posted data on your php script ie username & password then the issue is that you will not get data in $_FILES array since the Xcode you share is not uploading the data as form (multipart/form-data) instead it's posting the data (i believe), so i think you will be getting raw image data in $_POST['photo'] and query should be: 如果您正在php脚本上获取其他已发布的数据(即用户名和密码),那么问题在于您不会在$_FILES数组中获取数据,因为共享的Xcode并未将数据上传为表单(multipart / form-data),而是发布数据(我相信),所以我认为您将在$_POST['photo']获取原始图像数据,查询应为:

$data = $_POST['photo'];

$query = "INSERT INTO mytb VALUES (' ','$username','$password'','$data')";

Even if you get the photo in $_FILES then still your using wrong thing to fread it should be: 即使您将照片保存在$_FILES您仍然使用错误的东西来fread照片:

$data = addslashes(fread(fopen($_FILES['photo']['tmp_name'], "rb")));

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

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