Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用AFN网络上传图像_Php_Objective C_Upload_Afnetworking - Fatal编程技术网

Php 使用AFN网络上传图像

Php 使用AFN网络上传图像,php,objective-c,upload,afnetworking,Php,Objective C,Upload,Afnetworking,我已经搜索了很多工作上传代码使用AFNetwork,但没有运气。我从两个不同的网站获得了这个代码: NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]); NSURL *url = [NSURL URLWithString:@"http://localhost/project"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBa

我已经搜索了很多工作上传代码使用AFNetwork,但没有运气。我从两个不同的网站获得了这个代码:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]);
NSURL *url = [NSURL URLWithString:@"http://localhost/project"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"google" fileName:@"1.png" mimeType:@"image/png"];
}];


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
} failure:nil];

[operation start];

我真的很感谢你的帮助。

我几天前刚做了这个。iPhone端的代码(它上载一个图像和另一个文本参数item)


不确定它是否没有错误,因为它是从较长的源代码中获取的,所以我修改了一些代码,这是几天前我刚刚做的。iPhone端的代码(它上载一个图像和另一个文本参数item)


不确定它是否没有错误,因为它来自更长的源代码,所以我修改了一些代码

查看我对类似问题的回答。我使用这个自定义类(由我编写)(用于我的所有远程web服务操作。它足够灵活,允许将其他POST参数与图像文件数据一起发送。processFileUploadRequestWithURL方法是您需要的方法,其他方法您可以使用,也可以不使用


查看我对一个类似问题的回答。我使用这个自定义类(由我编写)(用于我的所有远程web服务操作。它足够灵活,允许将附加的POST参数与图像文件数据一起发送。processFileUploadRequestWithURL方法是您需要的方法,其他方法您可以使用,也可以不使用


我在某个地方读到SBJason已被删除。对吗?我在某个地方读到SBJason已被删除。对吗?Thanx对于代码,只有一件事,当我使用html browse测试我的php代码时,我有一个问题:我的图像名为1.png。我在使用这些设置itemData和dict时遇到问题。你能帮我一下吗?你很简单吗您必须在NSData中转换UIImage。您可以使用self.itemData=UIImagePNGRepresentation(yourUIImageInstance);在我的代码中self.itemData是一个属性类型NSData。你能再解释一下吗?我现在不明白你的问题。如果你想用浏览器检查你的php代码,你必须用一个表单和一个输入类型文件(你发布的那行)创建一个简单的页面。表单方法是post,操作是php接收者页面的地址。url.com/upload.php“>然后只需选择文件并上载。对此,我用浏览器检查了它,并确认了。现在我想知道如何用您的变量替换我的变量。如以下代码所示:NSDictionary*dict=[NSDictionary Dictionary WithObjects:@[self.itemValue]forKeys:@[@“item”]];我想知道self.itemValue和item是什么,以便可以替换它们。对于:[formData appendPartWithFileData:self.itemData名称:@“imageData”文件名:@“imageData.jpg”mimeType:@“image/png”];Thanx对于代码,只有一件事,当我使用html browse测试我的php代码时,我有这样一个问题:我的图像名称是1.png。我在使用这些设置itemData和dict时遇到问题。你能帮我吗?你只需在NSData中转换你的UIImage。你可以使用self.itemData=UIImagePNGRepresentation(yourUIImageInstance);在我的代码中self.itemData是一个属性类型NSData。你能再解释一下吗?我现在不明白你的问题。如果你想用浏览器检查你的php代码,你必须用一个表单和一个输入类型文件(你发布的那行)创建一个简单的页面。表单方法是post,操作是php接收者页面的地址。url.com/upload.php“>然后只需选择文件并上载。对此,我用浏览器检查了它,并确认了。现在我想知道如何用您的变量替换我的变量。如以下代码所示:NSDictionary*dict=[NSDictionary Dictionary WithObjects:@[self.itemValue]forKeys:@[@“item”]];我想知道self.itemValue和item是什么,以便可以替换它们。对于:[formData appendPartWithFileData:self.itemData名称:@“imageData”文件名:@“imageData.jpg”mimeType:@“image/png”];
function upload(){
$uploaddir = 'uploads/';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    sendResponse(200, 'Upload Successful');
    return true;
}
sendResponse(403, 'Upload Failed');
    return false;
}
NSString *uploadURL = @"http://www.baseurl.it/";
NSURL *url = [[NSURL alloc]initWithString:uploadURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSDictionary *dict = [NSDictionary
                      dictionaryWithObjects:@[self.itemValue]
                      forKeys:@[@"item"]];
// self.itemValue is a post parameter type nsstring

NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"path/to/page.php" parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
// self.itemData is an NSData containing the image
    if (self.itemData)
        [formData appendPartWithFileData:self.itemData name:@"imageData" fileName:@"imageData.jpg" mimeType:@"image/png"];
}];

// sorry for the formatting here, is a long method using blocks
AFJSONRequestOperation *json = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                                                                                   NSLog(@"Upload Success");
                                                                               }                                                                                   
                                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                   NSLog(@"Error: %@", error.description);

                                                                               }];
[json start];
// file upload
try {
$fileName = $_FILES['imageData']['name'];
$tmpName  = $_FILES['imageData']['tmp_name'];
$fileSize = $_FILES['imageData']['size'];
$fileType = $_FILES['imageData']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

} catch (Exception $e) {
    echo json_encode("Error:".$e);
}

// $content contains your image