iOS上传图片php + mysql

时间:2014-11-29 20:02:33

标签: php ios mysql

我有一个问题。我在互联网上搜索它,但找不到一个好的答案。

我喜欢将图像上传到mysql表,一个longblob字段。我也必须在其中获得一些帖子数据。 (用户名和传递和东西)。

我想从xCode中的iOS应用程序执行此操作。那么有没有人有我必须在iOS端和服务器(php)端实现的代码?

2 个答案:

答案 0 :(得分:0)

所以,我现在有了这个代码,但它似乎没有起作用。

<?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执行请求发送和处理响应。但作为回应我得到:&#34;没有选择/上传图像&#34;

答案 1 :(得分:0)

这是我解决问题的方法

Objective-C代码:

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 = @"\r\n";

// 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)

PHP代码:     //将其更改为图像必须具有的路径     $ uploadfile =&#34;新图片的路径&#34; 。 &#34; png格式| .JPG | ...&#34 ;;

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

}