【问题标题】:can't upload uiimage from ios to server with php无法使用php将uiimage从ios上传到服务器
【发布时间】:2015-07-24 09:44:02
【问题描述】:

这是我的代码:

NSString *image_name = @"test.jpg";
NSData *imageData = UIImageJPEGRepresentation(self.myImage.image, 0.1);

NSString *urlString = [NSString stringWithFormat:@"http://xxx/uploadfile.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"];

NSMutableData *body = [NSMutableData data];

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

[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"%@\"\r\n",image_name] 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];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);

这里是php代码:

<?php 
$uploaddir = 'C:\'; 
$file = basename($_FILES['file']['name']); 
$uploadfile = $uploaddir . $file; 
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) 
    { echo "Received file" . $_FILES; } 
?> 

但它不起作用。响应字符串说:

500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.

【问题讨论】:

    标签: php ios image upload server


    【解决方案1】:

    这是IOS端的代码

    - (IBAction)uploadImage {
        /*
         turning the image into a NSData object
         getting the image back out of the UIImageView
         setting the quality to 90
        */
        NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
        // setting up the URL to post to
        NSString *urlString = @"http://XXXXX.com/test-upload.php";
    
        // setting up the request object now
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];
    
        /*
         add some header info now
         we always need a boundary when we post a file
         also we need to set the content type
    
         You might want to generate a random boundary.. this is just the same
         as my output from wireshark on a valid html post
        */
        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
        /*
         now lets create the body of the post
        */
        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"--%@",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\""] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type:application/octet-stream"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // setting the body of the post to the reqeust
        [request setHTTPBody:body];
    
        // now lets make the connection to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    
        NSLog(returnString);
    }
    

    这里是抓取上传文件的php函数:

    $uploaddir = './uploads/';
    $file = basename($_FILES['userfile']['name']);
    $uploadfile = $uploaddir . $file;
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
            echo "http://XXXXXXX.com/uploads/{$file}";
    }
    

    之后,您需要使用 xampp 或 WAMMP 转到您的 PHP 项目放置的目录,以提供完全访问权限,再考虑需要在该项目调用中创建一个文件夹,作为本教程的 uploads

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-09
      • 2011-05-30
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2013-06-22
      相关资源
      最近更新 更多