douwu7168 2015-07-10 23:26
浏览 36

通过php和AFNetworking从iOS上传图片到mySQL

I have been looking everywhere online for a way to upload an image from an iOS app via PHP using AFNetworking 2.0 to a mySQL database. I have finally found a solution after searching and searching. I can take a picture and upload it to my database. When I download the image it saves as "Locations-29.image" or "Locations-30.image" I just don't get why the file extension is .image? When the image saves to the database it shows up like this: /var/lib/php/session/phpA7fFDB

I would really appreciate any help. Is it something to do with the Xcode? My question really is how can I save it as a .jpeg/png file rather than what I'm getting now? and is it okay for the URL to be /var/lib/php/session/phpA7fFDB ?

Here is my code:

- (IBAction)postImageToServer:(UIButton*)sender {

CGFloat compression = 1.0f;
UIImage *image = _uploadImage.image;

NSData *imageData1 = UIImageJPEGRepresentation(image, compression);   
int random = arc4random() % 9999999;
NSString *photoName=[NSString stringWithFormat:@"%d-image.jpg",random];

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]     initWithBaseURL:[NSURL URLWithString:@"http://urlhere.com"]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"fileName": photoName};
AFHTTPRequestOperation *op = [manager POST:@"getImage.php?" parameters:parameters     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData1 name:@".jpg" fileName:photoName     mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
}

This is my PHP file:

<?php
$hostusername = "";
$hostpassword = "";
$hostname = ""; 

$name = $_POST['name'];
$image1 = basename($_FILES['image']['tmp_name']);
$tmp_img = $_FILES['image']['tmp_name'];

$dbhandle = mysql_connect($hostname, $hostusername, $hostpassword) 
 or die("Unable to connect to MySQL");

$selected = mysql_select_db("em",$dbhandle) 
or die("Could not select userName");

$query = mysql_query("INSERT INTO Locations (name, image)
        VALUES ('$name','$tmp_img')");

mysql_close($dbhandle);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog