内部服务器错误500上传图像iOS PHP

时间:2013-01-31 06:55:10

标签: php objective-c image post

我看过很多人有同样的问题,都是相似的,与我的服务器设置有关,确保图片dir有755权限,并且由于某种原因我看到我的用户子文件夹设置为777,而我已经创建了php.ini文件,我确保我的upload_max_filesize设置正确,并且我的php.ini有适当的设置。我从许多不同的教程中尝试了几种不同的编码技术,都输出了相同的错误。

这是我的NSlog中的错误

myproject[11784:907] test <!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,
 webmaster@myproject.com and inform them of the time the error occurred,
and anything you might have done that may have
caused the 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>

这是我的Objective-c代码

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self  dismissViewControllerAnimated:YES completion:NULL];

NSData *dataImage = UIImageJPEGRepresentation(image.image, 0.8f);
NSString *urlString = @"http://www.myproject.com/myphpscript.php";
NSString *filename = [NSString stringWithFormat:@"%@.jpg", [MyClass str]];
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 *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@.png\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:dataImage]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"email"] dataUsingEncoding:NSASCIIStringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@",[MyClass str]] dataUsingEncoding:NSASCIIStringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString* returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"test %@",returnString);
}

和我的PHP

<?php
// removed connection 
$mysqli = new mysqli($host, $username, $password, $database);
if ($stmt = $mysqli->prepare("INSERT INTO `user` SET imagepath=? WHERE email=?")) 
{
    $stmt->bind_param('ss', $albumname, $email);

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

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

}
$albumname = $uploadfile;
    $email = $_POST['email'];
    $stmt->execute();
    $stmt->close(); 
    }


else {
    printf("Prepared Statement Error: %s\n", $mysqli->error);
}

$mysqli->close();
?>

2 个答案:

答案 0 :(得分:0)

错误500是服务器端的错误。

我建议尝试从服务器上传到自己,这样你可能会得到更多信息有什么不对。还要检查来自您的Web服务器,nginx,appache或者您运行过的错误日志,在那里您将找到导致错误的原因。

答案 1 :(得分:0)

虽然我无法单独从您的代码中看到500错误,但我强烈建议您确保您的PHP ini文件启用了错误日志记录并查看日志文件。在大多数情况下,500错误是由PHP引擎中的运行时错误引起的,它将输出到日志文件,或者如果启用了display_errors,则会直接输出到浏览器。

一旦您可以读取日志文件,就可以轻松修复错误9次中的9次。