如何从php数据库帖子中获取URL?

时间:2014-02-23 13:51:46

标签: php ios nsurl

我有一个iOS应用程序,它将图像发布到php页面,将其保存到数据库中。

iOS代码如下所示:

- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{

    NSLog(@"uploading");

    NSString *urlString = @"http://www.myserver.com/appsapce/assets/uploadPics.php";

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

    NSString *boundary = @"0xKhTmLbOuNdArY";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //Set the filename
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //append the image data
    [body appendData:[NSData dataWithData:imageData]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

    NSLog(@"returningOKString");

    return ([returnString isEqualToString:@"OK"]);
}

并且php看起来像这样:

<?php
$uploaddir = 'photos/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK";
} else {
    echo "ERROR";
}

?>

如何将已发布照片的服务器网址返回到我的iOS应用程序?

THX

1 个答案:

答案 0 :(得分:0)

而不是echo "OK";使用echo $uploadfile;

此外,我发现您只从iOS传递文件名basename($_FILES['userfile']['name'])

如果你知道iOS中的文件名,只需检查returnString。如果是OK,则表示路径如下。

www.xyz.com/photos/fileNameIPassed

此处fileNameIPassed = basename($_FILES['userfile']['name'])