gpt4 book ai didi

php - 如果照片上传成功

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:06 24 4
gpt4 key购买 nike

我希望能够将照片上传到我的网站,并在完成后定位到不同的 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:@"POST"];
[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屏幕上方没有loading 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];

最佳答案

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:@"POST"];
[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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com