iOS上传图像php+;mysql

iOS上传图像php+;mysql,php,ios,mysql,Php,Ios,Mysql,我有个问题。我在网上搜索了一下,但没有找到一个好的答案 我喜欢将图像上传到mysql表,一个longblob字段。我还得在里面弄些帖子资料。(用户名、通行证等) 我想从xCode中的iOS应用程序执行此操作。那么,有人有我必须在iOS端和服务器(php)端实现的代码吗?所以,我现在有了这些代码,但它似乎不起作用 <?php require_once("Database.php"); $con = getConnection(); session_start(); $email = $_P

我有个问题。我在网上搜索了一下,但没有找到一个好的答案

我喜欢将图像上传到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";
}


?>

contactsyncobject执行请求发送和处理响应。但我得到的回答是:“没有选择/上传图像”

这是我解决问题的方法

目标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=“新图像的路径”。“.png |.jpg |…”


使用标准html(5)将适用于所有主要的Bowser和iOS。唯一具体的是css,它可以在移动设备上很好地显示内容(也称为响应式),请参见此处,还有一些额外的类似功能,可以直接从手机的摄像头上传,但我想在应用程序中实现这一点,我会将其添加到问题中(在html中运行你的应用程序;)应用程序必须做什么?iOS应用程序可以完全访问sockets/http协议,但会使事情变得比简单的html更复杂。html模式不可能:),它必须在Xcode中。顺便说一句,谷歌搜索5秒钟也会得到同样的结果,使用术语“请求后可可”;)看起来上传的图像是用ascii编码的$_post['image'],而不是作为上传文件处理的(就像用html表单上传文件一样),您必须调整php代码以从$_post['image']而不是从$_文件引用创建文件。无法告诉您如何恢复NSASCIIStringEncoding。另一个选项是找到一种添加头contentType的方法:“application/x-www-form-urlencoded”,然后我猜php将创建$\u文件引用程序。这可以帮助您非常感谢!!它的工作与一些调整!现在发布您的工作代码以便其他人可以找到它可能会很好:)
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)
// 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";

}