【问题标题】:Error uploading photo from iOS to server将照片从 iOS 上传到服务器时出错
【发布时间】:2014-05-13 13:44:29
【问题描述】:

我在将照片从我的 iOS 应用程序上传到我的网络服务器时出错。

我想知道是否有人知道问题可能出在哪里,以及我该如何解决它。任何帮助将不胜感激。

这是错误信息:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at 
 webmaster@namehere.ca to inform them of the time this error occurred,
 and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

这是 iOS 代码:

- (IBAction)uploadPhoto:(id)sender {
    NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 90);
    NSString *urlString = @"http://website.ca/uploadPhoto.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:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" 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 = 'uploads/';
$ran = rand () ;

$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir .$ran.$file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "Uploaded";
}
?>

【问题讨论】:

  • 做一个 print_r($_FILES);并在此处添加。
  • 什么错误给你?
  • 我应该推荐使用 AFNetworking 它更简单的包装器来做这些事情。因为你的 PHP 代码似乎是正确的。发布二进制数据传输时,代码问题应该在您的Objective-C代码上

标签: php ios apache http


【解决方案1】:

图像在 $_POST['userfile'] 而不是 $_Files['userfile'] 中接收...使用以下代码

 <?php


$image = $_POST['userfile'];
$image = str_replace("<","",$image);
$image = str_replace(">","",$image);
$image = str_replace(" ","",$image);
$image = pack("H*", $image);
$filename = "img/image.png";
$f = fopen($filename,'wb');
fwrite($f, $image);
fclose($f);



?>

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 2011-04-11
    • 2012-05-02
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    相关资源
    最近更新 更多