【问题标题】:iOS upload image php + mysqliOS上传图片php+mysql
【发布时间】:2014-12-01 03:43:48
【问题描述】:

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

我喜欢上传一张图片到一个 mysql 表,一个 longblob 字段。我还必须在其中获取一些帖子数据。 (用户名和通行证等)。

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

【问题讨论】:

  • 使用标准 html(5) 将适用于所有主要的弓箭手和 iOS。唯一可以具体说明的是 css 可以在移动设备上很好地显示内容(也称为响应式),请参阅此处 codepool.biz/tech-frontier/html5/… 以及一些额外的内容,例如 允许直接从手机的相机上传
  • 是的,但我想在应用程序中进行,我将其添加到问题中
  • 在 html 中运行您的应用程序;)该应用程序需要做什么? iOS 应用程序可以完全访问 sockets/http 协议,但是让事情变得比简单的 html 更复杂。
  • html 模式是不可能的 :),它必须在 xCode 中
  • 看看这里stackoverflow.com/questions/3566516/… 顺便说一句,5 秒的谷歌搜索会得到相同的结果,使用术语“post request cocoa”;)

标签: php ios mysql


【解决方案1】:

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

<?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 执行请求发送和处理响应。但作为回应,我得到:“未选择/上传图片”

【讨论】:

  • 看起来上传的图片是用 ascii 编码的 $_post['image'] 而不是作为上传的文件处理的(就像使用 html 表单上传的文件一样),你必须调整 php 代码从 $_post['image'] 而不是从 $_FILE 引用创建文件。无法告诉您如何恢复 NSASCIIStringEncoding。另一种选择是找到一种方法来添加标题 contentType: "application/x-www-form-urlencoded" 然后我猜 php 将创建 $_FILE 引用
  • 非常感谢!!它进行了一些调整!
  • 现在发布您的工作代码可能会很好,以便其他人可以找到它:)
【解决方案2】:

这是我的问题的解决方案

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 = "新图像的路径" . ".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.\n";

    } else {

        echo "Possible file upload attack!\n";

    }





} else {

    print "No image selected/uploaded";

}

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 2012-07-19
    • 2013-03-27
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 2015-01-17
    • 2013-03-02
    相关资源
    最近更新 更多