无法将图像上传到服务器ios [英] Unable to Upload Image to Server ios

查看:193
本文介绍了无法将图像上传到服务器ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是我的代码上传图像

   - (IBAction)btnUpload:(id)sender {

if(self.imageViewGallery.image == nil){
UIAlertView * ErrorAlert = [[UIAlertView alloc] initWithTitle:@等待
消息:@请选择一个图片上传。委托:无
cancelButtonTitle:@OK
otherButtonTitles:nil,nil];
[ErrorAlert show];
NSLog(@error);
}
else {
NSData * imageData = UIImageJPEGRepresentation(self.imageViewGallery.image,90);
NSString * urlString = @http://localhost/ColorPicker/api.upload.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];
NSString * imgName = LIbraryImage;
NSLog(@image name:%@,imgName);

NSMutableData * body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
$ b $ [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@Content-Disposition:form-data; name = \files \; filename = \%@ \ \r\\\
,imgName]] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@Content-Type:application / octet-stream \r\\\
\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ - \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];
NSLog(@setRequest:%@,request);


NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@returnstring is:%@,returnString);



$ b $现在这里是我的服务器端代码。

 <?php 

$图片= $ _FILES ['文件']['名称'];
print_r($ _ FILES);
foreach($ Image为$ f => $ name){

$ allowedExts = array(gif,jpeg,jpg,png);
$ temp = explode(。,$ name);
$ extension = end($ temp); ($($ _FILES ['files'] ['type'] [$ f] ==image / gif)||($ _FILES ['files'] ['type' ]($ _FILES ['files'] ['type'] [$ f] ==image / jpg)||($ _FILES ['files'] ['type'] [$ f] ==image / png))&&($ _FILES ['files'] ['size'] [$ f]< 2000000)&& in_array($ $ {$ b $ if($ _FILES ['files'] ['error'] [$ f]> 0){
echo返回码:。 $ _FILES ['files'] ['error'] [$ f]。 <峰; br> 中;
$ else $ {
$ b $ if(file_exists(upload /。$ name)){

} else {
move_uploaded_file($ _ FILES [文件'] ['tmp_name'] [$ f],'upload /'。uniqid()。_。$ name);
}
}
} else {
$ error =文件无效;
}

}
?>

现在每当我尝试上传时都会发生这样的警告

 < b>警告< / b> ;:为< b> / Applications / XAMPP / xamppfiles / htdocs / ColorPicker / uploadAction中的foreach()提供的无效参数。 PHP和LT; / b个在线< b> 5< / b>< br /> 


解决方案

$ _ FILES 。从PHP 4.3.0开始, $ _ REQUEST 不再包含以这种方式上传的文件的任何信息。 其余的代码是基于图像没有被上传为多部分的想法,而是以标准形式上传为base-64字符串。您只需从 $ _ FILES 获取文件的相关信息,然后使用 move_uploaded_file


Hello I am trying to upload Image From my IOS device to server.

And Here Is my code to upload the Image

- (IBAction)btnUpload:(id)sender {

    if (self.imageViewGallery.image == nil) {
        UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Wait"
                                                             message:@"Please Select An Image To Upload." delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil, nil];
        [ErrorAlert show];
        NSLog(@"error");
    }
    else{
    NSData *imageData = UIImageJPEGRepresentation(self.imageViewGallery.image, 90);
    NSString *urlString = @"http://localhost/ColorPicker/api.upload.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"];
        NSString *imgName = LIbraryImage;
        NSLog(@"image name : %@",imgName);

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@\"\r\n", imgName]] 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];
        NSLog(@"setRequest : %@", request);


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnstring is : %@",returnString);
    }

Now here is my server side code. It is a bit long.

  <?php

$Image = $_FILES['files']['name'];
    print_r($_FILES);
foreach ($Image as $f => $name) {

    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $name);
    $extension = end($temp);

    if ((($_FILES['files']['type'][$f] == "image/gif") || ($_FILES['files']['type'][$f] == "image/jpeg") || ($_FILES['files']['type'][$f] == "image/jpg") || ($_FILES['files']['type'][$f] == "image/png")) && ($_FILES['files']['size'][$f] < 2000000) && in_array($extension, $allowedExts)) {
        if ($_FILES['files']['error'][$f] > 0) {
            echo "Return Code: " . $_FILES['files']['error'][$f] . "<br>";
        } else {

            if (file_exists("upload/" . $name)) {

            } else {
                move_uploaded_file($_FILES['files']['tmp_name'][$f], 'upload/' . uniqid() . "_" . $name);
            }
        }
    } else {
        $error = "Invalid file";
    }

}
?>

Now what happen every time I try to upload got this warning

<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/Applications/XAMPP/xamppfiles/htdocs/ColorPicker/uploadAction.php</b> on line <b>5</b><br />

解决方案

When you upload image data as multipart, it end up in $_FILES. Since PHP 4.3.0, $_REQUEST no longer contains any information about files uploaded in this manner.

The rest of your code is based around the idea of the image not being uploaded as multipart but instead being uploaded as a base-64 string in a standard form. Instead of writing base 64 data to a file, you will simply need to get the information about the file from $_FILES and move it to your target directory with move_uploaded_file.

这篇关于无法将图像上传到服务器ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆