我有一个问题。我在互联网上搜索过,但没有找到好的答案。
我喜欢上传一张图片到mysql表,一个longblob字段。我还必须在其中获取一些帖子数据。 (用户名和通行证等等)。
我想通过 xCode 中的 iOS 应用程序执行此操作。那么有人有我必须在 iOS 端和服务器 (php) 端实现的代码吗?
最佳答案
所以,我现在有了这段代码,但它似乎不起作用。
<?php
require_once("Database.php");
$con = getConnection();
session_start();
$email = $_POST['email'];
$contact = $_POST['contactID'];
$password = $_POST['password'];
$email = stripslashes($email);
$contact = stripslashes($contact);
$password = stripslashes($password);
$password = md5($password);
$sql="SELECT ID FROM User WHERE email='$email' AND password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
//echo $id ." <br/>";
//echo $count;
if($count==1) {
$row = mysql_fetch_object($result);
$id = $row->ID;
}
// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
// Temporary file name stored on the server
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
} else {
print "No image selected/uploaded";
}
?>
我的 php 文件。对于我的 xcode 上传:
-(void) uploadImage: (SparkContact *) contact
{
NSString *type = @"upload";
NSString *email = self.fileController.user.email;
NSString *password = self.fileController.user.password;
UIImage *image = [[UIImage alloc] initWithData:contact.image];
NSData *data = UIImagePNGRepresentation(image);
NSString *post =
[[NSString alloc] initWithFormat:@"&email=%@&password=%@&contactID=%@&image=%@",email,password,contact.userid,data];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", (int)[postData length]];
NSURL *url = [NSURL URLWithString:@"http://spark-app.freeiz.com/addImage.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
ContactSyncObject *obj = [[ContactSyncObject alloc] initWithType:type withContact:contact];
obj.fileController = self.fileController;
[obj startSync:theRequest];
}
contactsyncobject 负责发送请求和处理响应。但作为回应,我得到:“没有选择/上传图像”
关于php - iOS上传图片php + mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206520/