• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php - 如果照片上传成功

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:32:25 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我希望能够将照片上传到我的网站,并在完成后将其定位到不同的 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap