使用ios将图像上传到php


Uploading Images using ios to php

我试图从Iphone图库上传的图像都保存为ipodfile.jpg

我想要从图库中选择的确切图像名称我想创建一个文件夹(使用登录用户名),并将其存储在文件夹中

例如

  • 用户名:Mike
  • 图片:welcome.jpg

然后我想创建一个名为mike的文件夹,并将图像保存在里面。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissModalViewControllerAnimated:true];
    img = [info valueForKey:UIImagePickerControllerOriginalImage];
    //images.image = img; 
    NSData *imageData = UIImageJPEGRepresentation(img, 90);
    NSString *urlString = @"http://192.168.1.92/Abdul/IOS/Chat/upload_file.php";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449"
    ;
    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]];
    [body appendData:[@"Content-Disposition: form-data; name='"file'"; filename='"ipodfile.jpg'"'r'n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream'r'n'r'n" dataUsingEncoding:NSUTF8StringEncoding]];
    [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];
    NSLog(@"Successfully uploaded");
}

这是我使用的服务器端代码

<?php
$oname=$_FILES["file"]["name"];
$tempname=$_FILES["file"]["tmp_name"];
$path="upload";
echo "Upload: " . $oname . "<br />";
echo "Temp file: " . $tempname . "<br />";

echo "<br>";
$ex=file_exists("upload/$oname");
if($ex)
{
    echo "File Already exist";
}
else
{
    $upload=move_uploaded_file($tempname,$path.$oname);
    if($upload)
    {
        echo "Sucessfully Stored in: " . $path. $oname;
    }
    else
    {
    echo "Not stored Sucessfully : " . $path.$oname;
    }
}

您应该为每个图像取唯一的文件名,比如时间戳,它将始终是唯一的。

在下面的行中,您正在设置图像名称,所以设置它的时间戳。

 [body appendData:[@"Content-Disposition: form-data; name='"file'"; filename='"ipodfile.jpg'"'r'n" dataUsingEncoding:NSUTF8StringEncoding]];

你可以用我用过一次的唯一的名字,

            NSDate *currentDate = [[NSDate alloc] init];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
            NSString *localDateString = [dateFormatter stringFromDate:currentDate];
            NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""];
            NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",i];
            NSString *finalUniqueImageNAme = [cleanedString2 stringByAppendingString:@".jpg"];

希望这会有所帮助;)

更新:

你可以使用这样的唯一名称,

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name='"file'"; filename='"%@'"'r'n",finalUniqueImageNAme] dataUsingEncoding:NSUTF8StringEncoding]];

第二件事,你应该使用AFNetworking来做这类事情。它可以管理所有这些东西,比如设置边界等。点击这里查看AFNetworking github

更新2(根据评论查询):

您应该使用NSURLSession而不是nsurlconnection,因为现在不推荐使用连接,所以您的代码应该是这样的

 NSURLSession *session = [NSURLSession sharedSession];

[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (data) {
        //if response is in string format
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        //if response in json format then
        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    }

}]resume];

更改的路径

CCD_ 6,然后上传图像。通过这种方式,你的图像将被上传到迈克文件夹中。

    NSData *imageData = UIImagePNGRepresentation(yourImage);
    NSString *urlString = [ NSString stringWithFormat:@"http://192.168.1.92/Abdul/IOS/Chat/upload_file.php"];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    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]];
    [[body appendData:[@"Content-Disposition: form-data; name='"file'"; filename='"%@'"'r'n",img] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream'r'n'r'n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"'r'n--%@--'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];