iOS上传图片php + mysql

I got a question. I searched for it on the internet, but couldn't find a good answer.

I like to upload an image to a mysql table, a longblob field. I have to get some post data in it also. (Username and pass and stuff).

I want to do this from an iOS app in xCode. So does anyone have the code I have to implement on the iOS side and on the server (php) side?

So, I have this code now, but it doesn't seem to be working.

<?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.
";
    } else {
        echo "Possible file upload attack!
";
    }


} else {
    print "No image selected/uploaded";
}


?>

for my php file. And for my xcode upload:

-(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];
}

And the contactsyncobject does the request sending and handling the response. But as response I get: "No image selected/uploaded"

This is my solution for my problem

The Objective-C code:

NSString *email = self.fileController.user.email;
NSString *password = self.fileController.user.password;
UIImage *image = [[UIImage alloc] initWithData:contact.image];
if(image == nil){
    //UNKNOWN IMAGE
    //image = [UIImage imageNamed:@"unknown.png"];
    return;
}

//The $_POST parameters you want to add
NSDictionary *params = @{ @"email": email, @"contact": contact.userid, @"password": password };
//The image to data
NSData *imageData = UIImagePNGRepresentation(image);
//your url
NSString *urlString = @"http://spark-app.freeiz.com/addImage.php";


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *kNewLine = @"
";

// Note that setValue is used so as to override any existing Content-Type header.
// addValue appends to the Content-Type header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

// Add the parameters from the dictionary to the request body
for (NSString *name in params.allKeys) {
    NSData *value = [[NSString stringWithFormat:@"%@", params[name]] dataUsingEncoding:NSUTF8StringEncoding];

    [body appendData:[[NSString stringWithFormat:@"--%@%@", boundary, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", name] dataUsingEncoding:NSUTF8StringEncoding]];
    // For simple data types, such as text or numbers, there's no need to set the content type
    [body appendData:[[NSString stringWithFormat:@"%@%@", kNewLine, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:value];
    [body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];
}

// Add the image to the request body
[body appendData:[[NSString stringWithFormat:@"--%@%@", boundary, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"myPngFile.png\"%@", @"image", kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@%@", kNewLine, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];

// Add the terminating boundary marker to signal that we're at the end of the request body
[body appendData:[[NSString stringWithFormat:@"--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];
//Now start the request (asynchronous or synchronous)

The PHP code: //change this to the path the image has to have $uploadfile = "path to new image" . ".png|.jpg|...";

// 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.
";

    } else {

        echo "Possible file upload attack!
";

    }





} else {

    print "No image selected/uploaded";

}