OGeek|极客世界-中国程序员成长平台

标题: php - 如果照片上传成功 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:32
标题: php - 如果照片上传成功

我希望能够将照片上传到我的网站,并在完成后将其定位到不同的 View Controller 。如果它执行 xyz 有错误:

喜欢:

if (!error) {
   [self performSegueWithIdentifier"tocity&country" sender:self];
}
else
{
   NSError *error = [request error];
}

我如何上传照片:

所以 objective-c 代码:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed"image.jpg"]);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL
                                                 URLWithString"http://******.co.uk/****/imageupload.php"]
                                    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                    timeoutInterval:20.0];
    [request setHTTPMethod"OST"];
    [request setValue"image/jpg"
   forHTTPHeaderField"Content-type"];
    [request setValue:[NSString stringWithFormat"%lu",
                       (unsigned long)[imageData length]]
   forHTTPHeaderField"Content-length"];
    [request setHTTPBody:[self imageDataToSend]];
    
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if( theConnection )
    {
        [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
    
    [theConnection release];

imageupload.php:

<?php
$handle = fopen("image.jpg", "wb"); // write binary
 
fwrite($handle, $HTTP_RAW_POST_DATA);
 
fclose($handle);
 
print "Received image file.";
?>

我也注意到iphone的屏幕顶部没有这样的加载gif,上传图片时怎么来的?:

enter image description here


编辑:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString"http://******.co.uk/****/imageupload.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

//where does the http code come in?

NSURL *filePath = [NSURL fileURLWithPath"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Success: %@ %@", response, responseObject);
    }
}];
[uploadTask resume];



Best Answer-推荐答案


PHP

始终添加检查以确保您确实得到了一些东西,

您可以使用is_uploaded_file试试这样:

<?php 
if(is_uploaded_file($_FILES['image']['tmp_name'])){ 
    $folder = "uploads/"; 
    $file = basename( $_FILES['image']['name']); 
    $full_path = $folder.$file; 
    if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) { 
        echo '{"success":true, "msg": "succesful upload, we have an image!"}'; 
    } else { 
        echo '{"success":false, "msg": "upload received! but process failed"}'; 
    } 
}else{ 
    echo '{"success":false, "msg": "upload failure ! Nothing was uploaded"}'; 
} 
?>

Objective-C

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"image.jpg"]);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                initWithURL:[NSURL
                    URLWithString:@"http://******.co.uk/****/imageupload.php"]
                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                timeoutInterval:20.0];
[request setHTTPMethod:@"OST"];
[request setValue:@"image/jpg" forHTTPHeaderField:@"Content-type"];
[request setValue:[NSString stringWithFormat:@"%lu",
                   (unsigned long)[imageData length]]
forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[self imageDataToSend]];
//Send the Request
NSData* returnData = [NSURLConnection sendSynchronousRequest: request 
                                           returningResponse: nil error: nil];
//serialize to JSON                                          
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];

//parsing JSON
bool success = [result[@"success"] boolValue];
if(success){
    NSLog(@"Success=%@",result[@"msg"]);
}else{
    NSLog(@"Fail=%@"result[@"msg"]);
}

关于php - 如果照片上传成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23256387/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4