我有一个问题:我想将一张图片从我的 iOS 应用程序发送到 php 脚本,以将该图像插入到 mysql 数据库中。 mysql 数据库中图像的字段是 LONGBLOB。发送的图像是 _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];
在受影响部分的 php 脚本中,我这样做:
$data = addslashes(fread(fopen($_FILES[photo], "rb"))); $query = "INSERT INTO mytb VALUES (' ','$username','$password'','$data')";
插入没有发生..我哪里错了?请帮助我!
最佳答案
如果您在 php 脚本上获取其他发布的数据,即用户名和密码,那么问题是您将无法在 $_FILES
数组中获取数据,因为您共享的 Xcode 不会将数据上传为表单(multipart/form-data)而不是发布数据(我相信),所以我认为您将在 $_POST['photo']
中获取原始图像数据,查询应该是:
$data = $_POST['照片'];
$query = "INSERT INTO mytb VALUES (' ','$username','$password'','$data')";
即使您在$_FILES
中获取照片,那么您仍然使用错误的东西fread
它应该是:
$data = addslashes(fread(fopen($_FILES['photo']['tmp_name'], "rb")));
关于php - 将图片从 iOS 应用程序发送到 php 脚本到 mySQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17569306/